山峰和山谷Grz 黑暗爆炸 - 1102
考察:Flood Fill(dfs or bfs)
思路:
用Flood Fill算法时,判定边界是否有> < 的高度.
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int N = 1001; 6 int mp[N][N]; 7 int xx[8] = {-1,1,0,0,-1,-1,1,1},yy[8] = {0,0,-1,1,-1,1,-1,1}; 8 int cnth,cntl,n; 9 bool st[N][N],high,low; 10 void dfs(int x,int y) 11 { 12 st[x][y] = 1; 13 for(int i=0;i<8;i++) 14 { 15 int dx = x+xx[i],dy = y+yy[i]; 16 if(dx>=1&&dx<=n&&dy>=1&&dy<=n) 17 { 18 if(mp[dx][dy]>mp[x][y]) high = 0; 19 if(mp[dx][dy]<mp[x][y]) low = 0; 20 if(mp[dx][dy]==mp[x][y]&&st[dx][dy]) dfs(dx,dy); 21 } 22 } 23 } 24 int main() 25 { 26 scanf("%d",&n); 27 for(int i=1;i<=n;i++) 28 for(int j=1;j<=n;j++) scanf("%d",&mp[i][j]); 29 for(int i=1;i<=n;i++) 30 for(int j=1;j<=n;j++) 31 if(!st[i][j]) 32 { 33 high = 1,low = 1; 34 dfs(i,j); 35 if(high) cnth++; 36 if(low) cntl++; 37 } 38 printf("%d %d\n",cnth,cntl); 39 return 0; 40 }