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

rt

若地图中一个元素所邻接的所有元素都比这个元素高度要小(或它邻接的是地图的边界),则该元素和其周围所有按照这样顺序排列的元素的集合称为一个小山丘。

那么我们可以用一个结构体类型记录所有非零高度的横纵坐标以及相应的高度值

然后对高度进行排序 对每一次高度进行$dfs$ 及时删除所有的联通高度的小山丘

很显然 如果从一个给定高度的山丘的最高高度进行$dfs$ 一定可以将该山丘的所有高度清零

犯了一个很zz的错误

在递归调用的时候 $tmp$定义的全局变量 然后在递归调用的时候 在下一次递归结束

之后 tmp的值已经被改变了 而当前层的tmp还未判断完成

我发誓我以后在$dfs$里面只用局部变量 手动再见

代码如下:

#include<bits/stdc++.h>
using namespace std;
int a[705][705],ans,px,py,tot,m,n;
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,0,1,-1,0,1,-1};
struct p{
    int x,y,num;
}f[490005];
void dfs(int x,int y){
    int tmp=a[x][y];//在删除之前保存数据
    //!!!每次循环都要调用tmp 在下一次递归结束之后 tmp的值已经被改变了 而当前层的tmp还未判断完成
    a[x][y]=0;
    for (int i=0;i<=7;i++){
        px=x+dx[i];py=y+dy[i];
        if (px>0&&px<=n&&py>0&&py<=m&&tmp>=a[px][py]&&a[px][py]!=0) dfs(px,py);
    }
}
bool cmp(p a,p b){
    return a.num>b.num;
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            if (a[i][j]) {
                tot++;
                f[tot].x=i;
                f[tot].y=j;
                f[tot].num=a[i][j];
            }
        }
    sort(f+1,f+tot+1,cmp);
    for (int i=1;i<=tot;i++)
        if (a[f[i].x][f[i].y]) {//如果在删除过程当中没有被删除
            ans++;
            dfs(f[i].x,f[i].y);
        }
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Hiraeth-dh/p/10819929.html

时间: 2024-08-30 07:00:11

题解 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

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

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

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目链接 数据规模16-20的都是状压 如果要每一位都压序号的话空间肯定是不够的,所以每一位只能是0或1.1表示有这头牛,0表示没有这头牛.显然每个位置的选择和他两边的牛有关,所以我们就可以定义这样的状态: f[i][j]表示使用集合i的牛,其中最后一头牛的序号为j时的总方案数.答案从f[(1 << n) - 1][n]累加即可. 转移也比较好想,从i中枚举选出倒数第二头牛,作为子状态的最后一头牛,注意边界情况.

[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

[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

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

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