bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm
相似题:P3456 [POI2007]GRZ-Ridges and Valleys
按海拔是否相同分块
每次bfs海拔相同的块,根据与周围的块的大小关系判断是否是山丘。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<cctype> 6 #define re register 7 using namespace std; 8 void read(int &x){ 9 char c=getchar();x=0; 10 while(!isdigit(c)) c=getchar(); 11 while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar(); 12 } 13 #define N 702 14 const int d1[8]={0,1,0,-1,1,1,-1,-1}; 15 const int d2[8]={1,0,-1,0,1,-1,1,-1}; 16 struct data{int x,y;}; 17 int n,m,H[N][N],ans; bool vis[N][N]; 18 void bfs(int f1,int f2){ 19 queue <data> h; h.push((data){f1,f2}); 20 vis[f1][f2]=1; int p=1; 21 while(!h.empty()){ 22 data u=h.front(); h.pop(); 23 for(int i=0;i<8;++i){ 24 int r1=u.x+d1[i],r2=u.y+d2[i]; 25 if(r1<1||r1>n||r2<1||r2>m) continue; 26 p&=(H[r1][r2]<=H[u.x][u.y]);//是否是山丘 27 if(vis[r1][r2]) continue; 28 if(H[r1][r2]==H[u.x][u.y]) 29 h.push((data){r1,r2}),vis[r1][r2]=1; 30 } 31 }ans+=p; 32 } 33 int main(){ 34 read(n);read(m); 35 for(re int i=1;i<=n;++i) 36 for(re int j=1;j<=m;++j) 37 read(H[i][j]); 38 for(re int i=1;i<=n;++i) 39 for(re int j=1;j<=m;++j) 40 if(!vis[i][j]) bfs(i,j); 41 printf("%d",ans); 42 return 0; 43 }