-
Notifications
You must be signed in to change notification settings - Fork 15
/
go-to-considered-helpful.cpp
154 lines (146 loc) · 5.11 KB
/
go-to-considered-helpful.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2019 kamyu. All rights reserved.
/*
* Google Code Jam 2019 World Finals - Problem F. Go To Considered Helpful
* https://codingcompetitions.withgoogle.com/codejam/round/0000000000051708/000000000016c934
*
* Time: O(N^4), N is max(R, C)
* Space: O(N^2)
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <utility>
#include <tuple>
#include <algorithm>
#include <cassert>
using std::ios_base;
using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::to_string;
using std::queue;
using std::function;
using std::pair;
using std::make_pair;
using std::tie;
using std::max;
using std::min;
const int MAX_R = 100;
const int MAX_C = 100;
const int INF = MAX_R * MAX_C;
// Time: O(N^2)
vector<vector<int>> inline bfs(const vector<vector<char>>& A,
const int r, int c,
const function<bool(int, int)>& check_fn) {
const auto& R = A.size(), &C = A[0].size();
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
vector<vector<int>> dist(R, vector<int>(C, INF));
dist[r][c] = 0;
queue<pair<int, int>> q({{r, c}});
while (!q.empty()) {
int r, c;
tie(r, c) = q.front(); q.pop();
for (const auto& kvp : directions) {
const auto& nr = r + kvp.first, &nc = c + kvp.second;
if (0 <= nr && nr < R && 0 <= nc && nc < C &&
dist[nr][nc] == INF && check_fn(nr, nc)) {
dist[nr][nc] = dist[r][c] + 1;
q.emplace(nr, nc);
}
}
}
return dist;
}
bool inline check(const vector<vector<char>>& A, int r, int c) {
const auto& R = A.size(), &C = A[0].size();
return 0 <= r && r < R &&
0 <= c && c < C &&
A[r][c] != '#';
}
string go_to_considered_helpful() {
int R, C;
cin >> R >> C;
vector<vector<char>> A(R, vector<char>(C));
pair<int, int> M, N;
for (int r = 0; r < R; ++r) {
for (int c = 0; c < C; ++c) {
cin >> A[r][c];
if (A[r][c] == 'M') {
M = make_pair(r, c);
} else if (A[r][c] == 'N') {
N = make_pair(r, c);
}
}
}
const auto& P = bfs(A, M.first, M.second,
[&A](int r, int c) { return A[r][c] != '#'; });
int result = P[N.first][N.second];
int cnt = 0;
for (int dr = -R + 1; dr < R; ++dr) { // enumerate (dr, dc)
for (int dc = -C + 1; dc < C; ++dc) {
if ((dr == 0 && dc == 0) ||
!check(A, N.first - dr, N.second - dc)) {
continue;
}
vector<vector<vector<bool>>> is_valid(2,
vector<vector<bool>>(R, vector<bool>(C)));
for (int r = 0; r < R; ++r) {
for (int c = 0; c < C; ++c) {
is_valid[0][r][c] = check(A, r, c);
}
}
for (int k = 1;
check(A, N.first - dr * k, N.second - dc * k);
++k) { // enumerate k
// the number of (dr, dc, k) combinations is
// at most sum(N / max(abs(dr), abs(dc)))
// for each (dr, dc) = O(N^2)
assert(++cnt <= 2 * max(R, C) * max(R, C));
auto& is_valid_for_all_k_loops = is_valid[k % 2];
auto& is_valid_for_all_k_minus_1_loops = is_valid[(k - 1) % 2];
for (int r = 0; r < R; ++r) {
for (int c = 0; c < C; ++c) {
is_valid_for_all_k_loops[r][c] =
is_valid_for_all_k_minus_1_loops[r][c] &&
check(A, r - dr * k, c - dc * k);
}
}
const auto& Q1 = bfs(A, N.first, N.second,
[&is_valid_for_all_k_loops](int r, int c) {
return is_valid_for_all_k_loops[r][c];
});
const auto& Q2 = bfs(A, N.first - dr, N.second - dc,
[&is_valid_for_all_k_minus_1_loops](int r, int c) {
return is_valid_for_all_k_minus_1_loops[r][c];
});
for (int r = 0; r < R; ++r) { // enumerate all possible cells B
for (int c = 0; c < C; ++c) {
if (!check(A, r - dr * k, c - dc * k)) {
continue;
}
// instructions:
// M ---P---> B ---Q1---> N ---Q2---> Goto B
result = min(result,
P[r - dr * k][c - dc * k] +
Q1[r][c] + Q2[r][c] + 1);
}
}
}
}
}
return result == INF ? "IMPOSSIBLE" : to_string(result);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
for (int test = 1; test <= T; ++test) {
cout << "Case #" << test << ": " << go_to_considered_helpful() << '\n';
}
return 0;
}