DFS:Lake Counting(POJ 2386)

  好吧前几天一直没更新博客,主要是更新博客的确是要耗费一点精力

             

             北大教你数水坑

  最近更新博客可能就是一点旧的东西和一些水题,主要是最近对汇编感兴趣了嘻嘻嘻

  这一题挺简单的,没什么难度,简单深搜

#include <stdio.h>
#include <stdlib.h>

typedef int Postion;

static int Lake[100][100];
static int N, M;

void DFS(Postion, Postion);

int main(void)
{
    int i, j, pool = 0;
    while (~scanf("%d%d",&N,&M))
    {
        for (i = 0; i < N; i++)//读图
        {
            getchar();//别忘了现在是输入的是字符,一定要把回车键拿掉
            for (j = 0; j < M; j++)
                scanf("%c", &Lake[i][j]);
        }

        for (i = 0; i < N; i++)
            for (j = 0; j < M; j++)
                if (Lake[i][j] == 'W')
                {
                    DFS(i, j);
                    pool++;
                }
        printf("%d
", pool);
    }
}

void DFS(Postion x, Postion y)
{
    int i, j;
    Postion toi, toj;
    Lake[x][y] = '.';//搜索过的地方就置换成.

    for (i = -1; i <= 1; i++)
        for (j = -1; j <= 1; j++)
        {
            toi = x + i; toj = y + j;
            if (toi >= 0 && toi < N
                && toj >= 0 && toj < M//坐标均在图内
                && Lake[toi][toj] == 'W'//且没进入搜索过
                )
                DFS(toi, toj);
        }
}
原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/4784173.html