AcWing 1113. 红与黑
原题链接
简单
作者:
wjie
,
2020-07-01 20:54:56
,
所有人可见
,
阅读 465
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int N = 25;
int n, m;
string a[N];
bool st[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct Node{
int x;
int y;
}start;
bool valid(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
}
int bfs()
{
memset(st, false, sizeof(st));
queue<Node> q;
q.push(start);
st[start.x][start.y] = true;
int res = 1;
while (!q.empty())
{
Node u = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int nx = u.x + dx[i], ny = u.y + dy[i];
if (valid(nx, ny) && a[nx][ny] != '#' && !st[nx][ny])
{
++res;
st[nx][ny] = true;
q.push({nx, ny});
}
}
}
return res;
}
int main()
{
while (scanf("%d %d", &m, &n) && n+m)
{
for (int i = 0; i < n; ++i)
{
cin >> a[i];
for (int j = 0; j < m; ++j)
{
if (a[i][j] == '@')
start = {i, j};
}
}
printf("%d\n", bfs());
}
return 0;
}