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 }

原文地址:https://www.cnblogs.com/kafuuchino/p/9862138.html

时间: 2024-11-10 17:56:05

bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm的相关文章

洛谷 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

题解 P2919 【[USACO08NOV]守护农场Guarding the Farm】

rt 若地图中一个元素所邻接的所有元素都比这个元素高度要小(或它邻接的是地图的边界),则该元素和其周围所有按照这样顺序排列的元素的集合称为一个小山丘. 那么我们可以用一个结构体类型记录所有非零高度的横纵坐标以及相应的高度值 然后对高度进行排序 对每一次高度进行$dfs$ 及时删除所有的联通高度的小山丘 很显然 如果从一个给定高度的山丘的最高高度进行$dfs$ 一定可以将该山丘的所有高度清零 犯了一个很zz的错误 在递归调用的时候 $tmp$定义的全局变量 然后在递归调用的时候 在下一次递归结束

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

洛谷 P3079 [USACO13MAR]农场的画Farm Painting

P3079 [USACO13MAR]农场的画Farm Painting 题目描述 After several harsh winters, Farmer John has decided it is time to re-paint his farm. The farm consists of N fenced enclosures (1 <= N <= 50,000), each of which can be described by a rectangle in the 2D plane

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 matr

[USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of  barns connected with  bidirectional paths between some pairs of barn

洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of  barns connected with  bidirectional paths between some pairs of barn

[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

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734