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

本文介绍了一道USACO竞赛题“守护农场”的解决思路。任务是计算地图上的山丘数量,以便确定放置哨岗的位置。通过贪心算法与深度优先搜索(DFS)策略,实现对每个可能山丘顶点的遍历与计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 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.

农场里有许多山丘,在山丘上约翰要设置哨岗来保卫他的价值连城的奶牛.

约翰不知道有多少山丘,也就不知道要设置多少哨岗.他有一张地图,用整数矩阵的方式描 述了农场N(1 <= N<=700)行M(1 < M<=700)列块土地的海拔高度好 H_ij (0 <= H_ij <= 10,000).请帮他 计算山丘的数量.

一个山丘是指某一个方格,与之相邻的方格的海拔高度均严格小于它.当然,与它相邻的方 格可以是上下左右的那四个,也可以是对角线上相邻的四个.

输入输出格式

输入格式:

 

  • 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

 

输出格式:

 

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

 

输入输出样例

输入样例#1:
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 
输出样例#1:
3 

说明

There are three peaks: The one with height 4 on the left top, one of the points with height 2 at the bottom part, and one of the points with height 1 on the right top corner.

 

 

贪心+dfs

我们知道山峰为中间高四周低的地方,这样我们寻找一下有没有这样的区域使中间高。我们可以存一下比较高的区域,然后判断每一个高的区域是否已经属于另一座山峰,如果不是,则说明这个地即为下一个山峰的最高点,然后从这个点进行dfs,遍历它周围可以与她组成一个山峰的地方,我们知道这样的地方的高度一定比他低

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1100
using namespace std;
bool vis[N][N];
int n,m,x,y,h[N][N],ans,sum;
int xx[8]={0,0,-1,-1,-1,1,1,1},yy[8]={1,-1,1,0,-1,1,0,-1};
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
struct Node
{
    int x,y,h;
}node[N*N];
int cmp(Node a,Node b)
{
    return a.h>b.h;
}
void dfs(int x,int y)
{
    for(int i=0;i<8;i++)
    {
        int fx=x+xx[i],fy=y+yy[i];
        if(fx<1||fy<1||fx>n||fy>m) continue;
        if(h[fx][fy]<=h[x][y]&&!vis[fx][fy])
        {
            vis[fx][fy]=true;
            dfs(fx,fy);
         } 
    }
    return ;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
     {
         h[i][j]=read();
         node[++sum].x=i;
         node[sum].y=j;
         node[sum].h=h[i][j];
      } 
    sort(node+1,node+1+sum,cmp);
    for(int i=1;i<=sum;i++)
    {
        x=node[i].x,y=node[i].y;
        if(vis[x][y]) continue;
        vis[x][y]=true;
        ans++; dfs(x,y);
    }
    printf("%d",ans);
    return  0;
}

 

转载于:https://www.cnblogs.com/z360/p/7590742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值