Description
Input
Output
Sample Input
5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8
输入样例2
5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7
Sample Output
2 1
输出样例2
3 3
HINT
笑而不语……原来bzoj上也有这种水题
直接上爆搜不解释
#include<cstdio> #define N 1010 const int mx[8]={1,1,1,0,0,-1,-1,-1}; const int my[8]={1,0,-1,1,-1,1,0,-1}; bool vis[N][N]; int a[N][N]; int n,s1,s2,t,w,nx,ny,wx,wy; int qx[N*N],qy[N*N]; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void bfs(int sx,int sy) { t=0;w=1;qx[1]=sx;qy[1]=sy; vis[sx][sy]=1; bool mrk1=1,mrk2=1; for (int j=0;j<8;j++) if (sx+mx[j]>=1&&sx+mx[j]<=n&&sy+my[j]>=1&&sy+my[j]<=n) { int t=a[sx+mx[j]][sy+my[j]]; if (t<a[sx][sy])mrk1=0; if (t>a[sx][sy])mrk2=0; } while(t<w) { nx=qx[++t]; ny=qy[t]; for (int k=0;k<8;k++) { wx=nx+mx[k]; wy=ny+my[k]; if (wx<1||wx>n||wy<1||wy>n||a[wx][wy]!=a[sx][sy]||vis[wx][wy])continue; if (!vis[wx][wy]) { qx[++w]=wx; qy[w]=wy; vis[wx][wy]=1; for (int j=0;j<8;j++) if (wx+mx[j]>=1&&wx+mx[j]<=n&&wy+my[j]>=1&&wy+my[j]<=n) { int t=a[wx+mx[j]][wy+my[j]]; if (t<a[sx][sy])mrk1=0; if (t>a[sx][sy])mrk2=0; } } } } if (mrk1)s1++; if (mrk2)s2++; } int main() { n=read(); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) a[i][j]=read(); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if(!vis[i][j])bfs(i,j); printf("%d %d\n",s2,s1); }