题号:POJ 2386
题目:
Input
Output
Sample Input
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
Sample Output
3
Hint
#include<iostream>
#include<cstdio>
#define N 125
using namespace std;
char a[N][N];
int row,col;
void dfs(int x,int y)
{
a[x][y]=‘.‘;
for (int dx=-1;dx<=1;dx++)
for (int dy=-1;dy<=1;dy++)
{
int nx=x+dx,ny=y+dy;
if (nx>=0&&nx<=row&&ny>=0&&ny<=col&&a[nx][ny]==‘W‘)
dfs(nx,ny);
}
}
int main()
{
cin>> row>> col;
for (int i = 0; i <row; i++)
for (int j = 0; j <col; j++)
cin >>a[i][j];
int res = 0;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if (a[i][j] == ‘W‘)//从有积水的地方开始dfs
{
dfs(i, j);
res++;
}
cout << res << endl;
return 0;
}
原文:http://www.cnblogs.com/lisijie/p/7739146.html