P3456 [POI2007]GRZ-Ridges and Valleys(bfs)

P3456 [POI2007]GRZ-Ridges and Valleys

八个方向都跑一遍bfs,顺便判断一下是山峰还是山谷,或者是山坡(俩都不是)

(实在不知道要说啥了qwq)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
inline int Int(){
    char c=getchar(); int x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x;
}
int d1[8]={0,1,0,-1,1,1,-1,-1};
int d2[8]={1,0,-1,0,1,-1,1,-1};
int n,a[1002][1002],ridge,valley;
bool vis[1002][1002];
struct data{int x,y;};
inline void bfs(data f){
    queue <data> h; h.push(f);
    int p=-1; vis[f.x][f.y]=1; //p:该处的状态
    while(!h.empty()){
        data u=h.front(); h.pop();
        for(int i=0;i<8;++i){
            int r1=u.x+d1[i],r2=u.y+d2[i];
            if(r1<1||r1>n||r2<1||r2>n) continue;
            if(a[u.x][u.y]!=a[r1][r2]){
                if(p==-1) p=(a[u.x][u.y]>a[r1][r2]);
                else p= p==(a[u.x][u.y]>a[r1][r2]) ? p:2;
                continue; //直接写成这样方便(逃
            }
            if(!vis[r1][r2]) vis[r1][r2]=1; //标记记得打
            else continue;
            h.push((data){r1,r2});
        }
    }
    if(p==-1) ++ridge,++valley; //整个图都是一块相同海拔的
    else if(p==0) ++valley;
    else if(p==1) ++ridge;
}
int main(){
    n=Int();
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            a[i][j]=Int();
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            if(!vis[i][j])
                bfs((data){i,j});
    printf("%d %d",ridge,valley);
    return 0;
}

 

posted @ 2018-09-04 21:16  kafuuchino  阅读(198)  评论(0编辑  收藏  举报