-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20a-race-condition.cpp
More file actions
68 lines (64 loc) · 1.6 KB
/
20a-race-condition.cpp
File metadata and controls
68 lines (64 loc) · 1.6 KB
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef _DEBUG
#include "utils/debug.h"
#else
#define debug(...) 0
#endif
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
cin.tie(0)->sync_with_stdio(0);
string line;
ll ans = 0;
vector<string> g;
while (getline(cin, line)) {
g.push_back(line);
}
int N = g.size(), M = g[0].size();
auto in = [&](int i, int j) { return i >= 0 && j >= 0 && i < N && j < M; };
int sx, sy, tx, ty;
for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) {
if (g[i][j] == 'S') sx = i, sy = j;
if (g[i][j] == 'E') tx = i, ty = j;
}
auto go = [&](int srcx, int srcy) -> vector<vector<int>> {
vector<vector<int>> dist(N, vector<int>(M, 1e8));
struct Item { int x, y; };
queue<Item> q;
dist[srcx][srcy] = 0;
q.push({srcx, srcy});
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int dir = 0; dir < 4; ++dir) {
int nx = x + dx[dir], ny = y + dy[dir];
if (!in(nx, ny)) continue;
int nd = dist[x][y] + 1;
if (nd < dist[nx][ny]) {
dist[nx][ny] = nd;
if (g[nx][ny] != '#') q.push({nx, ny});
}
}
}
return dist;
};
auto sd = go(sx, sy);
auto td = go(tx, ty);
auto base = sd[tx][ty];
map<int, int> cnt;
for (int i = 1; i < N-1; ++i) {
for (int j = 1; j < M-1; ++j) {
for (int dir = 0; dir < 4; ++dir) {
int i2 = i + dx[dir], j2 = j + dy[dir];
if (!in(i2, j2)) continue;
if (g[i2][j2] == '#') continue;
auto cur = sd[i][j] + td[i2][j2] + 1;
if (cur > base) continue;
cnt[base - cur]++;
if (cur <= base - 100) ans++;
}
}
}
cout << ans << '\n';
}