标签:
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 6 13
#include<cstdio>
#include<cstring>
#include<iostream>//
using namespace std;//C++要加这两个头文件
char map[22][22];
int bx[5]={0,1,0,-1};
int by[5]={1,0,-1,0};
int sum;
int n,m;
bool judge(int a,int b)
{
if(a<1||a>m||b<1||b>n)
return false;
else
{
if(map[a][b]!='#')
return true;
else
return false;
}
}
void dfs(int a,int b)
{
int i;
int nowa,nowb;
sum++;
map[a][b]='#';
for(i=0;i<4;i++)
{//对定点进行上下左右四种变化
nowa=a+bx[i];
nowb=b+by[i];
if(judge(nowa,nowb))
dfs(nowa,nowb);
//直接递归调用就好
else
continue;
}
}
int main()
{
//int n,m;
int i,j,k;
int stax,stay;
while(scanf("%d%d",&n,&m),n+m)
{
memset(map,0,sizeof(map));
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
stax=i;
stay=j;
}
}
sum=0;
dfs(stax,stay);
cout<<sum<<endl;
//若输出需要换行,则加上<<endl
}
return 0;
}标签:
原文地址:http://blog.csdn.net/wangluoershixiong/article/details/44900357