AcWing 1113. 红与黑
原题链接
简单
作者:
Meteor_7
,
2021-04-08 13:30:14
,
所有人可见
,
阅读 303
#include<iostream>
using namespace std;
const int N = 30;
int n , m;
char g[N][N];
int dx[] = {1, 0, -1, 0},dy[] = {0, 1, 0, -1};
int bfs(int x, int y)
{
int res = 1;
g[x][y] = '#';
for(int i = 0;i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if(a >= 0 && b >= 0 && a < n && b < m && g[a][b] == '.')
res += bfs(a,b);
}
return res;
}
int main()
{
while(cin >> m >> n , n || m)
{
for(int i = 0; i < n; i++)cin>>g[i];
int x, y;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
if(g[i][j]=='@')
{
x = i;
y = j;
}
}
cout<<bfs(x, y)<<endl;
}
}