AcWing 1113. 红与黑
原题链接
简单
作者:
wjie
,
2021-01-12 17:07:10
,
所有人可见
,
阅读 327
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 25;
char arr[N][N];
int n, m, res, st[N][N];
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
void dfs(int x, int y)
{
res ++;
st[x][y] = 1;
for (int i = 0; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && arr[nx][ny] != '#' && !st[nx][ny])
{
dfs(nx, ny);
}
}
}
int main()
{
while (scanf("%d%d", &m, &n) && n+m)
{
for (int i = 0; i < n; ++i) scanf("%s", arr[i]);
int x, y;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (arr[i][j] == '@')
{
x = i, y = j;
break;
}
}
}
memset(st, 0, sizeof st);
res = 0;
dfs(x, y);
printf("%d\n", res);
}
return 0;
}