#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int n, k;
bool ok = 0;
pair<int,int> sta, en;
vector<string> G;
vector<vector<int>> V;
vector<vector<int>> vis;
queue<pair<pair<int,int>,int>> Q;
auto cmp = [](pair<pair<int,int>,pair<int,int>> l, pair<pair<int,int>,pair<int,int>> r){
if(l.f.f != r.f.f)return l.f.f < r.f.f;
return l.f.s > r.f.s;
};
priority_queue<pair<pair<int,int>,pair<int,int>>, vector<pair<pair<int,int>,pair<int,int>>>, decltype(cmp)> PQ(cmp);
void fin(pair<pair<int,int>,pair<int,int>> now){
if(vis[now.s.f][now.s.s] < now.f.s)return;
vis[now.s.f][now.s.s] = now.f.s;
if(now.s == en){cout << now.f.f << "\n";ok = 1;return;}
if(now.s.f != n-1){
if(V[now.s.f+1][now.s.s] > now.f.s/k+now.f.s/k-1){
PQ.push({{min(V[now.s.f+1][now.s.s]-now.f.s/k-1,now.f.f), now.f.s+1},{now.s.f+1,now.s.s}});
}
}
if(now.s.s != n-1){
if(V[now.s.f][now.s.s+1] > now.f.s/k+now.f.s/k-1){
PQ.push({{min(V[now.s.f][now.s.s+1]-now.f.s/k-1,now.f.f), now.f.s+1},{now.s.f,now.s.s+1}});
}
}
if(now.s.f != 0){
if(V[now.s.f-1][now.s.s] > now.f.s/k+now.f.s/k-1){
PQ.push({{min(V[now.s.f-1][now.s.s]-now.f.s/k-1,now.f.f), now.f.s+1},{now.s.f-1,now.s.s}});
}
}
if(now.s.s != 0){
if(V[now.s.f][now.s.s-1] > (now.f.s/k)+now.f.s/k-1){
PQ.push({{min(V[now.s.f][now.s.s-1]-now.f.s/k-1,now.f.f), now.f.s+1},{now.s.f,now.s.s-1}});
}
}
}
void search(pair<pair<int,int>,int> now){
if(now.f.f < 0 || now.f.s < 0 || now.f.f >= n || now.f.s >= n)return;
if(V[now.f.f][now.f.s] != -1)return;
V[now.f.f][now.f.s] = now.s;
Q.push({{now.f.f-1,now.f.s},now.s+1});
Q.push({{now.f.f+1,now.f.s},now.s+1});
Q.push({{now.f.f,now.f.s-1},now.s+1});
Q.push({{now.f.f,now.f.s+1},now.s+1});
}
int main(){
cin >> n >> k;
G.resize(n);
V.resize(n, vector<int> (n,-1));
vis.resize(n, vector<int> (n, 100000));
for(auto &x : G)cin >> x;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(G[i][j] == 'T')V[i][j] = -2;
if(G[i][j] == 'M')sta = {i,j};
if(G[i][j] == 'D'){en = {i,j};V[i][j] = 100000;}
if(G[i][j] == 'H')Q.push({{i,j},0});
}
}
while(!Q.empty()){
auto x = Q.front();
Q.pop();
search(x);
}
//for(int i = 0; i < n; ++i){
//for(int j = 0; j < n; ++j){
//cout << V[i][j] << " ";
//}
//cout << "\n";
//}
PQ.push({{V[sta.f][sta.s], 0},sta});
while(!PQ.empty()){
auto x = PQ.top();
PQ.pop();
//cout << x.f.f << " " << x.f.s << " " << x.s.f << " " << x.s.s << "\n";
fin(x);
if(ok)break;
}
if(!ok)cout << -1 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
7 |
Runtime error |
492 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
13 |
Runtime error |
646 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Runtime error |
646 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
15 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Incorrect |
12 ms |
1536 KB |
Output isn't correct |
34 |
Correct |
14 ms |
1408 KB |
Output is correct |
35 |
Runtime error |
419 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
36 |
Incorrect |
16 ms |
1792 KB |
Output isn't correct |
37 |
Correct |
16 ms |
1792 KB |
Output is correct |
38 |
Runtime error |
433 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
39 |
Incorrect |
20 ms |
2176 KB |
Output isn't correct |
40 |
Correct |
20 ms |
2208 KB |
Output is correct |
41 |
Runtime error |
474 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Incorrect |
25 ms |
2816 KB |
Output isn't correct |
43 |
Correct |
25 ms |
2816 KB |
Output is correct |
44 |
Runtime error |
436 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
45 |
Incorrect |
32 ms |
3328 KB |
Output isn't correct |
46 |
Correct |
31 ms |
3200 KB |
Output is correct |
47 |
Runtime error |
450 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
48 |
Incorrect |
38 ms |
3832 KB |
Output isn't correct |
49 |
Correct |
35 ms |
3712 KB |
Output is correct |
50 |
Runtime error |
454 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
51 |
Incorrect |
47 ms |
4344 KB |
Output isn't correct |
52 |
Incorrect |
43 ms |
4352 KB |
Output isn't correct |
53 |
Runtime error |
463 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
54 |
Incorrect |
55 ms |
4864 KB |
Output isn't correct |
55 |
Incorrect |
52 ms |
4856 KB |
Output isn't correct |
56 |
Runtime error |
467 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Incorrect |
63 ms |
5504 KB |
Output isn't correct |
58 |
Incorrect |
65 ms |
5504 KB |
Output isn't correct |
59 |
Runtime error |
485 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
60 |
Incorrect |
69 ms |
6136 KB |
Output isn't correct |
61 |
Incorrect |
74 ms |
6144 KB |
Output isn't correct |
62 |
Runtime error |
499 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
63 |
Incorrect |
79 ms |
6136 KB |
Output isn't correct |
64 |
Correct |
134 ms |
6144 KB |
Output is correct |
65 |
Correct |
133 ms |
6136 KB |
Output is correct |
66 |
Incorrect |
98 ms |
6144 KB |
Output isn't correct |
67 |
Correct |
92 ms |
6160 KB |
Output is correct |
68 |
Incorrect |
88 ms |
6392 KB |
Output isn't correct |
69 |
Correct |
95 ms |
6392 KB |
Output is correct |
70 |
Incorrect |
78 ms |
6392 KB |
Output isn't correct |
71 |
Incorrect |
82 ms |
6416 KB |
Output isn't correct |
72 |
Incorrect |
79 ms |
6396 KB |
Output isn't correct |
73 |
Runtime error |
469 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
74 |
Runtime error |
703 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
75 |
Runtime error |
597 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
76 |
Runtime error |
620 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
77 |
Runtime error |
813 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
78 |
Runtime error |
703 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
79 |
Runtime error |
700 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
80 |
Runtime error |
694 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
81 |
Runtime error |
704 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
82 |
Runtime error |
691 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
83 |
Runtime error |
742 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
84 |
Runtime error |
684 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
85 |
Runtime error |
722 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
86 |
Runtime error |
768 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
87 |
Runtime error |
683 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
88 |
Runtime error |
499 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
89 |
Runtime error |
717 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
90 |
Runtime error |
509 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
91 |
Runtime error |
500 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
92 |
Runtime error |
506 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |