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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 498  Solved: 223
[Submit][Status]

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.

农夫JOHN的农夫上有很多小山丘,他想要在那里布置一些保镖(……)去保卫他的那些相当值钱的奶牛们。 他想知道如果在一座小山丘上布置一名保镖的话,他总共需要招聘多少名保镖。他现在手头有一个用数字矩阵来表示地形的地图。这个矩阵有N行(1 < N < = 100)和M列( 1 < M < = 70) 。矩阵中的每个元素都有一个值H_ij(0 < = H_ij < =10,000)来表示该地区的海拔高度。请你帮助他统计出地图上到底有多少个小山丘。 小山丘的定义是:若地图中一个元素所邻接的所有元素都比这个元素高度要小(或它邻接的是地图的边界),则该元素和其周围所有按照这样顺序排列的元素的集合称为一个小山丘。这里邻接的意义是:若一个元素与另一个横坐标纵坐标和它的横纵坐标相差不超过1,则称这两个元素邻接。 问题名称:guard 输入格式: 第一行:两个由空格隔开的整数N和M 第二行到第N+1行:第I+1行描述了地图上的第I行,有M个由空格隔开的整数:H_ij. 输入样例:(guard.in): 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 输出格式: 第一行:小山丘的个数 输出样例:(guard.out): 3 输出样例解释: 地图上有三个小山丘:每个小山丘的山峰位置分别在左上角(高度为4),右上角(高度为1)和底部(高度为2)。

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的方格.

Source

Silver

题解:为啥我又犯逗比了——明明输入N×M,结果处理时却给我按照N×N了啊啊啊啊啊,害得我光各种WA不说,还纠结了一整天尼玛(phile:无脑的孩纸啊*_*)。。。好啦,除了这个之外,这题还是比较基础的!!!先把所有周围8个点(注意,是8个点,不是4个)中包含比此点高的点统统干掉,然后就可以类比很经典的寻找细胞问题(DFS连通块个数)啦啦啦。。。还有一点,网上不知道那么多逗比(额。。至少我这么看)说应该把所有点从高到低排序然后一个个干,看样子挺好的,但本人实际测试的结果是——网上那样的C++标程VS我的Pascal朴素DFS——50×50时差不多,700×700时,随机0-100的高度,结果我的快1倍左右;700×700,随机0-5的高度,那个程序2s多才出来,我的呵呵呵呵照样0.3s。。。(USACO银组数据居然不卡常数我也是醉了。。。)

 const tt:array[..,..] of longint=((,),(,-),(,),(-,),(,),(,-),(-,),(-,-));
var
i,j,k,l,m,n:longint;
a,b:array[..,..] of longint;
c:array[..,..] of longint;
procedure swap(var x,y:longint);inline;
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);inline;
var i,j,x,y:longint;
begin
i:=l;j:=r;
x:=a[c[(i+j) div ,],c[(i+j) div ,]];
repeat
while a[c[i,],c[i,]]>x do inc(i);
while a[c[j,],c[j,]]<x do dec(j);
if i<=j then
begin
swap(c[i,],c[j,]);
swap(c[i,],c[j,]);
inc(i);
dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
procedure kill(x,y:longint);inline;
var i:longint;
begin
if b[x,y]= then exit;
b[x,y]:=;
for i:= to do
if (a[x+tt[i,],y+tt[i,]]=a[x,y]) and (b[x+tt[i,],y+tt[i,]]=) then kill(x+tt[i,],y+tt[i,]);
end;
function check(x,y:longint):boolean;inline;
var i:longint;
begin
for i:= to do
begin
if a[x+tt[i,],y+tt[i,]]>a[x,y] then exit(false);
end;
exit(true);
end; begin
readln(n,m);
fillchar(b,sizeof(b),);
fillchar(a,sizeof(a),-);
for i:= to n+ do
begin
b[i,]:=;
b[i,m+]:=;
end;
for i:= to m+ do
begin
b[,i]:=;
b[n+,i]:=;
end;
for i:= to n do
begin
for j:= to m do
read(a[i,j]);
readln;
end;
for i:= to n do
begin
for j:= to m do
begin
if b[i,j]= then continue;
if check(i,j)=false then kill(i,j);
end;
end;
for i:= to n do
for j:= to m do
begin
if b[i,j]= then
begin
inc(l);
kill(i,j);
end;
end;
writeln(l);
end.

1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场的更多相关文章

  1. BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 491  S ...

  2. 【BZOJ】1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1619 首先不得不说,,题目没看懂.... 原来就是找一个下降的联通块.... 排序后dfs标记即可. ...

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

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

  4. bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场【bfs】

    不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!! 是我看不懂人话还是翻译不说人话= = 把所有格子按值排个序,bfs扩展打标记即可 #includ ...

  5. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

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

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

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

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1619 给出一张图每个点的高度,在一个点上安排守卫可以监视周围所有不高于于当前点的点.也就是类似 ...

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

    P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...

  9. [Usaco2008 Nov]mixup2 混乱的奶牛 简单状压DP

    1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 685  Solved: 383[S ...

随机推荐

  1. 有关extdelete恢复测试

    客户意外rm掉了数据文件,导致数据库无法打开,由于没有完整的备份和归档,需要使用别的方法,而客户又关闭了数据库,导致无法使用文件描述符恢复,就要使用linux上别的方法了,现记录使用extundele ...

  2. vue原来可以这样上手

    今儿与一群友讨论vue相关问题让我思量极深,1.我们是否在争对性解决问题或者说是帮助别人:2.我们是否在炫耀自己的技能.以下是被戏剧化的对白: "群友":最近按照vue官网示例学习 ...

  3. Java NIO------基础理论之缓存区

    1.概述:NIO我的理解就是 New IO,是API1.4里提供的新的API,为所有的原始类型做缓存支持. NIO主要的核心组成部分: Buffer(缓存) Channels(通道) Selector ...

  4. 制作一个功能丰富的Android天气App

    简易天气是一个基于和风天气数据采用MD设计的Android天气App.目前的版本采用传统的MVC模式构建.通过丰富多彩的页面为用户提供日常所需的天气资讯. 项目说明 项目放在github上面 地址是: ...

  5. Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport

    Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...

  6. Wireshark网络抓包(一)——数据包、着色规则和提示

    一.数据包详细信息 Packet Details面板内容如下,主要用于分析封包的详细信息. 帧:物理层.链路层 包:网络层 段:传输层.应用层 1)Frame 物理层数据帧概况 2)Ethernet ...

  7. asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  8. java实现简单的单点登录_转

    摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...

  9. 理解volatile

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. spring事件驱动模型--观察者模式在spring中的应用

    spring中的事件驱动模型也叫作发布订阅模式,是观察者模式的一个典型的应用,关于观察者模式在之前的博文中总结过,http://www.cnblogs.com/fingerboy/p/5468994. ...