AcWing 4481. 方格探索

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2002;
int n, m, r, c, x, y, dist[N][N], ans, ds[] = {1, 0, -1, 0, 1};
char s[N][N];
deque<pair<int, int>> q;
int main() {
scanf("%d%d%d%d%d%d", &n, &m, &r, &c, &x, &y);
for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
memset(dist, 0x3f, sizeof(dist));
dist[r][c] = 0;
q.emplace_back(r, c);
while (q.size()) {
auto [r, c] = q.front(); q.pop_front();
for (int i = 0; i < 4; ++i) {
int nr = r + ds[i], nc = c + ds[i + 1];
if (nr <= 0 || nr > n || nc <= 0 || nc > m || s[nr][nc] == '*') continue;
int w = 0;
if (i == 3) w = 1; // when r + 0 and c + 1.
if (dist[nr][nc] > dist[r][c] + w) {
dist[nr][nc] = dist[r][c] + w;
if (w) q.emplace_back(nr, nc);
else q.emplace_front(nr, nc);
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
int b = dist[i][j];
if (b <= y && b + c - j <= x)
++ans;
}
printf("%d\n", ans);
return 0;
}