POJ 2386 Lake Counting(dfs)

来源:互联网 发布:校园暴力看待 知乎 编辑:程序博客网 时间:2024/05/16 02:46

Lake Counting

题目链接:

http://poj.org/problem?id=2386

解题思路:

搜索大水题。。。

AC代码:

#include <iostream>#include <cstdio>using namespace std;int n,m;char a[110][110];void dfs(int x,int y){    if(x<0 || x>=n || y<0 || y>=m|| a[x][y] == '.')        return;    a[x][y] = '.';    dfs(x-1,y);    dfs(x-1,y-1);    dfs(x,y-1);    dfs(x+1,y-1);    dfs(x+1,y);    dfs(x+1,y+1);    dfs(x,y+1);    dfs(x-1,y+1);}int main(){    while(~scanf("%d%d",&n,&m)){        int sum = 0;        for(int i = 0; i < n; i++)            scanf("%s",a[i]);        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++){                if(a[i][j] == 'W'){                    sum++;                    dfs(i,j);                }            }        printf("%d\n",sum);    }    return 0;}



0 0
原创粉丝点击