#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' || 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((cost2[row][col] / s) >= cost[row][col]) 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));
}
}
if(cost2[er][ec] == -1)
return false;
return true;
}
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[er][ec] - 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]);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
2900 KB |
Output is correct |
6 |
Correct |
1 ms |
2900 KB |
Output is correct |
7 |
Correct |
40 ms |
6100 KB |
Output is correct |
8 |
Correct |
1 ms |
2900 KB |
Output is correct |
9 |
Correct |
1 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 |
Incorrect |
1 ms |
3028 KB |
Output isn't 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 |
2900 KB |
Output is correct |
17 |
Correct |
2 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 |
1 ms |
2900 KB |
Output is correct |
21 |
Correct |
1 ms |
3028 KB |
Output is correct |
22 |
Correct |
2 ms |
3028 KB |
Output is correct |
23 |
Correct |
1 ms |
3152 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 |
3028 KB |
Output is correct |
29 |
Correct |
2 ms |
3028 KB |
Output is correct |
30 |
Correct |
1 ms |
3028 KB |
Output is correct |
31 |
Correct |
1 ms |
3156 KB |
Output is correct |
32 |
Correct |
2 ms |
3156 KB |
Output is correct |
33 |
Correct |
4 ms |
4180 KB |
Output is correct |
34 |
Correct |
6 ms |
4308 KB |
Output is correct |
35 |
Correct |
8 ms |
4180 KB |
Output is correct |
36 |
Correct |
5 ms |
4436 KB |
Output is correct |
37 |
Correct |
5 ms |
4436 KB |
Output is correct |
38 |
Correct |
10 ms |
4460 KB |
Output is correct |
39 |
Correct |
6 ms |
4564 KB |
Output is correct |
40 |
Correct |
7 ms |
4564 KB |
Output is correct |
41 |
Correct |
12 ms |
4564 KB |
Output is correct |
42 |
Correct |
6 ms |
4820 KB |
Output is correct |
43 |
Correct |
7 ms |
4820 KB |
Output is correct |
44 |
Correct |
14 ms |
4820 KB |
Output is correct |
45 |
Correct |
7 ms |
4948 KB |
Output is correct |
46 |
Correct |
8 ms |
5052 KB |
Output is correct |
47 |
Correct |
27 ms |
5084 KB |
Output is correct |
48 |
Correct |
10 ms |
5204 KB |
Output is correct |
49 |
Correct |
11 ms |
5204 KB |
Output is correct |
50 |
Correct |
20 ms |
5204 KB |
Output is correct |
51 |
Correct |
10 ms |
5332 KB |
Output is correct |
52 |
Correct |
10 ms |
5332 KB |
Output is correct |
53 |
Correct |
23 ms |
5332 KB |
Output is correct |
54 |
Correct |
11 ms |
5588 KB |
Output is correct |
55 |
Correct |
12 ms |
5588 KB |
Output is correct |
56 |
Correct |
28 ms |
5588 KB |
Output is correct |
57 |
Correct |
12 ms |
5844 KB |
Output is correct |
58 |
Correct |
13 ms |
5844 KB |
Output is correct |
59 |
Correct |
30 ms |
5844 KB |
Output is correct |
60 |
Correct |
14 ms |
5972 KB |
Output is correct |
61 |
Correct |
15 ms |
5972 KB |
Output is correct |
62 |
Correct |
37 ms |
5972 KB |
Output is correct |
63 |
Correct |
72 ms |
5972 KB |
Output is correct |
64 |
Correct |
115 ms |
5972 KB |
Output is correct |
65 |
Correct |
124 ms |
5972 KB |
Output is correct |
66 |
Correct |
92 ms |
6032 KB |
Output is correct |
67 |
Correct |
83 ms |
6024 KB |
Output is correct |
68 |
Correct |
34 ms |
5960 KB |
Output is correct |
69 |
Correct |
25 ms |
5972 KB |
Output is correct |
70 |
Correct |
26 ms |
5964 KB |
Output is correct |
71 |
Correct |
24 ms |
5972 KB |
Output is correct |
72 |
Correct |
20 ms |
5972 KB |
Output is correct |
73 |
Correct |
25 ms |
6228 KB |
Output is correct |
74 |
Correct |
44 ms |
6228 KB |
Output is correct |
75 |
Correct |
40 ms |
6228 KB |
Output is correct |
76 |
Correct |
39 ms |
6228 KB |
Output is correct |
77 |
Correct |
37 ms |
6232 KB |
Output is correct |
78 |
Correct |
53 ms |
6276 KB |
Output is correct |
79 |
Correct |
38 ms |
6300 KB |
Output is correct |
80 |
Correct |
41 ms |
6284 KB |
Output is correct |
81 |
Correct |
44 ms |
6180 KB |
Output is correct |
82 |
Correct |
40 ms |
6272 KB |
Output is correct |
83 |
Correct |
42 ms |
6228 KB |
Output is correct |
84 |
Correct |
39 ms |
6228 KB |
Output is correct |
85 |
Correct |
39 ms |
6228 KB |
Output is correct |
86 |
Correct |
45 ms |
6228 KB |
Output is correct |
87 |
Correct |
55 ms |
6248 KB |
Output is correct |
88 |
Correct |
45 ms |
6192 KB |
Output is correct |
89 |
Correct |
44 ms |
6192 KB |
Output is correct |
90 |
Correct |
43 ms |
6100 KB |
Output is correct |
91 |
Correct |
44 ms |
6100 KB |
Output is correct |
92 |
Correct |
40 ms |
6100 KB |
Output is correct |