BZOJ1102 [POI2007]山峰和山谷Grz(洛谷P3456)

版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/81986740

BFS

BZOJ题目传送门
洛谷题目传送门

划水

对每个联通块BFS一遍,然后就没了。。。

代码:

#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1005
#define F inline
using namespace std;
const int t1[8]={1,0,-1,0,1,1,-1,-1};
const int t2[8]={0,1,0,-1,1,-1,1,-1};
int n,ans1,ans2,w[N][N],f[N][N],q[N*N][2];
F char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    return l==r?EOF:*l++;
}
F int _read(){
    int x=0; char ch=readc();
    while (!isdigit(ch)) ch=readc();
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
    return x;
}
F void bfs(int sx,int sy){
    int l=0,r=1,f1=1,f2=1; q[1][0]=sx,q[1][1]=sy,f[sx][sy]=true;
    while (l<r){
        int xx=q[++l][0],yy=q[l][1];
        for (int i=0;i<8;i++){
            int x=xx+t1[i],y=yy+t2[i];
            if (x&&x<=n&&y&&y<=n)
                if (w[xx][yy]>w[x][y]) f1=0;
                else if (w[xx][yy]==w[x][y]&&!f[x][y]){
                    q[++r][0]=x,q[r][1]=y,f[x][y]=true;
                }
                else if (w[xx][yy]<w[x][y]) f2=0;
        }
    }
    if (!f1&&f2) ans1++;
    if (f1&&!f2) ans2++;
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            w[i][j]=_read();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (!f[i][j]) bfs(i,j);
    if (!ans1&&!ans2) ans1=ans2=1;
    return printf("%d %d\n",ans1,ans2),0;
}

猜你喜欢

转载自blog.csdn.net/a1799342217/article/details/81986740