Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312

DFS不解释。。

#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#define N  50
using namespace std;
int n,m;
char map[N][N];
int sum;
void dfs(int x,int y)
{
	if(map[x+1][y]=='.') {sum++;map[x+1][y]='#';dfs(x+1,y);}
	if(map[x-1][y]=='.') {sum++;map[x-1][y]='#';dfs(x-1,y);}
	if(map[x][y-1]=='.') {sum++;map[x][y-1]='#';dfs(x,y-1);}
	if(map[x][y+1]=='.') {sum++;map[x][y+1]='#';dfs(x,y+1);}
}
int main()
{
	while(cin>>m>>n)
	{
		if(!n&&!m) break;
		memset(map,'\0',sizeof(map));
		sum=1;
		int a,b;
		for(int i=0;i<n;++i)
			{
				cin>>map[i];
			   for(int j=0;j<m;++j)
				if(map[i][j]=='@') a=i,b=j;
		    }
		map[a][b]='#';
		  dfs(a,b);
		  cout<<sum<<endl;
	}return 0;
}


 

你可能感兴趣的:(Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312)