3450. 【NOIP2013模拟联考3】山峰(summits) (Standard IO)

来源:互联网 发布:贵州浪人网络投诉 编辑:程序博客网 时间:2024/05/29 06:35

Description

作为地质学家的JIH,为了绘制地图进行了野外考察。考察结束,他得到了一张n*m的地面高度地图。为了科学研究,JIH定义了一种山峰叫做d-山峰。一个高度为h地点被称作d-山峰,只有满足从这里出发,在不经过小于等于h-d的地点的前提下无法达到比它更高的地方。JIH正纠结于怎么分礼物,标出d-山峰的任务就交给你了。

Input

第一行n,m,d第二行开始,有一个n*m的矩阵表示地图,用空格隔开。

Output

输出d-山峰的个数。

Sample Input

6 10 20 0 0 0 0 0 0 0 0 00 1 2 1 1 1 1 0 1 00 2 1 2 1 3 1 0 0 00 1 2 1 3 3 1 1 0 00 2 1 2 1 1 1 0 2 00 0 0 0 0 0 0 0 0 0

Sample Output

4

Data Constraint

30% n,m<=10100% n,m<=500

###思路

一开始只想搜索要点分,后来优化一下就过了。对于每个点dfs一次,判断是否为山峰,而对于走过的点如果清空就会很耗时间,所以第一次dfs时走过的点记为1,第二次为2,以此类推就不用清空数组了。
var  a,b:array[0..600,0..600] of longint;  n,m,d,x,h:longint;  f:boolean;procedure dfs(y,z,s:longint);begin  b[y,z]:=s;  if (y<=0)or(z<=0)or(y>n)or(z>m) then exit;  if a[y,z]>h then f:=false;  if not f then exit;  if (a[y,z-1]>h-d)and(b[y,z-1]<>s)and(f) then    dfs(y,z-1,s);  if (a[y,z+1]>h-d)and(b[y,z+1]<>s)and(f) then    dfs(y,z+1,s);  if (a[y-1,z]>h-d)and(b[y-1,z]<>s)and(f) then    dfs(y-1,z,s);  if (a[y+1,z]>h-d)and(b[y+1,z]<>s)and(f) then    dfs(y+1,z,s);end;var  i,j,ans:longint;begin  readln(n,m,d);  for i:=1 to n do    for j:=1 to m do      read(a[i,j]);  x:=0;  ans:=0;  for i:=1 to n do    begin      for j:=1 to m do        begin          inc(x);          h:=a[i,j];          f:=true;          dfs(i,j,x);          if f then inc(ans);        end;    end;  writeln(ans);end.