【usaco 2013 feb Bronze】计算周长

来源:互联网 发布:冰河木马软件下载 编辑:程序博客网 时间:2024/05/29 07:20

题目描述

题目:

     农夫约翰在他的农田上了放置了N个干草堆,如果我们考虑农田是100*100的方格,每个干草堆占一个小方格(没有两个干草堆占据同一个小方格)。

     约翰发现他的所有干草堆组成了一个连通分量,即从任意一个干草堆出发,都可以通过若干次向上或向下或向左或向右的移动到相邻的有干草堆的小方格而达到任意一个其他的干草堆。这里,干草堆的堆放可能会出现“洞”,“洞”是一块空地,但是都被干草堆所包围。

问题描述:

     请帮助约翰计算所有被干草堆占领的小方格所组成的图形的周长。注意,“洞”是不计入周长的范围内的。

沿着边搜索。

const  maxn=1000;  dx:array[1..4] of longint=(-1,0,1,0);  dy:array[1..4] of longint=(0,1,0,-1);var  a,f:array[-10..maxn+2,-10..maxn+2] of boolean;  maxx,maxy,ans,i,j,n,x,y:longint;procedure dfs(x,y:longint);var  i:longint;begin  if (x>=102) or (x<=-1) or (y>=102) or (y<=-1)then exit;  if a[x+1,y] then inc(ans);  if a[x,y+1] then inc(ans);  if a[x-1,y] then inc(ans);  if a[x,y-1] then inc(ans);  for i:=1 to 4 do    if (not f[x+dx[i],y+dy[i]]) and (not a[x+dx[i],y+dy[i]])      then begin             f[x+dx[i],y+dy[i]]:=true;             dfs(x+dx[i],y+dy[i]);           end;end;begin  assign(input,'Perimeter.in'); reset(input);  assign(output,'Perimeter.out');rewrite(output);  readln(n);  for i:=1 to n do    begin      readln(x,y);      a[x,y]:=true;    end;  dfs(0,0);  writeln(ans);  close(input);close(output);end.

0 0