#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010
const int N = 810;
int n , s;
char grid[N][N];
int cost[N][N];
int dr[] = {0 , 1, 0 , -1};
int dc[] = {1 , 0 , -1 , 0};
int sr , sc , er , ec;
int cost2[N][N];
void BFSH(){
queue < pair< int , int > > q;
memset(cost , -1, sizeof(cost));
for(int i = 0;i < n;i++){
for(int j = 0 ;j < n;j++){
if(grid[i][j] == 'H'){
q.push(make_pair(i , j));
cost[i][j] = 0;
}
}
}
int row , col;
while(!q.empty()){
row = q.front().first;
col = q.front().second;
q.pop();
for(int nr , nc , i = 0 ;i < 4;i++){
nr = row + dr[i];
nc = col + dc[i];
if(nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || grid[nr][nc] == 'D' || cost[nr][nc] != -1) continue;
cost[nr][nc] = cost[row][col] + 1;
q.push(make_pair(nr , nc));
}
}
}
bool check(int mid){
for(int i = 0 ;i < n;i++) for(int j = 0 ;j < n;j++) cost2[i][j] = -1;
cost2[sr][sc] = mid * s;
queue < pair< int , int > > q;
q.push(make_pair(sr, sc));
int row , col;
while(!q.empty()){
row = q.front().first;
col = q.front().second;
q.pop();
if(row == er && col == ec)
return true;
if(cost2[row][col] >= cost[row][col] * s) continue;
for(int nr , nc , i = 0 ;i < 4;i++){
nr = row + dr[i];
nc = col + dc[i];
if(nr < 0 || nr >= n || nc < 0 || nc >= n || grid[nr][nc] == 'T' || cost2[nr][nc] != -1) continue;
cost2[nr][nc] = cost2[row][col] + 1;
q.push(make_pair(nr , nc));
}
}
return false;
}
int main(){
scanf("%d%d",&n,&s);
for(int i = 0 ;i < n;i++){
scanf("%s",grid[i]);
for(int j = 0 ;j < n;j++){
if(grid[i][j] == 'M')
sr = i , sc = j;
if(grid[i][j] == 'D')
er = i , ec = j;
}
}
BFSH();
int low = 0 , high = cost[sr][sc] - 1 , mid , ans = -1;
while(high >= low){
mid = (low + high) / 2;
if(check(mid))
ans = mid , low = mid + 1;
else
high = mid - 1;
}
cout << ans << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%s",grid[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
1 ms |
2900 KB |
Output is correct |
6 |
Correct |
1 ms |
2912 KB |
Output is correct |
7 |
Correct |
54 ms |
6060 KB |
Output is correct |
8 |
Correct |
1 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2900 KB |
Output is correct |
11 |
Correct |
1 ms |
2900 KB |
Output is correct |
12 |
Correct |
1 ms |
3028 KB |
Output is correct |
13 |
Correct |
1 ms |
3028 KB |
Output is correct |
14 |
Correct |
2 ms |
3028 KB |
Output is correct |
15 |
Correct |
1 ms |
2900 KB |
Output is correct |
16 |
Correct |
1 ms |
2888 KB |
Output is correct |
17 |
Correct |
1 ms |
2900 KB |
Output is correct |
18 |
Correct |
1 ms |
2900 KB |
Output is correct |
19 |
Correct |
1 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
3028 KB |
Output is correct |
23 |
Correct |
1 ms |
3028 KB |
Output is correct |
24 |
Correct |
1 ms |
3028 KB |
Output is correct |
25 |
Correct |
1 ms |
3028 KB |
Output is correct |
26 |
Correct |
2 ms |
3028 KB |
Output is correct |
27 |
Correct |
1 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
3072 KB |
Output is correct |
29 |
Correct |
1 ms |
3156 KB |
Output is correct |
30 |
Correct |
1 ms |
3028 KB |
Output is correct |
31 |
Correct |
1 ms |
3156 KB |
Output is correct |
32 |
Correct |
1 ms |
3156 KB |
Output is correct |
33 |
Correct |
3 ms |
4180 KB |
Output is correct |
34 |
Correct |
3 ms |
4180 KB |
Output is correct |
35 |
Correct |
13 ms |
4180 KB |
Output is correct |
36 |
Correct |
4 ms |
4436 KB |
Output is correct |
37 |
Correct |
4 ms |
4436 KB |
Output is correct |
38 |
Correct |
16 ms |
4456 KB |
Output is correct |
39 |
Correct |
4 ms |
4576 KB |
Output is correct |
40 |
Correct |
5 ms |
4564 KB |
Output is correct |
41 |
Correct |
23 ms |
4644 KB |
Output is correct |
42 |
Correct |
5 ms |
4820 KB |
Output is correct |
43 |
Correct |
5 ms |
4820 KB |
Output is correct |
44 |
Correct |
28 ms |
4860 KB |
Output is correct |
45 |
Correct |
6 ms |
4952 KB |
Output is correct |
46 |
Correct |
6 ms |
4948 KB |
Output is correct |
47 |
Correct |
32 ms |
4948 KB |
Output is correct |
48 |
Correct |
7 ms |
5204 KB |
Output is correct |
49 |
Correct |
7 ms |
5160 KB |
Output is correct |
50 |
Correct |
40 ms |
5148 KB |
Output is correct |
51 |
Correct |
7 ms |
5332 KB |
Output is correct |
52 |
Correct |
8 ms |
5460 KB |
Output is correct |
53 |
Correct |
45 ms |
5440 KB |
Output is correct |
54 |
Correct |
8 ms |
5592 KB |
Output is correct |
55 |
Correct |
8 ms |
5588 KB |
Output is correct |
56 |
Correct |
53 ms |
5588 KB |
Output is correct |
57 |
Correct |
10 ms |
5844 KB |
Output is correct |
58 |
Correct |
9 ms |
5844 KB |
Output is correct |
59 |
Correct |
59 ms |
5776 KB |
Output is correct |
60 |
Correct |
10 ms |
5944 KB |
Output is correct |
61 |
Correct |
10 ms |
5936 KB |
Output is correct |
62 |
Correct |
71 ms |
5948 KB |
Output is correct |
63 |
Correct |
85 ms |
6028 KB |
Output is correct |
64 |
Correct |
125 ms |
6024 KB |
Output is correct |
65 |
Correct |
122 ms |
6024 KB |
Output is correct |
66 |
Correct |
90 ms |
5972 KB |
Output is correct |
67 |
Correct |
79 ms |
6024 KB |
Output is correct |
68 |
Correct |
36 ms |
5964 KB |
Output is correct |
69 |
Correct |
29 ms |
6072 KB |
Output is correct |
70 |
Correct |
26 ms |
5972 KB |
Output is correct |
71 |
Correct |
23 ms |
5972 KB |
Output is correct |
72 |
Correct |
22 ms |
5976 KB |
Output is correct |
73 |
Correct |
26 ms |
6220 KB |
Output is correct |
74 |
Correct |
43 ms |
6348 KB |
Output is correct |
75 |
Correct |
40 ms |
6288 KB |
Output is correct |
76 |
Correct |
44 ms |
6308 KB |
Output is correct |
77 |
Correct |
46 ms |
6304 KB |
Output is correct |
78 |
Correct |
52 ms |
6384 KB |
Output is correct |
79 |
Correct |
37 ms |
6376 KB |
Output is correct |
80 |
Correct |
43 ms |
6296 KB |
Output is correct |
81 |
Correct |
46 ms |
6284 KB |
Output is correct |
82 |
Correct |
41 ms |
6312 KB |
Output is correct |
83 |
Correct |
50 ms |
6228 KB |
Output is correct |
84 |
Correct |
43 ms |
6240 KB |
Output is correct |
85 |
Correct |
46 ms |
6324 KB |
Output is correct |
86 |
Correct |
61 ms |
6244 KB |
Output is correct |
87 |
Correct |
43 ms |
6156 KB |
Output is correct |
88 |
Correct |
51 ms |
6084 KB |
Output is correct |
89 |
Correct |
52 ms |
6184 KB |
Output is correct |
90 |
Correct |
76 ms |
6188 KB |
Output is correct |
91 |
Correct |
52 ms |
6100 KB |
Output is correct |
92 |
Correct |
53 ms |
6192 KB |
Output is correct |