BZOJ1619[Usaco2008 Nov]Guarding the Farm 保卫牧场

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ1619[Usaco2008 Nov]Guarding the Farm 保卫牧场相关的知识,希望对你有一定的参考价值。

Description

The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows. He wonders how many guards he will need if he wishes to put one on top of each hill. He has a map supplied as a matrix of integers; the matrix has N (1 < N <= 700) rows and M (1 < M <= 700) columns. Each member of the matrix is an altitude H_ij (0 <= H_ij <= 10,000). Help him determine the number of hilltops on the map. A hilltop is one or more adjacent matrix elements of the same value surrounded exclusively by either the edge of the map or elements with a lower (smaller) altitude. Two different elements are adjacent if the magnitude of difference in their X coordinates is no greater than 1 and the magnitude of differences in their Y coordinates is also no greater than 1.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Line i+1 describes row i of the matrix with M space-separated integers: H_ij

Output

* Line 1: A single integer that specifies the number of hilltops

Sample Input

8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0

Sample Output

3

HINT

 

   三个山丘分别是:左上角的高度为4的方格,右上角的高度为1的方格,还有最后一行中高度为2的方格.
bfs,找周围一圈的点,如果相同则加入队列,如果有高于本点的则不成为山丘;成为山丘的点要打上标记。
技术分享
 1 #include<cstdio>
 2 using namespace std;
 3 int n,m,ans;
 4 int list[500000][2];
 5 int map[705][705];
 6 bool bo[705][705];
 7 int dx[8]={0,1,0,-1,1,-1,1,-1};
 8 int dy[8]={1,0,-1,0,1,1,-1,-1};
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     for(;ch<0||ch>9;ch=getchar())if(ch==-)f=-1;
12     for(;ch>=0&&ch<=9;ch=getchar())x=(x<<1)+(x<<3)+ch-0;
13     return x*f;
14 }
15 bool bfs(int sx,int sy){
16     int t=1,h=1;
17     bool boo=true;
18     list[1][0]=sx,list[1][1]=sy;
19     while(h<=t){
20         int nx=list[h][0],ny=list[h][1];
21         for(int i=0;i<=7;i++){
22             int x=nx+dx[i],y=ny+dy[i];
23             if(x<1||x>n||y<1||y>m)continue;
24             if(map[x][y]>map[nx][ny]){boo=false;continue;}
25             if(map[x][y]==map[nx][ny]&&!bo[x][y]){
26                 bo[x][y]=true;
27                 list[++t][0]=x;
28                 list[t][1]=y;
29             }
30         }
31         h++;
32     }
33     for(int i=1;i<=t;i++)list[i][0]=list[i][1]=0;
34     return boo;
35 }
36 int main(){
37     n=read(),m=read();
38     for(int i=1;i<=n;i++)
39         for(int j=1;j<=m;j++)
40             map[i][j]=read();
41     for(int i=1;i<=n;i++)
42         for(int j=1;j<=m;j++)
43             if(!bo[i][j])
44                 if(bfs(i,j))ans++;
45     printf("%d\n",ans);
46     return 0;
47 }
View Code











以上是关于BZOJ1619[Usaco2008 Nov]Guarding the Farm 保卫牧场的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ_1619_[Usaco2008_Nov]_Guarding_the_Farm_保卫牧场_(模拟+bfs)

1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 搜索

bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm

Bzoj 1229: [USACO2008 Nov]toy 玩具

bzoj1230 [Usaco2008 Nov]lites 开关灯

[BZOJ1620][Usaco2008 Nov]Time Management 时间管理