Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 498 Solved: 223
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)。
* 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
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
3
三个山丘分别是:左上角的高度为4的方格,右上角的高度为1的方格,还有最后一行中高度为2的方格.
题解:为啥我又犯逗比了——明明输入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银组数据居然不卡常数我也是醉了。。。)
1 const tt:array[1..8,1..2] of longint=((0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1));
2 var
3 i,j,k,l,m,n:longint;
4 a,b:array[0..1000,0..1000] of longint;
5 c:array[0..1000000,1..2] of longint;
6 procedure swap(var x,y:longint);inline;
7 var z:longint;
8 begin
9 z:=x;x:=y;y:=z;
10 end;
11 procedure sort(l,r:longint);inline;
12 var i,j,x,y:longint;
13 begin
14 i:=l;j:=r;
15 x:=a[c[(i+j) div 2,1],c[(i+j) div 2,2]];
16 repeat
17 while a[c[i,1],c[i,2]]>x do inc(i);
18 while a[c[j,1],c[j,2]]<x do dec(j);
19 if i<=j then
20 begin
21 swap(c[i,1],c[j,1]);
22 swap(c[i,2],c[j,2]);
23 inc(i);
24 dec(j);
25 end;
26 until i>j;
27 if l<j then sort(l,j);
28 if i<r then sort(i,r);
29 end;
30 procedure kill(x,y:longint);inline;
31 var i:longint;
32 begin
33 if b[x,y]=1 then exit;
34 b[x,y]:=1;
35 for i:=1 to 8 do
36 if (a[x+tt[i,1],y+tt[i,2]]=a[x,y]) and (b[x+tt[i,1],y+tt[i,2]]=0) then kill(x+tt[i,1],y+tt[i,2]);
37 end;
38 function check(x,y:longint):boolean;inline;
39 var i:longint;
40 begin
41 for i:=1 to 8 do
42 begin
43 if a[x+tt[i,1],y+tt[i,2]]>a[x,y] then exit(false);
44 end;
45 exit(true);
46 end;
47
48 begin
49 readln(n,m);
50 fillchar(b,sizeof(b),0);
51 fillchar(a,sizeof(a),-1);
52 for i:=0 to n+1 do
53 begin
54 b[i,0]:=1;
55 b[i,m+1]:=1;
56 end;
57 for i:=0 to m+1 do
58 begin
59 b[0,i]:=1;
60 b[n+1,i]:=1;
61 end;
62 for i:=1 to n do
63 begin
64 for j:=1 to m do
65 read(a[i,j]);
66 readln;
67 end;
68 for i:=1 to n do
69 begin
70 for j:=1 to m do
71 begin
72 if b[i,j]=1 then continue;
73 if check(i,j)=false then kill(i,j);
74 end;
75 end;
76 for i:=1 to n do
77 for j:=1 to m do
78 begin
79 if b[i,j]=0 then
80 begin
81 inc(l);
82 kill(i,j);
83 end;
84 end;
85 writeln(l);
86 end.