問題: BOJ 16236 — 赤ちゃんザメ · 한국어 · English
サメが現在いる位置から、食べられる魚を探すたびに BFS を行います。サメより大きな魚がいるマスには入れません。空きマスとサメ以下の大きさの魚がいるマスには入れます。食べられる魚はサメより厳密に小さい魚です。つまり、同じ大きさの魚は通過できますが、食べることはできません。
BFS は距離の小さいマスから順に訪問します。食べられる魚を見つけたら、同じ距離にある残りのマスだけを確認すれば、最短距離の候補をすべて比較できます。その中から行番号が最小の魚を選び、行も同じなら列番号が最小の魚を選びます。これで距離、上、左の優先順位をそのまま適用できます。魚を食べたらそのマスを空にし、移動距離を経過時間に加えて、その位置から新しい BFS を開始します。訪問情報は探索ごとに初期化します。
サメは現在の大きさと同じ数の魚を食べると大きさが 1 増え、食べた数は 0 に戻ります。BFS で食べられる魚が見つからなければシミュレーションを終了します。1 回の BFS は時間 O(N^2)、空間 O(N^2) です。食べた魚の数を F とすると、全体の時間計算量は O(F N^2) です。
C++
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
73
74
75
76
77
78
79
80
81
82
83
84
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> grid(n, vector<int>(n));
int sharkRow = 0;
int sharkCol = 0;
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
cin >> grid[r][c];
if (grid[r][c] == 9) {
sharkRow = r;
sharkCol = c;
grid[r][c] = 0;
}
}
}
const int dr[] = {-1, 0, 0, 1};
const int dc[] = {0, -1, 1, 0};
int sharkSize = 2;
int eaten = 0;
int elapsed = 0;
while (true) {
vector<vector<int>> distance(n, vector<int>(n, -1));
queue<pair<int, int>> q;
q.push({sharkRow, sharkCol});
distance[sharkRow][sharkCol] = 0;
int preyRow = -1;
int preyCol = -1;
int preyDistance = -1;
while (!q.empty()) {
const auto [r, c] = q.front();
q.pop();
const int d = distance[r][c];
if (preyDistance != -1 && d > preyDistance) break;
if (grid[r][c] > 0 && grid[r][c] < sharkSize) {
if (preyRow == -1 || r < preyRow || (r == preyRow && c < preyCol)) {
preyRow = r;
preyCol = c;
preyDistance = d;
}
continue;
}
for (int direction = 0; direction < 4; ++direction) {
const int nr = r + dr[direction];
const int nc = c + dc[direction];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (distance[nr][nc] != -1 || grid[nr][nc] > sharkSize) continue;
distance[nr][nc] = d + 1;
q.push({nr, nc});
}
}
if (preyRow == -1) break;
elapsed += preyDistance;
sharkRow = preyRow;
sharkCol = preyCol;
grid[sharkRow][sharkCol] = 0;
++eaten;
if (eaten == sharkSize) {
++sharkSize;
eaten = 0;
}
}
cout << elapsed;
}