【POJ2386】Lake Counting

problem

solution

codes

//DFS 求通块
#include<iostream>
#include<string>
using namespace std;

int n, m, ans;
string a[100];

void dfs(int x, int y){
    for(int i = -1; i <= 1; i++)
        for(int j = -1; j <= 1; j++)
            if(x+i>=0&&x+i<n&&y+j>=0&&y+j<m && a[x+i][y+j]=='W')
                { a[x+i][y+j]='.'; dfs(x+i, y+j);}
}

int main(){
    cin>>n>>m;
    cin.get();
    for(int i = 0; i < n; i++)getline(cin, a[i]);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            if(a[i][j] == 'W'){ dfs(i, j); ans++;}
    cout<<ans<<"
";
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444803.html