bzoj1102: [POI2007]山峰和山谷Grz

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #define maxn 1005
 7 using namespace std;
 8 
 9 int n,sumx,sumy,ansx,ansy,hi[maxn][maxn],list[maxn*maxn][2],head,tail;
10 bool vis[maxn][maxn];
11 const int dx[8]={-1,0,1,-1,1,-1,0,1};
12 const int dy[8]={-1,-1,-1,0,0,1,1,1};
13 void init(int x){
14     for (int i=1;i<=x;i++){
15         for (int j=1;j<=x;j++){
16             scanf("%d",&hi[i][j]);
17         }
18     }
19 }
20 void bfs(int x,int y){
21     int a,b,c,d;
22     head=0,tail=1,list[tail][0]=x,list[tail][1]=y; sumx=sumy=0,vis[x][y]=1;
23     while (head<tail){
24         ++head,a=list[head][0],b=list[head][1];
25         for (int i=0;i<8;i++){
26             c=a+dx[i],d=b+dy[i];
27             if (c>=1&&c<=n&&d>=1&&d<=n){
28                 if (hi[c][d]==hi[a][b]&&!vis[c][d]){
29                     vis[c][d]=1;
30                     list[++tail][0]=c,list[tail][1]=d;
31                 }else{
32                     if (hi[c][d]>hi[a][b]) sumx++;
33                     if (hi[c][d]<hi[a][b]) sumy++;
34                 }
35             }
36         }
37     }
38 //    printf("%d %d %d %d\n",x,y,sumx,sumy);
39     if (sumx==0&&sumy==0) ansx++,ansy++;
40     else if (sumx>0&&sumy>0) return;
41     else if (sumx>0) ansy++;
42     else ansx++;
43 }
44 int main(){
45     scanf("%d",&n);
46     init(n);
47     memset(vis,0,sizeof(vis));
48     ansx=ansy=0;
49     for (int i=1;i<=n;i++){
50         for (int j=1;j<=n;j++){
51             if (vis[i][j]==1) continue;
52             bfs(i,j);
53         }
54     }
55     printf("%d %d\n",ansx,ansy);
56     return 0;
57 }
View Code

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1102

做法:为noip做准备。bfs即可。

posted @ 2016-06-06 00:28  oyzx~  阅读(161)  评论(0编辑  收藏