#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
queue < pair <int,int> > q;
pair <int,int> st, en;
char a[M][M];
int n, s, d[M][M], dist[M][M], res[M][M];
bool vist[M][M];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> s;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
if(a[i][j] == 'H') q.push({i, j}), d[i][j] = 1;
if(a[i][j] == 'D') en = {i, j};
if(a[i][j] == 'M') st = {i, j};
}
}
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || a[ny][nx] == 'D' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
d[ny][nx] = d[y][x] + 1;
q.push({ny, nx});
}
}
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cout << d[i][j] << " ";
// }
// cout << endl;
// }
d[en.F][en.S] = 1e9;
q.push(st);
dist[st.F][st.S] = 1;
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || (dist[y][x]) / s >= d[ny][nx] - 1 || dist[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
// cout << endl;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
// cout << endl;
// }
if(!dist[en.F][en.S]) return cout << -1 << endl, 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
res[i][j] = -1;
if(d[i][j] && dist[i][j]) res[i][j] = (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1);
// cout << res[i][j] << " ";
}
// cout << endl;
}
priority_queue < pair < int, pair <int,int> > > pq;
pq.push({res[st.F][st.S], st});
vist[st.F][st.S] = 1;
while(!pq.empty()){
int x = pq.top().S.S;
int y = pq.top().S.F;
int cost = pq.top().F;
pq.pop();
// cout << x << " " << y << " " << cost << endl;
if(x == en.S && y == en.F) return cout << cost << endl, 0;
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
int ncost = min(cost, res[ny][nx]);
if(vist[ny][nx] || dist[y][x] >= dist[ny][nx] || ncost == -1 || ny < 1 || nx < 1 || ny > n || nx > n) continue;
vist[ny][nx] = 1;
pq.push({ncost, {ny, nx}});
}
}
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
102 ms |
11060 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
32 |
Correct |
2 ms |
1228 KB |
Output is correct |
33 |
Incorrect |
7 ms |
4044 KB |
Output isn't correct |
34 |
Correct |
10 ms |
4832 KB |
Output is correct |
35 |
Incorrect |
14 ms |
5096 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
4684 KB |
Output isn't correct |
37 |
Correct |
10 ms |
5452 KB |
Output is correct |
38 |
Incorrect |
17 ms |
5836 KB |
Output isn't correct |
39 |
Incorrect |
11 ms |
5180 KB |
Output isn't correct |
40 |
Correct |
12 ms |
6056 KB |
Output is correct |
41 |
Incorrect |
23 ms |
6552 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
5752 KB |
Output isn't correct |
43 |
Correct |
15 ms |
6684 KB |
Output is correct |
44 |
Incorrect |
26 ms |
7188 KB |
Output isn't correct |
45 |
Incorrect |
15 ms |
6220 KB |
Output isn't correct |
46 |
Correct |
17 ms |
7276 KB |
Output is correct |
47 |
Incorrect |
35 ms |
7884 KB |
Output isn't correct |
48 |
Incorrect |
17 ms |
6760 KB |
Output isn't correct |
49 |
Correct |
20 ms |
8032 KB |
Output is correct |
50 |
Incorrect |
37 ms |
8588 KB |
Output isn't correct |
51 |
Incorrect |
23 ms |
7400 KB |
Output isn't correct |
52 |
Correct |
23 ms |
8572 KB |
Output is correct |
53 |
Incorrect |
43 ms |
9280 KB |
Output isn't correct |
54 |
Incorrect |
24 ms |
7920 KB |
Output isn't correct |
55 |
Correct |
26 ms |
9248 KB |
Output is correct |
56 |
Incorrect |
49 ms |
9932 KB |
Output isn't correct |
57 |
Incorrect |
25 ms |
8372 KB |
Output isn't correct |
58 |
Correct |
29 ms |
9844 KB |
Output is correct |
59 |
Incorrect |
62 ms |
10656 KB |
Output isn't correct |
60 |
Incorrect |
29 ms |
8940 KB |
Output isn't correct |
61 |
Correct |
33 ms |
10564 KB |
Output is correct |
62 |
Incorrect |
63 ms |
11368 KB |
Output isn't correct |
63 |
Correct |
78 ms |
11372 KB |
Output is correct |
64 |
Correct |
90 ms |
11332 KB |
Output is correct |
65 |
Correct |
82 ms |
11268 KB |
Output is correct |
66 |
Incorrect |
80 ms |
11268 KB |
Output isn't correct |
67 |
Correct |
41 ms |
7380 KB |
Output is correct |
68 |
Correct |
59 ms |
11368 KB |
Output is correct |
69 |
Correct |
57 ms |
11380 KB |
Output is correct |
70 |
Correct |
63 ms |
11336 KB |
Output is correct |
71 |
Correct |
49 ms |
11368 KB |
Output is correct |
72 |
Correct |
48 ms |
10692 KB |
Output is correct |
73 |
Correct |
69 ms |
11584 KB |
Output is correct |
74 |
Correct |
99 ms |
11576 KB |
Output is correct |
75 |
Correct |
80 ms |
11588 KB |
Output is correct |
76 |
Correct |
73 ms |
11564 KB |
Output is correct |
77 |
Correct |
80 ms |
11588 KB |
Output is correct |
78 |
Correct |
45 ms |
5980 KB |
Output is correct |
79 |
Correct |
85 ms |
10988 KB |
Output is correct |
80 |
Correct |
72 ms |
9412 KB |
Output is correct |
81 |
Correct |
78 ms |
9468 KB |
Output is correct |
82 |
Correct |
68 ms |
9440 KB |
Output is correct |
83 |
Correct |
93 ms |
9476 KB |
Output is correct |
84 |
Correct |
107 ms |
11072 KB |
Output is correct |
85 |
Correct |
81 ms |
9556 KB |
Output is correct |
86 |
Correct |
84 ms |
9500 KB |
Output is correct |
87 |
Correct |
88 ms |
9692 KB |
Output is correct |
88 |
Correct |
95 ms |
9788 KB |
Output is correct |
89 |
Correct |
103 ms |
11076 KB |
Output is correct |
90 |
Correct |
102 ms |
10756 KB |
Output is correct |
91 |
Correct |
126 ms |
11044 KB |
Output is correct |
92 |
Correct |
97 ms |
9804 KB |
Output is correct |