#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MN = 1010;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int R, C;
char G[MN][MN];
pii S, T;
int cc[MN][MN][4];
int dp(int r, int c, int d) {
int &ret = cc[r][c][d];
if(ret != -1) return ret;
int nr = r + dy[d];
int nc = c + dx[d];
if(G[nr][nc] == '#') return ret = r * C + c;
else return ret = dp(nr, nc, d);
}
vector<pii> adj[MN * MN];
priority_queue<pii> pq;
int dist[MN * MN];
void dijkstra(int s) {
for(int i = 0; i < R * C; i++) dist[i] = 1e9;
pq.push(pii(0, s));
dist[s] = 0;
while(!pq.empty()) {
int u = pq.top().second;
int ud = -pq.top().first;
pq.pop();
if(dist[u] < ud) continue;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first;
int w = adj[u][i].second;
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push(pii(-dist[v], v));
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin >> R >> C;
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
cin >> G[i][j];
}
}
cin >> S.first >> S.second >> T.first >> T.second;
S.first--;
S.second--;
T.first--;
T.second--;
memset(cc, -1, sizeof(cc));
for(int i = 1; i < R - 1; i++) {
for(int j = 1; j < C - 1; j++) {
for(int k = 0; k < 4; k++) {
int ni = i + dy[k];
int nj = j + dx[k];
if(G[ni][nj] == '#') continue;
adj[i * C + j].push_back(pii(ni * C + nj, 2));
adj[i * C + j].push_back(pii(dp(i, j, k), 1));
}
}
}
dijkstra(S.first * C + S.second);
if(dist[T.first * C + T.second] == 1e9) cout << -1;
else cout << dist[T.first * C + T.second];
}
Compilation message
skating.cpp: In function 'void dijkstra(int)':
skating.cpp:41:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
40440 KB |
Output is correct |
2 |
Correct |
46 ms |
40312 KB |
Output is correct |
3 |
Correct |
38 ms |
40312 KB |
Output is correct |
4 |
Correct |
38 ms |
40312 KB |
Output is correct |
5 |
Correct |
38 ms |
40312 KB |
Output is correct |
6 |
Correct |
38 ms |
40184 KB |
Output is correct |
7 |
Correct |
38 ms |
40312 KB |
Output is correct |
8 |
Correct |
39 ms |
40312 KB |
Output is correct |
9 |
Correct |
38 ms |
40272 KB |
Output is correct |
10 |
Correct |
38 ms |
40316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
40440 KB |
Output is correct |
2 |
Correct |
46 ms |
40312 KB |
Output is correct |
3 |
Correct |
38 ms |
40312 KB |
Output is correct |
4 |
Correct |
38 ms |
40312 KB |
Output is correct |
5 |
Correct |
38 ms |
40312 KB |
Output is correct |
6 |
Correct |
38 ms |
40184 KB |
Output is correct |
7 |
Correct |
38 ms |
40312 KB |
Output is correct |
8 |
Correct |
39 ms |
40312 KB |
Output is correct |
9 |
Correct |
38 ms |
40272 KB |
Output is correct |
10 |
Correct |
38 ms |
40316 KB |
Output is correct |
11 |
Correct |
59 ms |
43756 KB |
Output is correct |
12 |
Correct |
59 ms |
43784 KB |
Output is correct |
13 |
Correct |
59 ms |
43868 KB |
Output is correct |
14 |
Correct |
58 ms |
43384 KB |
Output is correct |
15 |
Correct |
49 ms |
42616 KB |
Output is correct |
16 |
Correct |
65 ms |
43896 KB |
Output is correct |
17 |
Correct |
57 ms |
43384 KB |
Output is correct |
18 |
Correct |
56 ms |
43256 KB |
Output is correct |
19 |
Correct |
59 ms |
43512 KB |
Output is correct |
20 |
Correct |
57 ms |
43640 KB |
Output is correct |
21 |
Correct |
51 ms |
42872 KB |
Output is correct |
22 |
Correct |
46 ms |
41976 KB |
Output is correct |
23 |
Correct |
58 ms |
43188 KB |
Output is correct |
24 |
Correct |
59 ms |
43708 KB |
Output is correct |
25 |
Correct |
55 ms |
43128 KB |
Output is correct |
26 |
Correct |
55 ms |
43212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
40440 KB |
Output is correct |
2 |
Correct |
46 ms |
40312 KB |
Output is correct |
3 |
Correct |
38 ms |
40312 KB |
Output is correct |
4 |
Correct |
38 ms |
40312 KB |
Output is correct |
5 |
Correct |
38 ms |
40312 KB |
Output is correct |
6 |
Correct |
38 ms |
40184 KB |
Output is correct |
7 |
Correct |
38 ms |
40312 KB |
Output is correct |
8 |
Correct |
39 ms |
40312 KB |
Output is correct |
9 |
Correct |
38 ms |
40272 KB |
Output is correct |
10 |
Correct |
38 ms |
40316 KB |
Output is correct |
11 |
Correct |
59 ms |
43756 KB |
Output is correct |
12 |
Correct |
59 ms |
43784 KB |
Output is correct |
13 |
Correct |
59 ms |
43868 KB |
Output is correct |
14 |
Correct |
58 ms |
43384 KB |
Output is correct |
15 |
Correct |
49 ms |
42616 KB |
Output is correct |
16 |
Correct |
65 ms |
43896 KB |
Output is correct |
17 |
Correct |
57 ms |
43384 KB |
Output is correct |
18 |
Correct |
56 ms |
43256 KB |
Output is correct |
19 |
Correct |
59 ms |
43512 KB |
Output is correct |
20 |
Correct |
57 ms |
43640 KB |
Output is correct |
21 |
Correct |
51 ms |
42872 KB |
Output is correct |
22 |
Correct |
46 ms |
41976 KB |
Output is correct |
23 |
Correct |
58 ms |
43188 KB |
Output is correct |
24 |
Correct |
59 ms |
43708 KB |
Output is correct |
25 |
Correct |
55 ms |
43128 KB |
Output is correct |
26 |
Correct |
55 ms |
43212 KB |
Output is correct |
27 |
Correct |
743 ms |
124600 KB |
Output is correct |
28 |
Correct |
881 ms |
124792 KB |
Output is correct |
29 |
Correct |
946 ms |
125424 KB |
Output is correct |
30 |
Correct |
765 ms |
123080 KB |
Output is correct |
31 |
Correct |
363 ms |
95756 KB |
Output is correct |
32 |
Correct |
661 ms |
124312 KB |
Output is correct |
33 |
Correct |
172 ms |
59724 KB |
Output is correct |
34 |
Correct |
470 ms |
117916 KB |
Output is correct |
35 |
Correct |
612 ms |
123496 KB |
Output is correct |
36 |
Correct |
573 ms |
119288 KB |
Output is correct |
37 |
Correct |
443 ms |
111276 KB |
Output is correct |
38 |
Correct |
891 ms |
124592 KB |
Output is correct |
39 |
Correct |
445 ms |
103288 KB |
Output is correct |
40 |
Correct |
222 ms |
79992 KB |
Output is correct |