#include <bits/stdc++.h>
using namespace std;
long long n,s,i,j,d[805][805],x,y,vi[805][805],tl,tr,mid;
char a[805][805];
vector<pair<long long,long long>> h;
queue<pair<long long,long long>> q;
pair<long long,long long> st,e;
const long long INF=1e14;
int main() {
cin >> n >> s;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
cin >> a[i][j];
if (a[i][j]=='H') {
h.push_back(make_pair(i,j));
}
if (a[i][j]=='M') {
st=make_pair(i,j);
}
if (a[i][j]=='D') {
e=make_pair(i,j);
}
}
}
for (i=0;i<h.size();i++) {
q.push(make_pair(h[i].first,h[i].second));
vi[h[i].first][h[i].second]=1;
}
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==0 && a[x-1][y]!='T' && a[x-1][y]!='D') {
d[x-1][y]=d[x][y]+s;
vi[x-1][y]=1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==0 && a[x+1][y]!='T' && a[x+1][y]!='D') {
d[x+1][y]=d[x][y]+s;
vi[x+1][y]=1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==0 && a[x][y-1]!='T' && a[x][y-1]!='D') {
d[x][y-1]=d[x][y]+s;
vi[x][y-1]=1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==0 && a[x][y+1]!='T' && a[x][y+1]!='D') {
d[x][y+1]=d[x][y]+s;
vi[x][y+1]=1;
q.push(make_pair(x,y+1));
}
}
d[e.first][e.second]=INF;
// for (i=1;i<=n;i++) {
// for (j=1;j<=n;j++) {
// cout << d[i][j] << ' ';
// }
// cout << endl;
// }
// cout << endl;
tl=0;
tr=n*n;
while(tl<tr) {
mid=(tl+tr)/2+1;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
vi[i][j]=-1;
}
}
if (d[st.first][st.second]<=mid*s) {
tr=mid-1;
continue;
}
q.push(st);
vi[st.first][st.second]=0;
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==-1 && a[x-1][y]!='T' && d[x-1][y]>vi[x][y]+1+mid*s) {
vi[x-1][y]=vi[x][y]+1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==-1 && a[x+1][y]!='T' && d[x+1][y]>vi[x][y]+1+mid*s) {
vi[x+1][y]=vi[x][y]+1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==-1 && a[x][y-1]!='T' && d[x][y-1]>vi[x][y]+1+mid*s) {
vi[x][y-1]=vi[x][y]+1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==-1 && a[x][y+1]!='T' && d[x][y+1]>vi[x][y]+1+mid*s) {
vi[x][y+1]=vi[x][y]+1;
q.push(make_pair(x,y+1));
}
}
// cout << tl << ' ' << tr << ' ' << mid << endl;
// for (i=1;i<=n;i++) {
// for (j=1;j<=n;j++) {
// cout << vi[i][j] << ' ';
// }
// cout << endl;
// }
// cout << endl;
if (vi[e.first][e.second]==-1) {
tr=mid-1;
}
else {
tl=mid;
}
}
mid=tl;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
vi[i][j]=-1;
}
}
if (d[st.first][st.second]<=mid*s) {
cout << -1 << endl;
return 0;
}
q.push(st);
vi[st.first][st.second]=0;
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==-1 && a[x-1][y]!='T' && d[x-1][y]>vi[x][y]+1+mid*s) {
vi[x-1][y]=vi[x][y]+1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==-1 && a[x+1][y]!='T' && d[x+1][y]>vi[x][y]+1+mid*s) {
vi[x+1][y]=vi[x][y]+1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==-1 && a[x][y-1]!='T' && d[x][y-1]>vi[x][y]+1+mid*s) {
vi[x][y-1]=vi[x][y]+1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==-1 && a[x][y+1]!='T' && d[x][y+1]>vi[x][y]+1+mid*s) {
vi[x][y+1]=vi[x][y]+1;
q.push(make_pair(x,y+1));
}
}
if (vi[e.first][e.second]==-1) {
cout << -1 << endl;
}
else {
cout << tl << endl;
}
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:26:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (i=0;i<h.size();i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
99 ms |
11248 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
12 ms |
4564 KB |
Output is correct |
34 |
Correct |
12 ms |
4820 KB |
Output is correct |
35 |
Correct |
24 ms |
5008 KB |
Output is correct |
36 |
Correct |
18 ms |
5192 KB |
Output is correct |
37 |
Correct |
15 ms |
5452 KB |
Output is correct |
38 |
Correct |
35 ms |
5844 KB |
Output is correct |
39 |
Correct |
17 ms |
5844 KB |
Output is correct |
40 |
Correct |
21 ms |
6212 KB |
Output is correct |
41 |
Correct |
38 ms |
6236 KB |
Output is correct |
42 |
Correct |
22 ms |
6428 KB |
Output is correct |
43 |
Correct |
21 ms |
6940 KB |
Output is correct |
44 |
Correct |
57 ms |
6996 KB |
Output is correct |
45 |
Correct |
28 ms |
6996 KB |
Output is correct |
46 |
Correct |
29 ms |
7600 KB |
Output is correct |
47 |
Correct |
75 ms |
7564 KB |
Output is correct |
48 |
Correct |
29 ms |
7620 KB |
Output is correct |
49 |
Correct |
29 ms |
8140 KB |
Output is correct |
50 |
Correct |
77 ms |
8348 KB |
Output is correct |
51 |
Correct |
34 ms |
8200 KB |
Output is correct |
52 |
Correct |
37 ms |
8740 KB |
Output is correct |
53 |
Correct |
130 ms |
8956 KB |
Output is correct |
54 |
Correct |
40 ms |
8784 KB |
Output is correct |
55 |
Correct |
42 ms |
9436 KB |
Output is correct |
56 |
Correct |
103 ms |
9684 KB |
Output is correct |
57 |
Correct |
45 ms |
9420 KB |
Output is correct |
58 |
Correct |
45 ms |
9908 KB |
Output is correct |
59 |
Correct |
117 ms |
10232 KB |
Output is correct |
60 |
Correct |
52 ms |
10060 KB |
Output is correct |
61 |
Correct |
58 ms |
10444 KB |
Output is correct |
62 |
Correct |
155 ms |
11144 KB |
Output is correct |
63 |
Correct |
127 ms |
11028 KB |
Output is correct |
64 |
Correct |
187 ms |
11016 KB |
Output is correct |
65 |
Correct |
185 ms |
10960 KB |
Output is correct |
66 |
Correct |
145 ms |
10956 KB |
Output is correct |
67 |
Correct |
142 ms |
10956 KB |
Output is correct |
68 |
Correct |
86 ms |
11084 KB |
Output is correct |
69 |
Correct |
86 ms |
11000 KB |
Output is correct |
70 |
Correct |
69 ms |
11084 KB |
Output is correct |
71 |
Correct |
72 ms |
11080 KB |
Output is correct |
72 |
Correct |
69 ms |
11084 KB |
Output is correct |
73 |
Correct |
71 ms |
11548 KB |
Output is correct |
74 |
Correct |
106 ms |
11524 KB |
Output is correct |
75 |
Correct |
101 ms |
11612 KB |
Output is correct |
76 |
Correct |
80 ms |
11520 KB |
Output is correct |
77 |
Correct |
98 ms |
11556 KB |
Output is correct |
78 |
Correct |
111 ms |
11508 KB |
Output is correct |
79 |
Correct |
75 ms |
11532 KB |
Output is correct |
80 |
Correct |
86 ms |
11532 KB |
Output is correct |
81 |
Correct |
88 ms |
11484 KB |
Output is correct |
82 |
Correct |
108 ms |
11424 KB |
Output is correct |
83 |
Correct |
119 ms |
11456 KB |
Output is correct |
84 |
Correct |
101 ms |
11448 KB |
Output is correct |
85 |
Correct |
96 ms |
11464 KB |
Output is correct |
86 |
Correct |
115 ms |
11460 KB |
Output is correct |
87 |
Correct |
107 ms |
11344 KB |
Output is correct |
88 |
Correct |
114 ms |
11280 KB |
Output is correct |
89 |
Correct |
106 ms |
11220 KB |
Output is correct |
90 |
Correct |
118 ms |
11344 KB |
Output is correct |
91 |
Correct |
120 ms |
11352 KB |
Output is correct |
92 |
Correct |
112 ms |
11264 KB |
Output is correct |