http://poj.org/problem?id=1979
Method is exactly the same as lake counting.
1#include <iostream>2#include <stdio.h>3 4 using namespacestd;5 //The idea is exactly the same as lake counting .6 Const intMaxSize = -;7 intm,n,cnt;8 CharRoom[maxsize][maxsize];9 intd[2][4] = {{-1,0,1,0}, {0,1,0, -1}};Ten One BOOLCheckintXinty) A { - if(X <0|| X >= N | | Y <0|| Y >= M)return false; - if(Room[x][y] = ='#')return false; the return true; - } - - voidDfsintXinty) + { - intNX, NY; +Room[x][y] ='#';//set this to # A for(inti =0; I <4; i++) at { -NX = x + d[0][i]; -NY = y + d[1][i]; - if(Check (NX, NY)) - { -cnt++; in DFS (NX, NY); - } to } + } - the intMain () * { $ intx, y;Panax NotoginsengFreopen ("In.txt","R", stdin); - while(~SCANF ("%d%d", &m, &N)) the { + if(M = =0&& N = =0) Break; A for(inti =0; i < N; i++) the { + GetChar (); - for(intj =0; J < M; J + +) $ { $scanf"%c", &room[i][j]); - if(Room[i][j] = ='@') - { thex =i; -y =J;Wuyi } the } - } WuCNT =1; - dfs (x, y); Aboutprintf"%d\n", CNT); $ } - return 0; -}
POJ 1979 Red and Black