#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ti tuple<int, int, int>
using namespace std;
int r, c, dp[1010][1010], stx, sty, enx, eny, cx, cy, cdi, mn[1010][1010];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char arr[1010][1010];
pair<int, int> dir[4][1010][1010], cur;
priority_queue<ti, vector<ti>, greater<ti>> pq;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> r >> c;
for(int i=0; i<1010; i++) for(int j=0; j<1010; j++) arr[i][j] = '#';
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
cin >> arr[i][j];
if(arr[i][j] == 'S') {
stx = i;
sty = j;
}
if(arr[i][j] == 'C') {
enx = i;
eny = j;
}
dp[i][j] = 1e9;
}
}
for(int j=0; j<=c+1; j++) {
for(int i=0; i<=r+1; i++) {
if(arr[i][j] == '#') cur = {i+1, j};
dir[0][i][j] = cur;
}
}
for(int i=0; i<=r+1; i++) {
for(int j=0; j<=c+1; j++) {
if(arr[i][j] == '#') cur = {i, j+1};
dir[1][i][j] = cur;
}
}
for(int j=0; j<=c+1; j++) {
for(int i=r+1; i>=0; i--) {
if(arr[i][j] == '#') cur = {i-1, j};
dir[2][i][j] = cur;
}
}
for(int i=0; i<=r+1; i++) {
for(int j=c+1; j>=0; j--) {
if(arr[i][j] == '#') cur = {i, j-1};
dir[3][i][j] = cur;
}
}
for(int i=1; i<=r; i++) {
for(int j=1; j<=c; j++) {
mn[i][j] = 1e9;
for(int k=0; k<4; k++) {
mn[i][j] = min(mn[i][j], abs(dir[k][i][j].first-i) + abs(dir[k][i][j].second-j));
}
}
}
pq.push({0, stx, sty});
while(pq.size()) {
cdi = get<0>(pq.top());
cx = get<1>(pq.top());
cy = get<2>(pq.top());
pq.pop();
if(cx < 1 || cx > r || cy < 1 || cy > c || arr[cx][cy] == '#') continue;
dp[cx][cy] = cdi;
if(cx == enx && cy == eny) {
cout << dp[enx][eny];
return 0;
}
for(int i=0; i<4; i++) {
if(dp[cx+dx[i]][cy+dy[i]] > cdi+1) {
pq.push({cdi+1, cx+dx[i], cy+dy[i]});
dp[cx+dx[i]][cy+dy[i]] = cdi+1;
}
}
for(int i=0; i<4; i++) {
if(dp[dir[i][cx][cy].first][dir[i][cx][cy].second] > cdi+mn[cx][cy]+1) {
pq.push({cdi+mn[cx][cy]+1, dir[i][cx][cy].first, dir[i][cx][cy].second});
dp[dir[i][cx][cy].first][dir[i][cx][cy].second] = cdi+mn[cx][cy]+1;
}
}
}
cout << dp[enx][eny];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10704 KB |
Output is correct |
9 |
Correct |
3 ms |
19032 KB |
Output is correct |
10 |
Correct |
3 ms |
19032 KB |
Output is correct |
11 |
Correct |
3 ms |
18776 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
18780 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
7 ms |
23132 KB |
Output is correct |
6 |
Correct |
10 ms |
23132 KB |
Output is correct |
7 |
Correct |
8 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10784 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
3 ms |
18900 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
8 ms |
23136 KB |
Output is correct |
15 |
Correct |
8 ms |
23144 KB |
Output is correct |
16 |
Correct |
7 ms |
23132 KB |
Output is correct |
17 |
Correct |
8 ms |
23128 KB |
Output is correct |
18 |
Correct |
8 ms |
23132 KB |
Output is correct |
19 |
Correct |
4 ms |
23132 KB |
Output is correct |
20 |
Correct |
6 ms |
23132 KB |
Output is correct |
21 |
Correct |
7 ms |
23132 KB |
Output is correct |
22 |
Correct |
8 ms |
23132 KB |
Output is correct |
23 |
Correct |
8 ms |
23132 KB |
Output is correct |
24 |
Correct |
7 ms |
23132 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
18776 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
4 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10768 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
19036 KB |
Output is correct |
11 |
Correct |
3 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
19036 KB |
Output is correct |
14 |
Correct |
9 ms |
23140 KB |
Output is correct |
15 |
Correct |
9 ms |
23132 KB |
Output is correct |
16 |
Correct |
7 ms |
23132 KB |
Output is correct |
17 |
Correct |
7 ms |
23132 KB |
Output is correct |
18 |
Correct |
8 ms |
23132 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
7 ms |
23136 KB |
Output is correct |
21 |
Correct |
8 ms |
23132 KB |
Output is correct |
22 |
Correct |
9 ms |
23132 KB |
Output is correct |
23 |
Correct |
8 ms |
23132 KB |
Output is correct |
24 |
Correct |
163 ms |
41408 KB |
Output is correct |
25 |
Correct |
187 ms |
41556 KB |
Output is correct |
26 |
Correct |
34 ms |
42324 KB |
Output is correct |
27 |
Correct |
105 ms |
42332 KB |
Output is correct |
28 |
Correct |
119 ms |
42276 KB |
Output is correct |
29 |
Correct |
142 ms |
42316 KB |
Output is correct |
30 |
Correct |
133 ms |
42560 KB |
Output is correct |
31 |
Correct |
7 ms |
23128 KB |
Output is correct |
32 |
Correct |
163 ms |
42324 KB |
Output is correct |
33 |
Correct |
1 ms |
10588 KB |
Output is correct |
34 |
Correct |
3 ms |
19036 KB |
Output is correct |
35 |
Correct |
115 ms |
42320 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
4 ms |
23132 KB |
Output is correct |
38 |
Correct |
49 ms |
42308 KB |
Output is correct |
39 |
Correct |
127 ms |
42332 KB |
Output is correct |