【usaco 2013 feb Bronze】计算周长

来源:互联网 发布:爱淘宝怎么注册账号 编辑:程序博客网 时间:2024/05/29 09:36
题目:

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

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

 

问题描述:

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

题解:

  BFS。

代码:

var  f:array[1..4,1..2] of longint=((0,1),(0,-1),(1,0),(-1,0));  a:array[-1..150,-1..150] of longint;  n,ans:longint;procedure main(x,y:longint);var  i:longint;  c:boolean;begin  if (x>101)or(x<0)or(y>101)or(y<0) then exit;  for i:=1 to 4 do    if a[x+f[i,1],y+f[i,2]]=1 then inc(ans);  a[x,y]:=-10;  for i:=1 to 4 do    if a[x+f[i,1],y+f[i,2]]=0 then      main(x+f[i,1],y+f[i,2])end;var  i,x,y:longint;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]:=1;    end;  main(1,1);  writeln(ans);  close(input);close(output);end.

3 0