lake counting(poj 2386)

#include <iostream>
#include<stdio.h>
#define MAX 1000
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int n,m;
char A[MAX][MAX];
void dfs(int x,int y);
int main(int argc, char** argv) {
	freopen("test.txt","r",stdin);
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
		    char c;
		    cin>>c;
		    A[i][j]=c;
		}
    }
    for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
		    cout<<A[i][j]<<" ";
		}
		cout<<endl;
    }
	int count=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(A[i][j]=='W')
			{
				dfs(i,j);
				count++;
			}
		}
	}
	cout<<count;
	return 0;
}
void dfs(int x,int y)
{
	A[x][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)//范围 
			{
				int a=x+i;
				int b=y+j;
				if(A[a][b]=='W')
				{
					dfs(x+i,y+j);
				}
			}
		}
	}
	return;
}

猜你喜欢

转载自blog.csdn.net/hj13547816754/article/details/79896299