#include <bits/stdc++.h>
using namespace std;
const int N=1010;
typedef pair<int,int> pii;
char G[N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int T[N][N],d[N][N],vis[N][N];
int n,s,sx,sy;
int bfs(int time){
if (time*s>=T[sx][sy])return 0;
memset(vis,0,sizeof(vis));
queue<pii> Q;
vis[sx][sy]=1;
Q.push(make_pair(sx*N+sy,time*s));
while(!Q.empty()){
int dis=Q.front().second;
int x=Q.front().first/N;
int y=Q.front().first%N;
Q.pop();
if (G[x][y]=='D')return 1;
for (int k=0;k<4;k++){
int X=x+dx[k],Y=y+dy[k];
if (X<1||X>n||Y<1||Y>n||G[X][Y]=='T'||dis+1>=T[X][Y]||vis[X][Y])continue;
vis[X][Y]=1;
Q.push(make_pair(X*N+Y,dis+1));
}
}
return 0;
}
int main(){
scanf("%d%d",&n,&s);
for (int i=1;i<=n;i++)scanf("%s",G[i]+1);
memset(T,-1,sizeof(T));
queue<int> bq;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
if (G[i][j]=='M')sx=i,sy=j;
if (G[i][j]=='D')T[i][j]=n*n*s;
if (G[i][j]=='H'){
T[i][j]=0;
bq.push(i*N+j);
}
}
while(!bq.empty()){
int x=bq.front()/N,y=bq.front()%N;
bq.pop();
for (int i=0;i<4;i++){
int X=x+dx[i],Y=y+dy[i];
if (x<1||x>n||y<1||y>n||T[X][Y]!=-1)continue;
if (G[X][Y]=='T'||G[X][Y]=='D')continue;
T[X][Y]=T[x][y]+s;
bq.push(X*N+Y);
}
}
int l=-1,r=n*n*2;
while(l+1<r){
int m=(l+r)/2;
if (bfs(m))l=m;
else r=m;
}
printf("%d\n",l);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&s);
~~~~~^~~~~~~~~~~~~~
mecho.cpp:32:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1;i<=n;i++)scanf("%s",G[i]+1);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8312 KB |
Output is correct |
2 |
Correct |
9 ms |
8312 KB |
Output is correct |
3 |
Correct |
9 ms |
8440 KB |
Output is correct |
4 |
Correct |
9 ms |
8316 KB |
Output is correct |
5 |
Correct |
8 ms |
8312 KB |
Output is correct |
6 |
Correct |
9 ms |
8360 KB |
Output is correct |
7 |
Correct |
120 ms |
9796 KB |
Output is correct |
8 |
Correct |
11 ms |
8312 KB |
Output is correct |
9 |
Correct |
11 ms |
8312 KB |
Output is correct |
10 |
Correct |
12 ms |
8312 KB |
Output is correct |
11 |
Correct |
11 ms |
8312 KB |
Output is correct |
12 |
Correct |
22 ms |
8412 KB |
Output is correct |
13 |
Correct |
11 ms |
8440 KB |
Output is correct |
14 |
Correct |
10 ms |
8312 KB |
Output is correct |
15 |
Correct |
12 ms |
8312 KB |
Output is correct |
16 |
Correct |
11 ms |
8284 KB |
Output is correct |
17 |
Correct |
12 ms |
8312 KB |
Output is correct |
18 |
Correct |
10 ms |
8312 KB |
Output is correct |
19 |
Correct |
10 ms |
8312 KB |
Output is correct |
20 |
Correct |
11 ms |
8312 KB |
Output is correct |
21 |
Correct |
12 ms |
8312 KB |
Output is correct |
22 |
Correct |
13 ms |
8440 KB |
Output is correct |
23 |
Correct |
10 ms |
8312 KB |
Output is correct |
24 |
Correct |
10 ms |
8296 KB |
Output is correct |
25 |
Correct |
10 ms |
8312 KB |
Output is correct |
26 |
Correct |
11 ms |
8440 KB |
Output is correct |
27 |
Correct |
10 ms |
8312 KB |
Output is correct |
28 |
Correct |
11 ms |
8440 KB |
Output is correct |
29 |
Correct |
11 ms |
8440 KB |
Output is correct |
30 |
Correct |
10 ms |
8312 KB |
Output is correct |
31 |
Correct |
11 ms |
8436 KB |
Output is correct |
32 |
Correct |
13 ms |
8440 KB |
Output is correct |
33 |
Correct |
13 ms |
8696 KB |
Output is correct |
34 |
Correct |
14 ms |
8824 KB |
Output is correct |
35 |
Correct |
32 ms |
8796 KB |
Output is correct |
36 |
Correct |
14 ms |
8948 KB |
Output is correct |
37 |
Correct |
15 ms |
8952 KB |
Output is correct |
38 |
Correct |
50 ms |
8952 KB |
Output is correct |
39 |
Correct |
15 ms |
8956 KB |
Output is correct |
40 |
Correct |
15 ms |
8952 KB |
Output is correct |
41 |
Correct |
51 ms |
9008 KB |
Output is correct |
42 |
Correct |
16 ms |
9080 KB |
Output is correct |
43 |
Correct |
18 ms |
9208 KB |
Output is correct |
44 |
Correct |
62 ms |
9104 KB |
Output is correct |
45 |
Correct |
17 ms |
9080 KB |
Output is correct |
46 |
Correct |
17 ms |
9080 KB |
Output is correct |
47 |
Correct |
69 ms |
9200 KB |
Output is correct |
48 |
Correct |
18 ms |
9336 KB |
Output is correct |
49 |
Correct |
18 ms |
9208 KB |
Output is correct |
50 |
Correct |
89 ms |
9336 KB |
Output is correct |
51 |
Correct |
19 ms |
9336 KB |
Output is correct |
52 |
Correct |
19 ms |
9336 KB |
Output is correct |
53 |
Correct |
108 ms |
9336 KB |
Output is correct |
54 |
Correct |
21 ms |
9464 KB |
Output is correct |
55 |
Correct |
20 ms |
9464 KB |
Output is correct |
56 |
Correct |
124 ms |
9592 KB |
Output is correct |
57 |
Correct |
22 ms |
9592 KB |
Output is correct |
58 |
Correct |
23 ms |
9592 KB |
Output is correct |
59 |
Correct |
149 ms |
9740 KB |
Output is correct |
60 |
Correct |
23 ms |
9720 KB |
Output is correct |
61 |
Correct |
23 ms |
9720 KB |
Output is correct |
62 |
Correct |
167 ms |
9848 KB |
Output is correct |
63 |
Correct |
125 ms |
9720 KB |
Output is correct |
64 |
Correct |
247 ms |
9852 KB |
Output is correct |
65 |
Correct |
184 ms |
9720 KB |
Output is correct |
66 |
Correct |
148 ms |
9812 KB |
Output is correct |
67 |
Correct |
140 ms |
9720 KB |
Output is correct |
68 |
Correct |
57 ms |
9848 KB |
Output is correct |
69 |
Correct |
58 ms |
9720 KB |
Output is correct |
70 |
Correct |
43 ms |
9720 KB |
Output is correct |
71 |
Correct |
49 ms |
9720 KB |
Output is correct |
72 |
Correct |
37 ms |
9764 KB |
Output is correct |
73 |
Correct |
40 ms |
9848 KB |
Output is correct |
74 |
Correct |
69 ms |
9976 KB |
Output is correct |
75 |
Correct |
86 ms |
9848 KB |
Output is correct |
76 |
Correct |
76 ms |
9904 KB |
Output is correct |
77 |
Correct |
75 ms |
9864 KB |
Output is correct |
78 |
Correct |
96 ms |
9848 KB |
Output is correct |
79 |
Correct |
58 ms |
9820 KB |
Output is correct |
80 |
Correct |
76 ms |
9976 KB |
Output is correct |
81 |
Correct |
85 ms |
9848 KB |
Output is correct |
82 |
Correct |
73 ms |
9820 KB |
Output is correct |
83 |
Correct |
99 ms |
9848 KB |
Output is correct |
84 |
Correct |
89 ms |
9848 KB |
Output is correct |
85 |
Correct |
101 ms |
9852 KB |
Output is correct |
86 |
Correct |
98 ms |
9848 KB |
Output is correct |
87 |
Correct |
97 ms |
9912 KB |
Output is correct |
88 |
Correct |
108 ms |
9896 KB |
Output is correct |
89 |
Correct |
111 ms |
9848 KB |
Output is correct |
90 |
Correct |
108 ms |
9848 KB |
Output is correct |
91 |
Correct |
106 ms |
9848 KB |
Output is correct |
92 |
Correct |
114 ms |
9948 KB |
Output is correct |