// IOI 2021
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
////////////////////////////////////////////////////////////////////
const int N = 1e3 + 2;
bool A[N][N];
int W[N][N], D[N][N];
pii X[4][N][N];
int DX[] = {+1, -1, 0, 0};
int DY[] = {0, 0, +1, -1};
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
mt19937 Rnd(time(0));
memset(W, 63, sizeof W), memset(D, 63, sizeof D);
int n, m; cin >> n >> m;
pii src, dest;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
char c; cin >> c;
A[i][j] = (c != '#');
if (c == 'S') src = make_pair(i, j);
if (c == 'C') dest = make_pair(i, j);
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j]) {
X[0][i][j] = (A[i][j - 1] ? X[0][i][j - 1] : make_pair(i, j));
X[1][i][j] = (A[i - 1][j] ? X[1][i - 1][j] : make_pair(i, j));
}
for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) if (A[i][j]) {
X[2][i][j] = (A[i][j + 1] ? X[2][i][j + 1] : make_pair(i, j));
X[3][i][j] = (A[i + 1][j] ? X[3][i + 1][j] : make_pair(i, j));
}
queue<pii> Q;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j] && !(A[i - 1][j] && A[i + 1][j] && A[i][j - 1] && A[i][j + 1])) W[i][j] = 0, Q.push(make_pair(i, j));
while (!Q.empty()) {
pii p = Q.front(); Q.pop();
for (int i = 0; i < 4; i++) {
pii q = make_pair(p.first + DX[i], p.second + DY[i]);
if (A[q.first][q.second] && W[p.first][p.second] + 1 < W[q.first][q.second])
W[q.first][q.second] = W[p.first][p.second] + 1, Q.push(q);
}
}
set< pair<int, pii> > S;
D[src.first][src.second] = 0;
S.insert(make_pair(0, src));
while (!S.empty()) {
pii p = S.begin()->second; S.erase(S.begin());
for (int i = 0; i < 4; i++) {
pii q = make_pair(p.first + DX[i], p.second + DY[i]);
if (A[q.first][q.second] && D[p.first][p.second] + 1 < D[q.first][q.second])
S.erase(make_pair(D[q.first][q.second], q)), D[q.first][q.second] = D[p.first][p.second] + 1, S.insert(make_pair(D[q.first][q.second], q));
q = X[i][p.first][p.second];
if (A[q.first][q.second] && D[p.first][p.second] + W[p.first][p.second] + 1 < D[q.first][q.second])
S.erase(make_pair(D[q.first][q.second], q)), D[q.first][q.second] = D[p.first][p.second] + W[p.first][p.second] + 1, S.insert(make_pair(D[q.first][q.second], q));
}
}
cout << D[dest.first][dest.second] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
8 ms |
8320 KB |
Output is correct |
3 |
Correct |
9 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8320 KB |
Output is correct |
5 |
Correct |
9 ms |
8320 KB |
Output is correct |
6 |
Correct |
9 ms |
8448 KB |
Output is correct |
7 |
Correct |
8 ms |
8320 KB |
Output is correct |
8 |
Correct |
9 ms |
8448 KB |
Output is correct |
9 |
Correct |
8 ms |
8320 KB |
Output is correct |
10 |
Correct |
8 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8448 KB |
Output is correct |
3 |
Correct |
9 ms |
8448 KB |
Output is correct |
4 |
Correct |
8 ms |
8320 KB |
Output is correct |
5 |
Correct |
8 ms |
8320 KB |
Output is correct |
6 |
Correct |
9 ms |
8448 KB |
Output is correct |
7 |
Correct |
8 ms |
8320 KB |
Output is correct |
8 |
Correct |
9 ms |
8448 KB |
Output is correct |
9 |
Correct |
10 ms |
9216 KB |
Output is correct |
10 |
Correct |
10 ms |
9216 KB |
Output is correct |
11 |
Correct |
9 ms |
9088 KB |
Output is correct |
12 |
Correct |
9 ms |
9088 KB |
Output is correct |
13 |
Correct |
10 ms |
9088 KB |
Output is correct |
14 |
Correct |
9 ms |
8320 KB |
Output is correct |
15 |
Correct |
9 ms |
9088 KB |
Output is correct |
16 |
Correct |
9 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
10 ms |
8448 KB |
Output is correct |
3 |
Correct |
9 ms |
8320 KB |
Output is correct |
4 |
Correct |
9 ms |
8320 KB |
Output is correct |
5 |
Correct |
17 ms |
13056 KB |
Output is correct |
6 |
Correct |
19 ms |
13056 KB |
Output is correct |
7 |
Correct |
19 ms |
13056 KB |
Output is correct |
8 |
Correct |
14 ms |
13088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
8 ms |
8320 KB |
Output is correct |
3 |
Correct |
10 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8320 KB |
Output is correct |
5 |
Correct |
8 ms |
8320 KB |
Output is correct |
6 |
Correct |
9 ms |
8320 KB |
Output is correct |
7 |
Correct |
9 ms |
8320 KB |
Output is correct |
8 |
Correct |
8 ms |
8320 KB |
Output is correct |
9 |
Correct |
10 ms |
9216 KB |
Output is correct |
10 |
Correct |
10 ms |
9088 KB |
Output is correct |
11 |
Correct |
10 ms |
9216 KB |
Output is correct |
12 |
Correct |
9 ms |
9088 KB |
Output is correct |
13 |
Correct |
9 ms |
9216 KB |
Output is correct |
14 |
Correct |
17 ms |
13056 KB |
Output is correct |
15 |
Correct |
19 ms |
13056 KB |
Output is correct |
16 |
Correct |
19 ms |
13056 KB |
Output is correct |
17 |
Correct |
19 ms |
13056 KB |
Output is correct |
18 |
Correct |
22 ms |
13184 KB |
Output is correct |
19 |
Correct |
21 ms |
12928 KB |
Output is correct |
20 |
Correct |
21 ms |
12928 KB |
Output is correct |
21 |
Correct |
17 ms |
13056 KB |
Output is correct |
22 |
Correct |
18 ms |
13056 KB |
Output is correct |
23 |
Correct |
19 ms |
13056 KB |
Output is correct |
24 |
Correct |
20 ms |
12928 KB |
Output is correct |
25 |
Correct |
9 ms |
8448 KB |
Output is correct |
26 |
Correct |
9 ms |
9088 KB |
Output is correct |
27 |
Correct |
8 ms |
8320 KB |
Output is correct |
28 |
Correct |
15 ms |
13056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8448 KB |
Output is correct |
3 |
Correct |
10 ms |
8448 KB |
Output is correct |
4 |
Correct |
9 ms |
8320 KB |
Output is correct |
5 |
Correct |
8 ms |
8448 KB |
Output is correct |
6 |
Correct |
9 ms |
8448 KB |
Output is correct |
7 |
Correct |
8 ms |
8320 KB |
Output is correct |
8 |
Correct |
9 ms |
8448 KB |
Output is correct |
9 |
Correct |
10 ms |
9216 KB |
Output is correct |
10 |
Correct |
9 ms |
9216 KB |
Output is correct |
11 |
Correct |
9 ms |
9088 KB |
Output is correct |
12 |
Correct |
9 ms |
9088 KB |
Output is correct |
13 |
Correct |
9 ms |
9088 KB |
Output is correct |
14 |
Correct |
17 ms |
13056 KB |
Output is correct |
15 |
Correct |
18 ms |
13056 KB |
Output is correct |
16 |
Correct |
20 ms |
13056 KB |
Output is correct |
17 |
Correct |
19 ms |
13056 KB |
Output is correct |
18 |
Correct |
22 ms |
13056 KB |
Output is correct |
19 |
Correct |
21 ms |
12928 KB |
Output is correct |
20 |
Correct |
22 ms |
12928 KB |
Output is correct |
21 |
Correct |
18 ms |
13056 KB |
Output is correct |
22 |
Correct |
20 ms |
13056 KB |
Output is correct |
23 |
Correct |
20 ms |
13056 KB |
Output is correct |
24 |
Correct |
268 ms |
45560 KB |
Output is correct |
25 |
Correct |
432 ms |
42360 KB |
Output is correct |
26 |
Correct |
317 ms |
41848 KB |
Output is correct |
27 |
Correct |
336 ms |
41848 KB |
Output is correct |
28 |
Correct |
186 ms |
46200 KB |
Output is correct |
29 |
Correct |
219 ms |
46328 KB |
Output is correct |
30 |
Correct |
255 ms |
46456 KB |
Output is correct |
31 |
Correct |
20 ms |
12928 KB |
Output is correct |
32 |
Correct |
310 ms |
41720 KB |
Output is correct |
33 |
Correct |
9 ms |
8320 KB |
Output is correct |
34 |
Correct |
9 ms |
9088 KB |
Output is correct |
35 |
Correct |
246 ms |
43644 KB |
Output is correct |
36 |
Correct |
9 ms |
8320 KB |
Output is correct |
37 |
Correct |
15 ms |
13056 KB |
Output is correct |
38 |
Correct |
128 ms |
46972 KB |
Output is correct |
39 |
Correct |
161 ms |
38008 KB |
Output is correct |