poj2386 Lake Counting(DFS解法)
/********************** author: yomi date: 18.8.14 ps:入门题果然很简单, 我觉得比选数,选物品什么的简单多了呀。 只要控制好递归边界和递归式就没问题。 **********************/ #include <iostream> #include <queue> #include<cstdio> using namespace std; int n, m, ans = 0; const int maxn = 500; bool vis[maxn][maxn]; char mmap[maxn][maxn]; void dfs(int x, int y) { if(mmap[x][y] == '.') return; if(x<0 || x>=n || y<0 || y>=m || vis[x][y]) return; vis[x][y] = true; dfs(x-1, y-1); dfs(x-1, y); dfs(x-1, y+1); dfs(x, y-1); dfs(x, y+1); dfs(x+1, y-1); dfs(x+1, y); dfs(x+1, y+1); } int main() { cin >> n >> m; for(int i=0; i<n; i++){ scanf("%s", mmap[i]); } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ if(mmap[i][j] == 'W' && !vis[i][j]){ dfs(i, j); ans++; } } } cout << ans << endl; return 0; } /** 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W. **/