链接:http://poj.org/problem?id=2386
DFS 的入门级题目....依次递归8个方向即可:
#include <iostream> //#include<cstdio> using namespace std; char data[105][105]; int n,m; void dfs(int x,int y) { int dx,dy; int nx,ny; data[x][y]='.'; for(dx=-1; dx<=1; dx++) for(dy=-1; dy<=1; dy++) { nx=x+dx;ny=y+dy; if(nx<n&&ny<m&&data[nx][ny]=='W'&&nx>=0&&ny>=0) dfs(nx,ny); } } int main() { int i,j; //freopen("1.txt","r",stdin); int ans; while(cin>>n>>m) { ans=0; for(i=0; i<n; i++) cin>>data[i]; for(i=0; i<n; i++) for(j=0; j<m; j++) { if(data[i][j]=='W') { dfs(i,j); ans++; } } cout<<ans<<endl; } return 0; }
天下武功,唯快不破