#include <bits/stdc++.h>
using namespace std;
const int N=810;
typedef pair<int,int> pii;
char G[N][N];
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
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));
// printf("X=%d Y=%d dis=%d\n",sx,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)continue;
if (G[X][Y]=='T')continue;
if (dis+1>=T[X][Y]||vis[X][Y])continue;
vis[X][Y]=1;
Q.push(make_pair(X*N+Y,dis+1));
// printf("X=%d Y=%d dis=%d\n",X,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:36: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:37: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 |
6 ms |
5496 KB |
Output is correct |
2 |
Correct |
7 ms |
5496 KB |
Output is correct |
3 |
Correct |
6 ms |
5496 KB |
Output is correct |
4 |
Correct |
6 ms |
5496 KB |
Output is correct |
5 |
Correct |
6 ms |
5496 KB |
Output is correct |
6 |
Correct |
6 ms |
5496 KB |
Output is correct |
7 |
Correct |
99 ms |
6236 KB |
Output is correct |
8 |
Correct |
6 ms |
5496 KB |
Output is correct |
9 |
Correct |
6 ms |
5496 KB |
Output is correct |
10 |
Correct |
7 ms |
5496 KB |
Output is correct |
11 |
Incorrect |
6 ms |
5500 KB |
Output isn't correct |
12 |
Correct |
6 ms |
5496 KB |
Output is correct |
13 |
Correct |
6 ms |
5496 KB |
Output is correct |
14 |
Correct |
7 ms |
5496 KB |
Output is correct |
15 |
Correct |
7 ms |
5496 KB |
Output is correct |
16 |
Correct |
7 ms |
5496 KB |
Output is correct |
17 |
Correct |
7 ms |
5496 KB |
Output is correct |
18 |
Correct |
7 ms |
5496 KB |
Output is correct |
19 |
Correct |
7 ms |
5496 KB |
Output is correct |
20 |
Correct |
7 ms |
5496 KB |
Output is correct |
21 |
Correct |
7 ms |
5496 KB |
Output is correct |
22 |
Correct |
7 ms |
5496 KB |
Output is correct |
23 |
Correct |
7 ms |
5496 KB |
Output is correct |
24 |
Correct |
7 ms |
5496 KB |
Output is correct |
25 |
Correct |
7 ms |
5496 KB |
Output is correct |
26 |
Correct |
7 ms |
5496 KB |
Output is correct |
27 |
Correct |
7 ms |
5496 KB |
Output is correct |
28 |
Correct |
8 ms |
5556 KB |
Output is correct |
29 |
Correct |
8 ms |
5496 KB |
Output is correct |
30 |
Correct |
7 ms |
5496 KB |
Output is correct |
31 |
Correct |
8 ms |
5500 KB |
Output is correct |
32 |
Correct |
8 ms |
5496 KB |
Output is correct |
33 |
Correct |
10 ms |
5752 KB |
Output is correct |
34 |
Correct |
10 ms |
5752 KB |
Output is correct |
35 |
Correct |
26 ms |
5752 KB |
Output is correct |
36 |
Correct |
10 ms |
5752 KB |
Output is correct |
37 |
Correct |
11 ms |
5752 KB |
Output is correct |
38 |
Correct |
36 ms |
5752 KB |
Output is correct |
39 |
Correct |
11 ms |
5752 KB |
Output is correct |
40 |
Correct |
12 ms |
5848 KB |
Output is correct |
41 |
Correct |
47 ms |
5864 KB |
Output is correct |
42 |
Correct |
12 ms |
6008 KB |
Output is correct |
43 |
Correct |
14 ms |
6008 KB |
Output is correct |
44 |
Correct |
54 ms |
6008 KB |
Output is correct |
45 |
Correct |
13 ms |
6008 KB |
Output is correct |
46 |
Correct |
13 ms |
6136 KB |
Output is correct |
47 |
Correct |
65 ms |
6136 KB |
Output is correct |
48 |
Correct |
14 ms |
6136 KB |
Output is correct |
49 |
Correct |
15 ms |
6268 KB |
Output is correct |
50 |
Correct |
80 ms |
6136 KB |
Output is correct |
51 |
Correct |
15 ms |
6296 KB |
Output is correct |
52 |
Correct |
16 ms |
6172 KB |
Output is correct |
53 |
Correct |
93 ms |
6296 KB |
Output is correct |
54 |
Correct |
16 ms |
6268 KB |
Output is correct |
55 |
Correct |
20 ms |
6304 KB |
Output is correct |
56 |
Correct |
115 ms |
6336 KB |
Output is correct |
57 |
Correct |
18 ms |
6260 KB |
Output is correct |
58 |
Correct |
19 ms |
6308 KB |
Output is correct |
59 |
Correct |
134 ms |
6392 KB |
Output is correct |
60 |
Correct |
18 ms |
6392 KB |
Output is correct |
61 |
Correct |
19 ms |
6384 KB |
Output is correct |
62 |
Correct |
151 ms |
6392 KB |
Output is correct |
63 |
Correct |
118 ms |
6400 KB |
Output is correct |
64 |
Correct |
245 ms |
6396 KB |
Output is correct |
65 |
Correct |
169 ms |
6396 KB |
Output is correct |
66 |
Correct |
148 ms |
6400 KB |
Output is correct |
67 |
Correct |
132 ms |
6404 KB |
Output is correct |
68 |
Correct |
61 ms |
6392 KB |
Output is correct |
69 |
Correct |
52 ms |
6392 KB |
Output is correct |
70 |
Correct |
44 ms |
6392 KB |
Output is correct |
71 |
Correct |
46 ms |
6440 KB |
Output is correct |
72 |
Correct |
49 ms |
6392 KB |
Output is correct |
73 |
Correct |
37 ms |
6520 KB |
Output is correct |
74 |
Correct |
65 ms |
6520 KB |
Output is correct |
75 |
Correct |
80 ms |
6520 KB |
Output is correct |
76 |
Correct |
67 ms |
6520 KB |
Output is correct |
77 |
Correct |
66 ms |
6520 KB |
Output is correct |
78 |
Correct |
84 ms |
6520 KB |
Output is correct |
79 |
Correct |
53 ms |
6472 KB |
Output is correct |
80 |
Correct |
64 ms |
6520 KB |
Output is correct |
81 |
Correct |
76 ms |
6648 KB |
Output is correct |
82 |
Correct |
63 ms |
6520 KB |
Output is correct |
83 |
Correct |
87 ms |
6432 KB |
Output is correct |
84 |
Correct |
77 ms |
6392 KB |
Output is correct |
85 |
Correct |
88 ms |
6536 KB |
Output is correct |
86 |
Correct |
81 ms |
6392 KB |
Output is correct |
87 |
Correct |
83 ms |
6540 KB |
Output is correct |
88 |
Correct |
93 ms |
6392 KB |
Output is correct |
89 |
Correct |
93 ms |
6520 KB |
Output is correct |
90 |
Correct |
93 ms |
6392 KB |
Output is correct |
91 |
Correct |
92 ms |
6432 KB |
Output is correct |
92 |
Correct |
102 ms |
6480 KB |
Output is correct |