[Ybtoj 1.5 avancé haute efficacité] B. Pics et vallées [Recherche large]

Insérez la description de l'image ici
Insérez la description de l'image ici

une analyse

Énumérez d'abord chaque point, et s'il n'a pas été visité, recherchez le "bloc" entier avec la même hauteur.
Ensuite, jugez si le bloc à côté est plus grand ou plus court que lui, et
additionnez le nombre de pics et de vallées

Télécharger le code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node 
{
    
    
	int x,y;
}q[1000001];
int n,cnt,sf,sg;
int a[1001][1001];
int v[1001][1001];
int dx[9]={
    
    0,0,1,0,-1,1,1,-1,-1};
int dy[9]={
    
    0,1,0,-1,0,1,-1,1,-1};

void bfs(int x,int y)
{
    
    
	int h=0,t=1;
	int f1=0,f2=0;
	q[1].x=x;q[1].y=y;
	while(h<t)
	{
    
    
		h++;
		for(int i=1;i<=8;i++)
		{
    
    
		    int xx=q[h].x+dx[i];
		    int yy=q[h].y+dy[i];
		    if(xx<1||xx>n||yy<1||yy>n) continue;
		    if(!v[xx][yy]&&a[xx][yy]==a[q[h].x][q[h].y])
		   	{
    
    
		   		v[xx][yy]=1;
		   		t++;
		   		q[t].x=xx;
		   		q[t].y=yy;
			}
			if(a[xx][yy]>a[q[h].x][q[h].y]) f1=1;
			if(a[xx][yy]<a[q[h].x][q[h].y]) f2=1;
		}
	}
	if(f1&&!f2) sg++;
	if(f2&&!f1) sf++;
}

int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=n;j++)
		{
    
    
			cin>>a[i][j];
		}
	}
    for(int i=1;i<=n;i++)
    {
    
    
    	for(int j=1;j<=n;j++)
    	{
    
    
    		if(!v[i][j])
    		{
    
    
    			v[i][j]=1;
    			bfs(i,j);
    			cnt++;
			}
		}
	}
	if(cnt==1)
	{
    
    
		cout<<1<<' '<<1;
		return 0;
	}
	else cout<<sf<<' '<<sg;
	return 0;
}

Je suppose que tu aimes

Origine blog.csdn.net/dglyr/article/details/112984630
conseillé
Classement