BFS
使用结构体存储坐标
keep数组判断此处黑点是否记录
lo数组是上下左右四个方位
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
};
#define check(x,y) (x>=0&&x<m&&y>=0&&y<n)
int lo[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
freopen("C:\\Users\\Lenovo\\Desktop\\input.txt","r",stdin);
int n,m;
node pos;
while(~scanf("%d%d",&n,&m)&&m&&n)
{
char ma[25][25];
int keep[25][25]={0};
for(int i=0;i<m;i++)
{
getchar();
for(int j=0;j<n;j++)
{
scanf("%c",&ma[i][j]);
if(ma[i][j]=='@')
{
pos.x=i,pos.y=j;
}
}
ma[i][n]=='\0';
}
queue<node>list;
list.push(pos);
int ans=0;
while(list.size())
{
int l=list.size();
for(int i=0;i<l;i++)
{
node temp=list.front(),index;
list.pop();
for(int j=0;j<4;j++)
{
int x=temp.x+lo[j][0];
int y=temp.y+lo[j][1];
if(check(x,y))
{
if(keep[x][y]==0&&ma[x][y]=='.')
{
ans++;
keep[x][y]=1;
index.x=x;index.y=y;
list.push(index);
}
}
}
}
}
printf("%d\n",ans+1);
}
return 0;
}