1. 题目源地址:http://acm.hdu.edu.cn/showproblem.php?pid=1312
2. 源代码:
#include<iostream>
#include<memory.h>
#include<stdio.h>
using namespace std;
char map[25][25];
int visited[25][25];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int W,H,steps;
int start_x,start_y;
bool InMap(int x,int y)
{
if(x>=1 && x<=H && y>=1 && y<=W)
return true;
else
return false;
}
void DFS(int x,int y)
{
int i,j;
int next_x,next_y;
visited[x][y]=1;
for(i=0;i<4;i++)
{
next_x=x+dx[i];
next_y=y+dy[i];
if(!visited[next_x][next_y] && InMap(next_x,next_y) && map[next_x][next_y]!='#')
{
visited[next_x][next_y]=1;
steps++;
DFS(next_x,next_y);
}
}
}
int main()
{
int i,j;
while(cin>>W>>H && (W||H))
{
memset(visited,0,sizeof(visited));
getchar();
for(i=1;i<=H;i++)
for(j=1;j<=W;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
start_x=i;
start_y=j;
}
}
steps=1;
DFS(start_x,start_y);
cout<<steps<<endl;
}
return 0;
}