Description
作为地质学家的JIH,为了绘制地图进行了野外考察。考察结束,他得到了一张n*m的地面高度地图。为了科学研究,JIH定义了一种山峰叫做d-山峰。一个高度为h地点被称作d-山峰,只有满足从这里出发,在不经过小于等于h-d的地点的前提下无法达到比它更高的地方。JIH正纠结于怎么分礼物,标出d-山峰的任务就交给你了。
Input
第一行n,m,d
第二行开始,有一个n*m的矩阵表示地图,用空格隔开。
Output
输出d-山峰的个数。
Sample Input
6 10 2
0 0 0 0 0 0 0 0 0 0
0 1 2 1 1 1 1 0 1 0
0 2 1 2 1 3 1 0 0 0
0 1 2 1 3 3 1 1 0 0
0 2 1 2 1 1 1 0 2 0
0 0 0 0 0 0 0 0 0 0
Sample Output
4
Data Constraint
30% n,m<=10
100% n,m<=500
Hint
分析:bfs和dfs直接暴力就好了,就是枚举每个点,每个点再枚举路径的那种暴力。
不要每次bfs都清数组。暴力专题吗?????
代码:
type
node=record
x,y:longint;
end;
const
maxn=501;
dx:array [1..4] of longint=(0,1,0,-1);
dy:array [1..4] of longint=(-1,0,1,0);
var
b:array [1..maxn,1..maxn] of boolean;
v:array [1..maxn,1..maxn] of longint;
f,h:array [1..maxn,1..maxn] of longint;
list:array [1..maxn*maxn*2] of node;
n,m,i,j,d,ans,max,min,q:longint;
procedure bfs(xx,yy:longint);
var head,tail,i,x,y,t:longint;
flag:boolean;
begin
inc(q);
flag:=true;
head:=0;
tail:=1;
t:=1;
list[tail].x:=xx;
list[tail].y:=yy;
v[xx,yy]:=q;
repeat
inc(head);
for i:=1 to 4 do
begin
x:=list[head].x+dx[i];
y:=list[head].y+dy[i];
if (x>=1) and (x<=n) and (y>=1) and (y<=m) then
if (v[x,y]<q) and (h[x,y]>h[xx,yy]-d) then
begin
inc(tail);
list[tail].x:=x;
list[tail].y:=y;
if h[x,y]>h[xx,yy] then begin flag:=false; break; end;
if h[x,y]<h[xx,yy] then b[xx,yy]:=true;
if h[x,y]=h[xx,yy] then
begin
b[x,y]:=true;
inc(t);
end;
v[x,y]:=q;
end;
if flag=false then break;
end;
until head=tail;
if flag then ans:=ans+t;
end;
begin
readln(n,m,d);
max:=0; min:=maxlongint;
for i:=1 to n do
for j:=1 to m do
begin
read(h[i,j]);
if h[i,j]<min then min:=h[i,j];
if h[i,j]>max then max:=h[i,j];
end;
for i:=1 to n do
for j:=1 to m do
begin
if h[i,j]=max then
begin
b[i,j]:=true;
inc(ans);
end;
if h[i,j]=min then b[i,j]:=true;
if b[i,j]=false then bfs(i,j);
end;
writeln(ans);
end.