#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define int long long
const int N=1000;
char arr[N][N];
bool vis[N][N];
int xx[4]={1,-1,0,0};
int yy[4]={0,0,-1,1};
int sx,sy,fx,fy;
char brr[N][N],crr[N][N];
int n,s;
void dfs(int y, int x){
vis[y][x]=1;
for(int i=0;i<4;i++){
if((brr[y+yy[i]][x+xx[i]]=='G' or brr[y+yy[i]][x+xx[i]]=='D') and vis[y+yy[i]][x+xx[i]]==0 ){
dfs(y+yy[i],x+xx[i]);
}
}
}
bool check(int t){
int h=t;
while(h--){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
crr[i][j]=brr[i][j];
vis[i][j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=0;k<4;k++){
if(crr[i+yy[k]][j+xx[k]]=='G' and crr[i][j]=='H'){
brr[i+yy[k]][j+xx[k]]='H';
}
}
}
}
}
vis[sy][sx]=1;
dfs(sy,sx);
return vis[fy][fx];
}
signed main(){
cin>>n>>s;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>arr[i][j];
if(arr[i][j]=='M'){
sy=i;
sx=j;
}
if(arr[i][j]=='D'){
fy=i;
fx=j;
}
}
}
int lo=0,hi=1000;
while(hi>=lo){
int mid=(hi+lo)/2;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
brr[i][j]=arr[i][j];
}
}
if(check(mid)){
lo=mid+1;
}
else{
hi=mid-1;
}
}
cout<<hi-2;
}
Compilation message
mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:36:46: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
36 | brr[i+yy[k]][j+xx[k]]='H';
| ~~~~~~~~~~~~~~~~~~~~~^~~~
mecho.cpp:13:6: note: at offset 0 to object 'brr' with size 1000000 declared here
13 | char brr[N][N],crr[N][N];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Execution timed out |
1097 ms |
4016 KB |
Time limit exceeded |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
108 ms |
604 KB |
Output isn't correct |
13 |
Incorrect |
10 ms |
652 KB |
Output isn't correct |
14 |
Incorrect |
14 ms |
696 KB |
Output isn't correct |
15 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
17 ms |
2556 KB |
Output isn't correct |
20 |
Incorrect |
9 ms |
580 KB |
Output isn't correct |
21 |
Incorrect |
56 ms |
604 KB |
Output isn't correct |
22 |
Incorrect |
26 ms |
604 KB |
Output isn't correct |
23 |
Incorrect |
100 ms |
604 KB |
Output isn't correct |
24 |
Incorrect |
61 ms |
604 KB |
Output isn't correct |
25 |
Incorrect |
147 ms |
600 KB |
Output isn't correct |
26 |
Incorrect |
137 ms |
2616 KB |
Output isn't correct |
27 |
Incorrect |
182 ms |
2652 KB |
Output isn't correct |
28 |
Incorrect |
164 ms |
2396 KB |
Output isn't correct |
29 |
Incorrect |
220 ms |
2900 KB |
Output isn't correct |
30 |
Incorrect |
293 ms |
2648 KB |
Output isn't correct |
31 |
Incorrect |
238 ms |
2768 KB |
Output isn't correct |
32 |
Incorrect |
238 ms |
2676 KB |
Output isn't correct |
33 |
Execution timed out |
1064 ms |
6776 KB |
Time limit exceeded |
34 |
Execution timed out |
1069 ms |
3732 KB |
Time limit exceeded |
35 |
Correct |
604 ms |
5508 KB |
Output is correct |
36 |
Execution timed out |
1039 ms |
6992 KB |
Time limit exceeded |
37 |
Execution timed out |
1090 ms |
4460 KB |
Time limit exceeded |
38 |
Correct |
869 ms |
7020 KB |
Output is correct |
39 |
Execution timed out |
1055 ms |
8532 KB |
Time limit exceeded |
40 |
Execution timed out |
1026 ms |
5468 KB |
Time limit exceeded |
41 |
Execution timed out |
1048 ms |
8528 KB |
Time limit exceeded |
42 |
Execution timed out |
1035 ms |
10308 KB |
Time limit exceeded |
43 |
Execution timed out |
1045 ms |
6472 KB |
Time limit exceeded |
44 |
Execution timed out |
1048 ms |
3396 KB |
Time limit exceeded |
45 |
Execution timed out |
1041 ms |
12112 KB |
Time limit exceeded |
46 |
Execution timed out |
1063 ms |
7508 KB |
Time limit exceeded |
47 |
Execution timed out |
1026 ms |
2648 KB |
Time limit exceeded |
48 |
Execution timed out |
1034 ms |
14160 KB |
Time limit exceeded |
49 |
Execution timed out |
1066 ms |
8512 KB |
Time limit exceeded |
50 |
Execution timed out |
1026 ms |
3160 KB |
Time limit exceeded |
51 |
Execution timed out |
1032 ms |
3160 KB |
Time limit exceeded |
52 |
Execution timed out |
1057 ms |
9784 KB |
Time limit exceeded |
53 |
Execution timed out |
1060 ms |
3932 KB |
Time limit exceeded |
54 |
Execution timed out |
1004 ms |
4184 KB |
Time limit exceeded |
55 |
Execution timed out |
1067 ms |
4188 KB |
Time limit exceeded |
56 |
Execution timed out |
1077 ms |
4188 KB |
Time limit exceeded |
57 |
Execution timed out |
1024 ms |
4176 KB |
Time limit exceeded |
58 |
Execution timed out |
1080 ms |
4184 KB |
Time limit exceeded |
59 |
Execution timed out |
1060 ms |
4188 KB |
Time limit exceeded |
60 |
Execution timed out |
1072 ms |
4332 KB |
Time limit exceeded |
61 |
Execution timed out |
1076 ms |
4444 KB |
Time limit exceeded |
62 |
Execution timed out |
1063 ms |
4444 KB |
Time limit exceeded |
63 |
Execution timed out |
1063 ms |
3932 KB |
Time limit exceeded |
64 |
Execution timed out |
1060 ms |
3932 KB |
Time limit exceeded |
65 |
Execution timed out |
1022 ms |
3928 KB |
Time limit exceeded |
66 |
Execution timed out |
1060 ms |
3932 KB |
Time limit exceeded |
67 |
Execution timed out |
1066 ms |
3932 KB |
Time limit exceeded |
68 |
Execution timed out |
1066 ms |
3932 KB |
Time limit exceeded |
69 |
Execution timed out |
1051 ms |
3928 KB |
Time limit exceeded |
70 |
Execution timed out |
1029 ms |
3928 KB |
Time limit exceeded |
71 |
Execution timed out |
1054 ms |
3932 KB |
Time limit exceeded |
72 |
Execution timed out |
1065 ms |
3932 KB |
Time limit exceeded |
73 |
Execution timed out |
1056 ms |
3932 KB |
Time limit exceeded |
74 |
Execution timed out |
1038 ms |
3928 KB |
Time limit exceeded |
75 |
Execution timed out |
1075 ms |
3932 KB |
Time limit exceeded |
76 |
Execution timed out |
1077 ms |
3932 KB |
Time limit exceeded |
77 |
Execution timed out |
1062 ms |
3924 KB |
Time limit exceeded |
78 |
Execution timed out |
1042 ms |
3924 KB |
Time limit exceeded |
79 |
Execution timed out |
1033 ms |
3932 KB |
Time limit exceeded |
80 |
Execution timed out |
1044 ms |
3920 KB |
Time limit exceeded |
81 |
Execution timed out |
1025 ms |
3928 KB |
Time limit exceeded |
82 |
Execution timed out |
1065 ms |
3932 KB |
Time limit exceeded |
83 |
Execution timed out |
1060 ms |
4436 KB |
Time limit exceeded |
84 |
Execution timed out |
1035 ms |
4432 KB |
Time limit exceeded |
85 |
Execution timed out |
1045 ms |
4436 KB |
Time limit exceeded |
86 |
Execution timed out |
1057 ms |
4444 KB |
Time limit exceeded |
87 |
Execution timed out |
1066 ms |
4396 KB |
Time limit exceeded |
88 |
Execution timed out |
1026 ms |
4432 KB |
Time limit exceeded |
89 |
Execution timed out |
1026 ms |
4440 KB |
Time limit exceeded |
90 |
Execution timed out |
1068 ms |
4444 KB |
Time limit exceeded |
91 |
Execution timed out |
1046 ms |
4440 KB |
Time limit exceeded |
92 |
Execution timed out |
1046 ms |
4432 KB |
Time limit exceeded |