AcWing 1113.红与黑

算法思路

常规的洪泛算法解决的问题,只需要从当前点开始扩展,对所有可以访问到的点进行计数即可

算法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstring>
#include <queue>

#define PII pair<int, int>

#define x first
#define y second

using namespace std;

const int N = 21;

int n, m;
char g[N][N];
bool st[N][N];
PII start;

int bfs()
{
queue<PII> q;
int cnt = 0;

q.push(start);

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

memset(st, false, sizeof st);
while(!q.empty())
{
auto t = q.front();
q.pop();

if (st[t.x][t.y]) continue;
st[t.x][t.y] = true;
cnt ++;

for (int i = 0; i < 4; ++i)
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '#') continue;
q.push({x, y});
}
}

return cnt;
}

int main()
{
cin >> m >> n;
while(n != 0 && m != 0)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> g[i][j];
if (g[i][j] == '@')
{
start.x = i;
start.y = j;
g[i][j] = '.';
}
}
}

cout << bfs() << '\n';
cin >> m >> n;
}
}

AcWing 1113.红与黑
http://anyin233.github.io/2022/01/27/AcWing-1113-红与黑/
Author
anyin233
Posted on
January 27, 2022
Licensed under