当前位置: 代码迷 >> 综合 >> 【2014.8.17NOIP普及组模拟】数池塘
  详细解决方案

【2014.8.17NOIP普及组模拟】数池塘

热度:82   发布时间:2023-10-09 12:35:38.0

题目描述

农夫约翰的农场可以表示成N*M(1<=N<=100,1<=M<=100)个方格组成的矩形。由于近日的降雨,在约翰农场上的不同地方形成了池塘。每一个方格或者有积水('W')或者没有积水('.')。农夫约翰打算数出他的农场上共形成了多少池塘。一个池塘是一系列相连的有积水的方格,每一个方格周围的八个方格都被认为是与这个方格相连的。

      现给出约翰农场的图样,要求输出农场上的池塘数。

题解

  把走过的标记和没水的一样,向八个方向找。

代码:

vara:array[0..120,0..120] of boolean;b:array[1..8,1..2] of longint=((0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1));n,m,ans:longint;s:string;
procedure seach(x,y:longint);
vari:longint;
beginif (x<1)or(y<1)or(x>n)or(y>m) then exit;a[x,y]:=false;for i:=1 to 8 doif a[x+b[i,1],y+b[i,2]] then seach(x+b[i,1],y+b[i,2]);
end;
vari,j:longint;
beginassign(input,'lkcount.in');reset(input);assign(output,'lkcount.out');rewrite(output);fillchar(a,sizeof(a),false);readln(n,m);for i:=1 to n dobeginreadln(s);for j:=1 to m doif s[j]='W' then a[i,j]:=trueelse a[i,j]:=false;end;for i:=1 to n dofor j:=1 to m doif a[i,j] then begin seach(i,j);inc(ans);end;writeln(ans);close(input);close(output);
end.


  相关解决方案