#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m;
int b[805][806];
queue<pair<int,int> > q;
int stx, sty;
int cnt[805][805];
int tmp[805][805];
bool check = 0;
char ch[805][805];
bool visited[805][805];
int main() {
IOS
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
cin >> ch[i][j];
if(ch[i][j] == 'H') {
b[i][j] = 1;
q.push({i,j});
}
if(ch[i][j] == 'M') {
stx = i;
sty = j;
}
}
}
while(q.size() > 0) {
int x = q.front().ff;
int y = q.front().ss;
q.pop();
if(x > 1 && b[x - 1][y] == 0 && ch[x - 1][y] != 'T' && ch[x - 1][y] != 'D') {
q.push({x - 1, y});
b[x - 1][y] = b[x][y] + 1;
}
if(x < n && b[x + 1][y] == 0 && ch[x + 1][y] != 'T' && ch[x + 1][y] != 'D') {
q.push({x + 1, y});
b[x + 1][y] = b[x][y] + 1;
}
if(y > 1 && b[x][y - 1] == 0 && ch[x][y - 1] != 'T' && ch[x][y - 1] != 'D') {
q.push({x, y - 1});
b[x][y - 1] = b[x][y] + 1;
}
if(y < n && b[x][y + 1] == 0 && ch[x][y + 1] != 'T' && ch[x][y + 1] != 'D') {
q.push({x, y + 1});
b[x][y + 1] = b[x][y] + 1;
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if(b[i][j] == 0) b[i][j] = 1e9;
else b[i][j] --;
}
}
int l = 0;
int r = n * n;
while(l < r) {
int mid = (l + r + 1) / 2;
memset(cnt, -1, sizeof(cnt));
memset(visited, 0, sizeof(visited));
while(q.size() > 0) q.pop();
//
if(mid >= b[stx][sty]) {
r = mid - 1;
continue;
}
q.push({stx, sty});
cnt[stx][sty] = mid;
tmp[stx][sty] = m;
visited[stx][sty] = 1;
bool ok = 0;
//
while(q.size() > 0) {
int x = q.front().ff;
int y = q.front().ss;
q.pop();
if(ch[x][y] == 'D') {
ok = 1;
break;
}
int num = cnt[x][y];
int move = tmp[x][y];
if(move == m) {
if(visited[x][y] == 0) continue;
num ++;
move = 0;
}
move ++;
//
if(x > 1 && num <= b[x - 1][y] && cnt[x - 1][y] == -1 && ch[x - 1][y] != 'T') {
cnt[x - 1][y] = num;
tmp[x - 1][y] = move;
q.push({x - 1, y});
if(num < b[x - 1][y]) visited[x - 1][y] = 1;
}
if(x < n && num <= b[x + 1][y] && cnt[x + 1][y] == -1 && ch[x + 1][y] != 'T') {
cnt[x + 1][y] = num;
tmp[x + 1][y] = move;
q.push({x + 1, y});
if(num < b[x + 1][y]) visited[x + 1][y] = 1;
}
if(y > 1 && num <= b[x][y - 1] && cnt[x][y - 1] == -1 && ch[x][y - 1] != 'T') {
cnt[x][y - 1] = num;
tmp[x][y - 1] = move;
q.push({x, y - 1});
if(num < b[x][y - 1]) visited[x][y - 1] = 1;
}
if(y < n && num <= b[x][y + 1] && cnt[x][y + 1] == -1 && ch[x][y + 1] != 'T') {
cnt[x][y + 1] = num;
tmp[x][y + 1] = move;
q.push({x, y + 1});
if(num < b[x][y + 1]) visited[x][y + 1] = 1;
}
}
if(ok == 1) {
check = 1;
l = mid;
}
else r = mid - 1;
}
if(check == 1){
cout << l;
}
else cout << -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
3 ms |
3540 KB |
Output is correct |
4 |
Correct |
2 ms |
3540 KB |
Output is correct |
5 |
Incorrect |
2 ms |
3540 KB |
Output isn't correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
86 ms |
8768 KB |
Output is correct |
8 |
Correct |
4 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
4 ms |
3536 KB |
Output is correct |
11 |
Correct |
2 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3660 KB |
Output is correct |
13 |
Incorrect |
3 ms |
3668 KB |
Output isn't correct |
14 |
Correct |
3 ms |
3796 KB |
Output is correct |
15 |
Correct |
2 ms |
3532 KB |
Output is correct |
16 |
Correct |
2 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3668 KB |
Output is correct |
21 |
Correct |
3 ms |
3668 KB |
Output is correct |
22 |
Correct |
3 ms |
3668 KB |
Output is correct |
23 |
Correct |
4 ms |
3668 KB |
Output is correct |
24 |
Correct |
3 ms |
3668 KB |
Output is correct |
25 |
Correct |
3 ms |
3668 KB |
Output is correct |
26 |
Correct |
4 ms |
3668 KB |
Output is correct |
27 |
Correct |
3 ms |
3668 KB |
Output is correct |
28 |
Correct |
3 ms |
3796 KB |
Output is correct |
29 |
Correct |
3 ms |
3792 KB |
Output is correct |
30 |
Correct |
3 ms |
3796 KB |
Output is correct |
31 |
Correct |
4 ms |
3788 KB |
Output is correct |
32 |
Correct |
4 ms |
3796 KB |
Output is correct |
33 |
Correct |
8 ms |
4960 KB |
Output is correct |
34 |
Correct |
6 ms |
4948 KB |
Output is correct |
35 |
Correct |
25 ms |
5992 KB |
Output is correct |
36 |
Correct |
9 ms |
5204 KB |
Output is correct |
37 |
Correct |
7 ms |
5204 KB |
Output is correct |
38 |
Correct |
25 ms |
6484 KB |
Output is correct |
39 |
Correct |
8 ms |
5504 KB |
Output is correct |
40 |
Correct |
8 ms |
5432 KB |
Output is correct |
41 |
Correct |
44 ms |
6852 KB |
Output is correct |
42 |
Correct |
12 ms |
5736 KB |
Output is correct |
43 |
Correct |
11 ms |
5716 KB |
Output is correct |
44 |
Correct |
41 ms |
7288 KB |
Output is correct |
45 |
Correct |
11 ms |
5924 KB |
Output is correct |
46 |
Correct |
10 ms |
5944 KB |
Output is correct |
47 |
Correct |
54 ms |
7756 KB |
Output is correct |
48 |
Correct |
12 ms |
6184 KB |
Output is correct |
49 |
Correct |
17 ms |
6232 KB |
Output is correct |
50 |
Correct |
72 ms |
8104 KB |
Output is correct |
51 |
Correct |
14 ms |
6484 KB |
Output is correct |
52 |
Correct |
13 ms |
6476 KB |
Output is correct |
53 |
Correct |
69 ms |
8524 KB |
Output is correct |
54 |
Correct |
17 ms |
6736 KB |
Output is correct |
55 |
Correct |
18 ms |
6672 KB |
Output is correct |
56 |
Correct |
97 ms |
8936 KB |
Output is correct |
57 |
Correct |
19 ms |
6984 KB |
Output is correct |
58 |
Correct |
23 ms |
7036 KB |
Output is correct |
59 |
Correct |
108 ms |
9360 KB |
Output is correct |
60 |
Correct |
19 ms |
7192 KB |
Output is correct |
61 |
Correct |
21 ms |
7256 KB |
Output is correct |
62 |
Correct |
119 ms |
9784 KB |
Output is correct |
63 |
Incorrect |
83 ms |
9732 KB |
Output isn't correct |
64 |
Correct |
171 ms |
9792 KB |
Output is correct |
65 |
Correct |
143 ms |
9800 KB |
Output is correct |
66 |
Correct |
105 ms |
9796 KB |
Output is correct |
67 |
Correct |
99 ms |
9792 KB |
Output is correct |
68 |
Incorrect |
44 ms |
9524 KB |
Output isn't correct |
69 |
Correct |
59 ms |
9756 KB |
Output is correct |
70 |
Correct |
43 ms |
9776 KB |
Output is correct |
71 |
Correct |
35 ms |
9824 KB |
Output is correct |
72 |
Correct |
28 ms |
7856 KB |
Output is correct |
73 |
Correct |
34 ms |
10044 KB |
Output is correct |
74 |
Correct |
49 ms |
10068 KB |
Output is correct |
75 |
Correct |
59 ms |
10008 KB |
Output is correct |
76 |
Correct |
61 ms |
10068 KB |
Output is correct |
77 |
Correct |
71 ms |
10056 KB |
Output is correct |
78 |
Correct |
77 ms |
8736 KB |
Output is correct |
79 |
Correct |
41 ms |
8332 KB |
Output is correct |
80 |
Correct |
60 ms |
8404 KB |
Output is correct |
81 |
Correct |
64 ms |
8732 KB |
Output is correct |
82 |
Correct |
50 ms |
8572 KB |
Output is correct |
83 |
Incorrect |
100 ms |
8652 KB |
Output isn't correct |
84 |
Correct |
85 ms |
8608 KB |
Output is correct |
85 |
Correct |
75 ms |
8652 KB |
Output is correct |
86 |
Correct |
83 ms |
8672 KB |
Output is correct |
87 |
Correct |
70 ms |
8684 KB |
Output is correct |
88 |
Incorrect |
68 ms |
8736 KB |
Output isn't correct |
89 |
Correct |
81 ms |
8692 KB |
Output is correct |
90 |
Correct |
97 ms |
8640 KB |
Output is correct |
91 |
Correct |
66 ms |
8816 KB |
Output is correct |
92 |
Correct |
71 ms |
8868 KB |
Output is correct |