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 }

时间: 2024-08-27 18:41:31

BZOJ1619[Usaco2008 Nov]Guarding the Farm 保卫牧场的相关文章

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

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1619 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 694 Solved: 306 [Submit][Status][Discuss] Description The farm has many hills upon which Farmer John wou

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

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 matr

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

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1619 给出一张图每个点的高度,在一个点上安排守卫可以监视周围所有不高于于当前点的点.也就是类似在一个点上灌水,周围(8格)低于它的点都会有水,然后继续...求最少的守卫数. 分析 首先图中的最高点必须是要灌水的,所以就从当前最高的开始宽搜,有水的点标记掉就好了. 1 #include <bits/stdc++.h> 2 #define fst first 3 #define scd sec

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

洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm

P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 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

bzoj1230 [Usaco2008 Nov]lites 开关灯

1230: [Usaco2008 Nov]lites 开关灯 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1615  Solved: 847[Submit][Status][Discuss] Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每一头被连续的编号为1..N, 站在一个彩色的灯下面.刚到傍晚的时候

[BZOJ] 1618: [Usaco2008 Nov]Buying Hay 购买干草

1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1216  Solved: 633[Submit][Status][Discuss] Description 约翰的干草库存已经告罄,他打算为奶牛们采购H(1≤H≤50000)磅干草,他知道N(1≤N≤100)个干草公司,现在用1到 N给它们编号.第i个公司卖的干草包重量为Pi(1≤Pi≤5000)磅,需要的开销为Ci(l≤Ci≤5

bzoj1620[Usaco2008 Nov]Time Management 时间管理*

bzoj1620[Usaco2008 Nov]Time Management 时间管理 题意: n个任务,每个有一个所需时间和最晚完成时刻,问最晚要从什么时候开始工作.n≤1000 题解: 贪心,按最晚完成时刻从早到晚排序,如果当前任务来不及完成,就将前面的任务往前推,否则累积一个“自由时间”.当推任务时,如果之前有“自由时间”,就用自由时间减往前推的时间,否则用最晚开始时间去减往前推的时间.反思:我开始贪错了,按最晚开始时刻从早到晚排序,结果WA很久.现在还是想不太清楚原因,希望哪位神犇能帮我

1230: [Usaco2008 Nov]lites 开关灯

1230: [Usaco2008 Nov]lites 开关灯 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1162  Solved: 589[Submit][Status] Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每一头被连续的编号为1..N, 站在一个彩色的灯下面.刚到傍晚的时候, 所有的灯都是关