题意翻译

农夫John的农场里有很多小山丘,他想要在那里布置一些保镖去保卫他的那些相当值钱的奶牛们。

他想知道如果在一座小山丘上布置一名保镖的话,他最少总共需要招聘多少名保镖。他现在手头有一个用数字矩阵来表示地形的地图。这个矩阵有N行(1<N≤700)和M列( 1<M≤ 700) 。矩阵中的每个元素都有一个值H_ij(0≤H_ij≤10000)来表示该地区的海拔高度。

小山丘的定义是:若地图中一个元素所邻接的所有元素都比这个元素高度要小(或它邻接的是地图的边界),则该元素和其周围所有按照这样顺序排列的元素的集合称为一个小山丘。这里邻接的意义是:若一个位置的横纵坐标与另一个位置的横纵坐标相差不超过1,则称这两个元素邻接,比如某个非边界点的位置有8个相邻点:上、下、左、右、左上、右上、左下、右下。

请你帮助他统计出地图上最少且尽量高的小山丘数量。

题目描述

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.

输入格式

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

我们可以把获取的高度从高到底排一下序,然后从高到低依次去扩展,这样的话就省了很多问题;最后统计答案就好了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define mod 100000007
#define inf 336860180
#define PI 3.1415926
#define ll long long
using namespace std;
int n,m,ans,H,tot,h[702][702];
struct po{int x,y,h;}a[703*703];
queue<int>qx,qy;
int fx,fy,rx,ry;
bool v[1000][1000];
bool cmp(po xx,po yy){return xx.h>yy.h;}
int X[8]={0,0,1,-1,1,-1,1,-1};
int Y[8]={1,-1,0,0,1,1,-1,-1};
void bfs(int x,int y){qx.push(x);qy.push(y);v[x][y]=1;while(!qx.empty()){fx=qx.front();qx.pop();fy=qy.front();qy.pop();H=h[fx][fy];for(int i=0;i<=7;i++){rx=X[i]+fx;ry=Y[i]+fy;if(rx<1 || rx >n || ry<1 || ry>m){continue;}if(v[rx][ry]){continue;}if(h[rx][ry]<=H){qx.push(rx);qy.push(ry);v[rx][ry]=1;    }}    }
}
int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){tot++;scanf("%d",&a[tot].h);h[i][j]=a[tot].h;a[tot].x=i;a[tot].y=j;}}sort(a+1,a+tot+1,cmp);memset(v,0,sizeof(v));for(int i=1;i<=tot;i++){int xx=a[i].x,yy=a[i].y;if(v[xx][yy]){continue;}bfs(xx,yy);ans++;}printf("%d",ans);return 0;
}

转载于:https://www.cnblogs.com/hrj1/p/11257309.html

守护农场Guarding the Farm相关推荐

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

    题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...

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

    题目链接:https://www.luogu.org/problemnew/show/P2919 1.搜索的时候分清楚全局变量和局部变量的区别 2.排序优化搜索 #include <cstdio ...

  3. bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场(DFS)

    1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 913  Solv ...

  4. 一道并查集的(坑)题:关闭农场closing the farm

    题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...

  5. BZOJ 1619 [Usaco2008 Nov]Guarding the Farm 保卫牧场:dfs【灌水】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1619 题意: 给你一个n*m的地形图,位置(x,y)的海拔为h[x][y]. 一个山顶的定 ...

  6. 什么是云渲染?【谈谈云渲染和传统渲染农场的区别】

    什么是云渲染?云渲染什么意思?云渲染和传统渲染农场的区别是什么?很多小伙伴有这样的疑问,今天,Renderbus瑞云渲染和大家谈一谈其中的含义. 云渲染是什么? 一.什么是云渲染,云渲染什么意思? 首 ...

  7. 用云渲染好还是自己搭建传统渲染农场好?

    今天云渲染小编就和大家说说云渲染以及它和传统渲染农场的区别.以及用云渲染好还是自己搭建传统渲染农场好? 一.什么是云渲染?云渲染什么意思? 首先云渲染是一种依托于云计算的云端服务,用户将本地文件提交到 ...

  8. 揭秘中国刷量“点击农场”:两人操纵上万台手机暴利赚钱

    本文讲的是揭秘中国刷量"点击农场":两人操纵上万台手机暴利赚钱,小伙伴们平时上微信.微博时都会看到很多点赞量特高的帖子,对于这种帖子我们一般都比较关注,但是"点击农场&q ...

  9. 云渲染是什么?云渲染和传统渲染农场有什么区别?

    云渲染是什么?云渲染和传统渲染农场有什么区别? 今天云渲染小编就来和大家说一说云渲染以及它和传统渲染农场的区别. 一.什么是云渲染?云渲染什么意思? 首先云渲染云渲染是一种依托于云计算的云端服务,用户 ...

最新文章

  1. 《程序是怎样跑起来的》第一章读后感
  2. vb.net2019- 调用 opencv
  3. python traceback class_traceback:让你更加灵活地处理python的异常
  4. 哥德巴赫猜想(升级版)(洛谷-P1579)
  5. 去除input填充颜色
  6. gentoo php,gentoo下的use原始配置让我安装php折腾好久。
  7. 洛谷P1087 FBI树
  8. 不愧是我,短短10分钟就为公司省下了几万块 ( ー̀◡ー́ )
  9. bgr to rgb
  10. matlab时域数据转频域,从时域和频域来解析傅里叶变换(含代码和性质)
  11. 天马行空脚踏实地,阿里巴巴有群百里挑一的天才应届生...
  12. java.util.list 报错_cannot be cast to java.util.List
  13. 房屋托管网络管理系统_学习管理系统和共享托管
  14. 一个Python的迷宫小游戏
  15. 梯形图调用c语言编写程序,简单使用C语言写梯形图精简V2.0.pdf
  16. 一个用户只能登录一次
  17. 注意力机制的直观理解
  18. js网站服务器时间,js获取本地时间、服务器时间、记时
  19. 【PMSM】二. 经典电流环、速度环设计(下)
  20. 滚动穿透及IOS惯性滚动究极解决方案

热门文章

  1. 计算机软件包括程序和指令吗,命令和指令的区别(计算机指令和程序的概念)
  2. JAVA计算机毕业设计校园绿化管理系统Mybatis+源码+数据库+lw文档+系统+调试部署
  3. 【目标检测】(9) 改进PANet特征提取金字塔,附Tensorflow完整代码
  4. Python BeautifulSoup中文乱码问题
  5. 如何一键抠图?手把手教你抠图
  6. mac 10.15 系统解决,系统打不开程序app,提示已损坏问题
  7. 华为存储Call Home CA证书到期解决方案
  8. mysql 查询表 所有字段
  9. node项目部署以及搭建nginx服务器
  10. navicat 新建数据库