POJ 2386 Lake Counting(DFS)

题意几乎和杭电OJ上的合并石油那个题一样,但是我改哪个石油的代码交的时候没过,索性就自己又再写了一遍,简单搜索,可以说是比较简单了,附代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char map[110][110];
int n,m;
void dfs(int x,int y)
{
	map[x][y] = '.';
	for(int dx = -1;dx <= 1; dx++)
    {
        for(int dy = -1;dy <= 1; dy++)
        {
            int next_x = dx + x;
            int next_y = dy + y;
            if(next_x >= 0 && next_x < n && next_y >= 0 && next_y < m)
            {
            	if(map[next_x][next_y] == 'W')
            	{
            		dfs(next_x,next_y);
				}
			}
        }
    }
	return;
}
int  main()
{
	int ans = 0;
	scanf("%d %d",&n,&m);
	getchar();
	for(int i = 0;i < n; i++)
	{
		scanf("%s",map[i]);
		getchar();
	}
	for(int i = 0;i < n; i++)
	{
		for(int j = 0;j < m; j++)
		{
			if(map[i][j] == 'W')
			{
				dfs(i,j);
				ans++;
			}
		}
	}
	printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/xiao__hei__hei/article/details/84559916