AcWing 1113. 红与黑
原题链接
简单
作者:
acwing_陌路
,
2021-03-01 15:11:20
,
所有人可见
,
阅读 218
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 25;
char g[N][N];
bool st[N][N];
int n,m;
int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};
int dfs(int x,int y)
{
int cnt = 1;
st[x][y] = true;
for(int i = 0;i < 4;i++)
{
int a = x + dx[i],b = y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(g[a][b] != '.') continue;
if(st[a][b] == true) continue;
cnt += dfs(a,b);
}
return cnt;
}
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;
}
}
}
memset(st,0,sizeof st);
cout << dfs(x,y) << endl;
}
return 0;
}