您的位置:首页 > 理论基础 > 计算机网络

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

2012-07-25 13:17 513 查看
DFS不解释。。

#include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#define N  50
using namespace std;
int n,m;
char map

;
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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: