poj 2386 Lake Counting(dfs)

来源:互联网 发布:网页游戏自动挂机软件 编辑:程序博客网 时间:2024/05/23 21:16

基础dfs

#include <cstdio>int n,m;char Map[110][110];int next[8][2] = {{0,1},{1,0},{1,1},{-1,-1},{0,-1},{-1,0},{1,-1},{-1,1}};void DFS(int x, int y){    int tx,ty;    for(int k = 0; k < 8; ++k)    {        tx = x + next[k][0];        ty = y + next[k][1];        if(tx < 0 || ty < 0 || tx >= n || ty >= m)            continue;        if(Map[tx][ty] == 'W')        {            Map[tx][ty] = '.';            DFS(tx,ty);        }    }    return;}int main(){    while(scanf("%d %d",&n,&m) != EOF)    {        for(int i = 0; i < n; ++i)            for(int j = 0; j < m; ++j)                scanf(" %c",&Map[i][j]);        int res = 0;        for(int i = 0; i < n; ++i)            for(int j = 0; j < m; ++j)            {                if(Map[i][j] == 'W')                {                    Map[i][j] = '.';                    DFS(i,j);                    ++res;                }            }        printf("%d\n",res);    }    return 0;}
0 0
原创粉丝点击