poj之旅——1979

思路:简单的深搜

参考程序(作者我从此改C++了):

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int direction[4][2]={
{-1,0},
{1,0},
{0,-1},
{0,1},
};
int step=0,W,H;
char room[20][20];
int dfs(const int& x,const int& y){
room[x][y]='#';
++step;
for (int i=0;i<4;++i){
int nx=x+direction[i][0];
int ny=y+direction[i][1];
if (nx>=0 && nx<W && ny>=0 && ny<H && room[nx][ny]=='.')
dfs(nx,ny);
}
return step;
}
int main(){
while (scanf("%d %d",&W,&H)==2 && W!=0) {
step=0;
int x,y;
for (y=0;y<H;++y) 
for (x=0;x<W;++x)
cin>>room[x][y];
bool found=false;
for (x=0;x<W;++x){
for (y=0;y<H;++y)
if (room[x][y]=='@') {
found=true;
break;
}
if (found) break;
}
printf("%d\n",dfs(x,y));
}
return 0;
}


你可能感兴趣的:(poj之旅——1979)