序:为什么写poi,zy说poi都是思路题目,不像hnoi妈的数据结构队。。。。。

1.bzoj1102

题目大意:定义了一个山谷和山峰,求他们数量。

题解:这种题bfs咯,在bfs的时候记录一下相邻的比我大的有多少,比我小的有多少,然后更新答案;

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define maxn 2000
 7 using namespace std;
 8 const int dx[8]={-1,0,1,-1,1,-1,0,1};
 9 const int dy[8]={-1,-1,-1,0,0,1,1,1};
10 int n,summ,sumn,ansm,ansn;
11 int lis[maxn*maxn][3];
12 int a[maxn][maxn];
13 bool vis[maxn][maxn];
14 int read()
15 {
16     int x=0; char ch; bool bo=0;
17     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;
18     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9');
19     if (bo) return -x; return x;
20 } 
21 void bfs(int x,int y)
22 {
23     int t=1,h=1; lis[1][1]=x; lis[1][2]=y; sumn=summ=0; vis[x][y]=1;
24     while (h<=t)
25     {
26         int aa=lis[h][1],bb=lis[h][2];
27         for (int i=0; i<8; i++)
28         {
29             int xx=aa+dx[i],yy=bb+dy[i];
30             if (xx<1 || xx>n || yy<1 || yy>n) continue;
31             if (a[xx][yy]==a[aa][bb] && !vis[xx][yy]) 
32             {
33                 ++t; lis[t][1]=xx; lis[t][2]=yy;
34                 vis[xx][yy]=1;
35             }
36             else
37             {
38                 if (a[xx][yy]<a[aa][bb]) summ++;
39                 if (a[xx][yy]>a[aa][bb]) sumn++;
40             }
41         }
42         h++;
43     }
44     if (!summ && !sumn) ansm++,ansn++;
45     else if (summ && sumn) return;
46     else if (summ) ansm++;
47     else ansn++;
48 }
49 int main()
50 {
51     memset(vis,0,sizeof(vis));
52     n=read();
53     for (int i=1; i<=n; i++) 
54     for (int j=1; j<=n; j++)
55     a[i][j]=read();
56     ansn=ansm=0;
57     for (int i=1; i<=n; i++) 
58         for (int j=1; j<=n; j++) 
59         if (!vis[i][j]) bfs(i,j);
60     printf("%d %d\n",ansm,ansn); 
61 }
View Code

相关文章:

  • 2022-01-11
  • 2021-10-16
  • 2021-11-09
  • 2021-12-01
  • 2022-02-09
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
相关资源
相似解决方案