#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int n,m;
char a[100][100];
void dfs(int x,int y)
{
int tx,ty,i;
a[x][y]='$'; //将‘.’用‘$’代替,然后统计‘$’的个数
for(int i=0;i<4;i++)
{
tx=dx[i]+x;
ty=dy[i]+y;
if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]=='.')
{
dfs(tx,ty);
}
}
}
int main()
{
int i,j,count,x,y;
while(~scanf("%d%d%*c",&m,&n)&&m+n!=0)
{
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='@')
{
x=i;
y=j;
}
}
getchar();
}
dfs(x,y);
count=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(a[i][j]=='$')
count++;
printf("%d\n",count);
}
return 0;
}------------------------------------#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int N,M;
char Map[35][35];
int DFS(int x,int y)
{
int d = 0;
if(x < 0 || x >= N || y < 0 || y >= M || Map[x][y] == '#')
return 0; //返回,递归边界
if(Map[x][y] == '.' || Map[x][y] == '@')
{
d = 1; //标记,避免重复搜索
Map[x][y] = '#';
}
return DFS(x+1,y) + DFS(x,y+1) + DFS(x-1,y) + DFS(x,y-1) + d;
}
int main()
{
while(~scanf("%d%d",&M,&N) && (N||M))
{
int ans = 0;
for(int i = 0; i < N; ++i)
scanf("%s",Map[i]);
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
if(Map[i][j] == '@')
ans = DFS(i,j);
printf("%d\n",ans);
}
return 0;
}
暂时没有评论,来抢沙发吧~