猴群(normal)
Time Limit:1000MS Memory Limit:65536K
Total Submit:101 Accepted:45
Description
输入一个矩阵(n*m),由0~9组成,其中数字0代表树,1~9代表猴子,凡是群居在一起的猴子称之为一群猴子。现在要你找出当中有多少只猴子。
Input
第一行两个整数n,m。(n<=1000;m<=1000)
接下来是一个n*m的矩阵。含义见题目描述。
Output
一个整数,表明有多少群猴子。
Sample Input
4 10
0234500067
1034560500
2045600671
0000000089
Sample Output
4
Source
Fstephen
-
var a:array[-1..10000,-1..10000]of boolean; i,j,n,m,tot:longint; procedure init; var i,j:longint; ch:char; s:string; begin readln(n,m); for i:=1 to n do begin for j:=1 to m do begin read(ch); if ord(ch)>ord('0') then a[i,j]:=true; end; readln; end; end; procedure ppp(x,y:longint); begin a[i,j]:=false; end; procedure pp(x,y:longint); var i,j:longint; begin a[x,y]:=false; if a[x+1,y] then pp(x+1,y); if a[x-1,y] then pp(x-1,y); if a[x,y+1] then pp(x,y+1); if a[x,y-1] then pp(x,y-1); end; begin init; for i:=1 to n do for j:=1 to m do begin if a[i,j] then begin pp(i,j); inc(tot);end; end; writeln(tot); end.