#include <bits/stdc++.h>
using namespace std;
const int N=810;
char G[N][N];
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int T[N][N],d[N][N];
int n,s,sx,sy,fx,fy;
bool vaild(int x,int y){
if (G[x][y]=='T'||G[x][y]=='H')return 0;
if (x<1||x>n||y<1||y>n)return 0;
return 1;
}
bool bfs(int time){
memset(d,-1,sizeof(d));
d[sx][sy]=time*s-1;
queue<int> Q;
Q.push(sx*N+sy);
while(!Q.empty()){
int x=Q.front()/N,y=Q.front()%N;Q.pop();
if (d[x][y]/s>=T[x][y])continue;
for (int k=0;k<4;k++){
int X=x+dx[k],Y=y+dy[k];
if (d[X][Y]>=0)continue;
if (!vaild(X,Y))continue;
if (X==fx&&Y==fy)return 1;
d[X][Y]=d[x][y]+1;
Q.push(X*N+Y);
}
}
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));
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')fx=i,fy=j,T[i][j]=0x7fffffff;
if (G[i][j]=='H')T[i][j]=0;
}
for (int t=1;;t++){
int ok=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (T[i][j]==t-1){
for (int k=0;k<4;k++){
int X=i+dx[k],Y=j+dy[k];
if (vaild(X,Y)&&T[X][Y]<0){
T[X][Y]=t;
ok=1;
}
}
}
if (!ok)break;
}
for (int i=T[sx][sy];i>=0;i--)
if (bfs(i)){
printf("%d\n",i);
return 0;
}else{
// printf("Time:%d\n",i);
// for (int x=1;x<=n;x++){
// for (int j=1;j<=n;j++)printf("%2d",d[x][j]);printf("\n");}
}
printf("-1\n");
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:33: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:34: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 |
Incorrect |
7 ms |
5496 KB |
Output isn't correct |
2 |
Incorrect |
7 ms |
5496 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
5496 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
5516 KB |
Output isn't correct |
5 |
Correct |
6 ms |
5496 KB |
Output is correct |
6 |
Correct |
7 ms |
5496 KB |
Output is correct |
7 |
Execution timed out |
1078 ms |
6904 KB |
Time limit exceeded |
8 |
Incorrect |
10 ms |
5496 KB |
Output isn't correct |
9 |
Correct |
7 ms |
5496 KB |
Output is correct |
10 |
Correct |
9 ms |
5496 KB |
Output is correct |
11 |
Correct |
7 ms |
5496 KB |
Output is correct |
12 |
Incorrect |
9 ms |
5496 KB |
Output isn't correct |
13 |
Incorrect |
8 ms |
5496 KB |
Output isn't correct |
14 |
Correct |
12 ms |
5496 KB |
Output is correct |
15 |
Incorrect |
6 ms |
5496 KB |
Output isn't correct |
16 |
Correct |
7 ms |
5496 KB |
Output is correct |
17 |
Incorrect |
7 ms |
5496 KB |
Output isn't correct |
18 |
Correct |
8 ms |
5496 KB |
Output is correct |
19 |
Incorrect |
8 ms |
5496 KB |
Output isn't correct |
20 |
Correct |
8 ms |
5496 KB |
Output is correct |
21 |
Incorrect |
7 ms |
5496 KB |
Output isn't correct |
22 |
Correct |
7 ms |
5496 KB |
Output is correct |
23 |
Incorrect |
9 ms |
5520 KB |
Output isn't correct |
24 |
Correct |
8 ms |
5496 KB |
Output is correct |
25 |
Incorrect |
11 ms |
5496 KB |
Output isn't correct |
26 |
Correct |
10 ms |
5496 KB |
Output is correct |
27 |
Incorrect |
15 ms |
5524 KB |
Output isn't correct |
28 |
Correct |
10 ms |
5496 KB |
Output is correct |
29 |
Incorrect |
18 ms |
5496 KB |
Output isn't correct |
30 |
Correct |
13 ms |
5496 KB |
Output is correct |
31 |
Incorrect |
22 ms |
5496 KB |
Output isn't correct |
32 |
Correct |
15 ms |
5496 KB |
Output is correct |
33 |
Execution timed out |
1067 ms |
3320 KB |
Time limit exceeded |
34 |
Execution timed out |
1068 ms |
3320 KB |
Time limit exceeded |
35 |
Correct |
425 ms |
6008 KB |
Output is correct |
36 |
Execution timed out |
1053 ms |
3320 KB |
Time limit exceeded |
37 |
Execution timed out |
1076 ms |
3320 KB |
Time limit exceeded |
38 |
Correct |
639 ms |
6008 KB |
Output is correct |
39 |
Execution timed out |
1070 ms |
3448 KB |
Time limit exceeded |
40 |
Execution timed out |
1064 ms |
3448 KB |
Time limit exceeded |
41 |
Execution timed out |
1016 ms |
6104 KB |
Time limit exceeded |
42 |
Execution timed out |
1075 ms |
3448 KB |
Time limit exceeded |
43 |
Execution timed out |
1082 ms |
3576 KB |
Time limit exceeded |
44 |
Execution timed out |
1081 ms |
6136 KB |
Time limit exceeded |
45 |
Execution timed out |
1071 ms |
3576 KB |
Time limit exceeded |
46 |
Execution timed out |
1089 ms |
3576 KB |
Time limit exceeded |
47 |
Execution timed out |
1061 ms |
6256 KB |
Time limit exceeded |
48 |
Execution timed out |
1066 ms |
3832 KB |
Time limit exceeded |
49 |
Execution timed out |
1069 ms |
3704 KB |
Time limit exceeded |
50 |
Execution timed out |
1085 ms |
6264 KB |
Time limit exceeded |
51 |
Execution timed out |
1054 ms |
3832 KB |
Time limit exceeded |
52 |
Execution timed out |
1070 ms |
3832 KB |
Time limit exceeded |
53 |
Execution timed out |
1077 ms |
6520 KB |
Time limit exceeded |
54 |
Execution timed out |
1073 ms |
3960 KB |
Time limit exceeded |
55 |
Execution timed out |
1071 ms |
3960 KB |
Time limit exceeded |
56 |
Execution timed out |
1072 ms |
6436 KB |
Time limit exceeded |
57 |
Execution timed out |
1065 ms |
3960 KB |
Time limit exceeded |
58 |
Execution timed out |
1064 ms |
3960 KB |
Time limit exceeded |
59 |
Execution timed out |
1060 ms |
6648 KB |
Time limit exceeded |
60 |
Execution timed out |
1054 ms |
4220 KB |
Time limit exceeded |
61 |
Execution timed out |
1064 ms |
4216 KB |
Time limit exceeded |
62 |
Execution timed out |
1079 ms |
6780 KB |
Time limit exceeded |
63 |
Execution timed out |
1076 ms |
4216 KB |
Time limit exceeded |
64 |
Execution timed out |
1081 ms |
4088 KB |
Time limit exceeded |
65 |
Execution timed out |
1073 ms |
4088 KB |
Time limit exceeded |
66 |
Execution timed out |
1060 ms |
4088 KB |
Time limit exceeded |
67 |
Execution timed out |
1064 ms |
4088 KB |
Time limit exceeded |
68 |
Correct |
465 ms |
6880 KB |
Output is correct |
69 |
Correct |
418 ms |
6648 KB |
Output is correct |
70 |
Correct |
413 ms |
6776 KB |
Output is correct |
71 |
Correct |
421 ms |
6776 KB |
Output is correct |
72 |
Incorrect |
364 ms |
6776 KB |
Output isn't correct |
73 |
Incorrect |
239 ms |
6776 KB |
Output isn't correct |
74 |
Correct |
545 ms |
6792 KB |
Output is correct |
75 |
Correct |
909 ms |
6760 KB |
Output is correct |
76 |
Correct |
832 ms |
6876 KB |
Output is correct |
77 |
Correct |
824 ms |
6908 KB |
Output is correct |
78 |
Execution timed out |
1081 ms |
7004 KB |
Time limit exceeded |
79 |
Correct |
392 ms |
6724 KB |
Output is correct |
80 |
Correct |
608 ms |
6880 KB |
Output is correct |
81 |
Execution timed out |
1088 ms |
6904 KB |
Time limit exceeded |
82 |
Correct |
682 ms |
6776 KB |
Output is correct |
83 |
Execution timed out |
1083 ms |
6876 KB |
Time limit exceeded |
84 |
Correct |
893 ms |
6756 KB |
Output is correct |
85 |
Correct |
599 ms |
6776 KB |
Output is correct |
86 |
Execution timed out |
1069 ms |
6776 KB |
Time limit exceeded |
87 |
Execution timed out |
1069 ms |
6880 KB |
Time limit exceeded |
88 |
Execution timed out |
1081 ms |
6784 KB |
Time limit exceeded |
89 |
Execution timed out |
1088 ms |
6648 KB |
Time limit exceeded |
90 |
Execution timed out |
1056 ms |
6752 KB |
Time limit exceeded |
91 |
Execution timed out |
1075 ms |
6904 KB |
Time limit exceeded |
92 |
Execution timed out |
1076 ms |
6756 KB |
Time limit exceeded |