#include<bits/stdc++.h>
using namespace std;
const int MAXN = 810;
const int INF = 1e9+7;
int dx[5] = {1,0,-1,0};
int dy[5] = {0,1,0,-1};
int inii, inij, desti, destj;
int n, s;
int distbees[MAXN][MAXN], distbear[MAXN][MAXN];
// 0 = grass
// 1 = bear
// 2 = bees
// 3 = trees
// 4 = dest
int grid[MAXN][MAXN];
bool bfsteste(int init){
if(init >= distbees[inii][inij] || init >= distbees[desti][destj]) return 0;
queue<pair<int, int > > q;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
distbear[i][j] = INF;
}
}
distbear[inii][inij] = 0;
q.push(make_pair(inii, inij));
while(!q.empty()){
int i = q.front().first;
int j = q.front().second;
q.pop();
for(int v = 0; v < 4; v++){
int ii = i + dx[v];
int jj = j + dy[v];
if(!ii || !jj || ii > n || jj > n || distbear[ii][jj] < INF || grid[ii][jj] == 3) continue;
if(init + (distbear[i][j] + 1)/s < distbees[ii][jj]){
q.push(make_pair(ii,jj));
distbear[ii][jj] = distbear[i][j] + 1;
}
}
}
//printf("%d\n", distbear[desti][destj] );
if(distbear[desti][destj] < INF) return true;
return false;
}
void bfsbees(){
queue<pair<int, int> > q;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
distbees[i][j] = INF;
if(grid[i][j] == 2){
//printf("%d %d\n", i, j);
q.push(make_pair(i,j));
distbees[i][j] = 0;
}
}
}
while(!q.empty()){
int i = q.front().first;
int j = q.front().second;
q.pop();
//printf("%lu\n",q.size() );
for(int v = 0; v < 4; v++){
int ii = i + dx[v];
int jj = j + dy[v];
if(!ii || !jj || ii > n || jj > n || distbees[ii][jj] < INF || grid[ii][jj] == 3 || grid[ii][jj] == 4){
continue;
}
q.push(make_pair(ii,jj));
distbees[ii][jj] = distbees[i][j] + 1;
}
}
//printf("%d\n", distbees[desti][destj] );
}
int main(){
scanf("%d %d", &n, &s);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
// 0 = grass
// 1 = bear
// 2 = bees
// 3 = trees
// 4 = dest
char c;
scanf(" %c", &c);
if(c == 'M'){
grid[i][j] = 1;
inii = i;
inij = j;
}
if(c == 'H') grid[i][j] = 2;
if(c == 'T') grid[i][j] = 3;
if(c == 'D'){
grid[i][j] = 4;
desti = i;
destj = j;
}
}
}
//printf("%d %d\n", desti, destj );
bfsbees();
int l = -1, r = n * n;
while(l + 1 < r){
int mid = (l+r)>>1;
if(bfsteste(mid)) l = mid;//, printf("oi\n");
else r = mid;
}
printf("%d\n",l );
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
79 | scanf("%d %d", &n, &s);
| ~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:88:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
131 ms |
7800 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
896 KB |
Output is correct |
26 |
Correct |
1 ms |
896 KB |
Output is correct |
27 |
Correct |
2 ms |
896 KB |
Output is correct |
28 |
Correct |
1 ms |
896 KB |
Output is correct |
29 |
Correct |
2 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
2 ms |
1024 KB |
Output is correct |
32 |
Correct |
1 ms |
1024 KB |
Output is correct |
33 |
Correct |
14 ms |
3584 KB |
Output is correct |
34 |
Correct |
14 ms |
3712 KB |
Output is correct |
35 |
Correct |
32 ms |
3712 KB |
Output is correct |
36 |
Correct |
19 ms |
4096 KB |
Output is correct |
37 |
Correct |
19 ms |
4224 KB |
Output is correct |
38 |
Correct |
41 ms |
4352 KB |
Output is correct |
39 |
Correct |
22 ms |
4608 KB |
Output is correct |
40 |
Correct |
23 ms |
4736 KB |
Output is correct |
41 |
Correct |
58 ms |
4728 KB |
Output is correct |
42 |
Correct |
27 ms |
5248 KB |
Output is correct |
43 |
Correct |
28 ms |
5376 KB |
Output is correct |
44 |
Correct |
69 ms |
5376 KB |
Output is correct |
45 |
Correct |
33 ms |
5760 KB |
Output is correct |
46 |
Correct |
35 ms |
5888 KB |
Output is correct |
47 |
Correct |
80 ms |
5880 KB |
Output is correct |
48 |
Correct |
39 ms |
6392 KB |
Output is correct |
49 |
Correct |
39 ms |
6392 KB |
Output is correct |
50 |
Correct |
97 ms |
6400 KB |
Output is correct |
51 |
Correct |
47 ms |
6904 KB |
Output is correct |
52 |
Correct |
49 ms |
6904 KB |
Output is correct |
53 |
Correct |
126 ms |
6904 KB |
Output is correct |
54 |
Correct |
52 ms |
7416 KB |
Output is correct |
55 |
Correct |
52 ms |
7416 KB |
Output is correct |
56 |
Correct |
149 ms |
7544 KB |
Output is correct |
57 |
Correct |
60 ms |
8056 KB |
Output is correct |
58 |
Correct |
62 ms |
8056 KB |
Output is correct |
59 |
Correct |
168 ms |
7988 KB |
Output is correct |
60 |
Correct |
66 ms |
8568 KB |
Output is correct |
61 |
Correct |
68 ms |
8568 KB |
Output is correct |
62 |
Correct |
196 ms |
8568 KB |
Output is correct |
63 |
Correct |
163 ms |
8568 KB |
Output is correct |
64 |
Correct |
266 ms |
8568 KB |
Output is correct |
65 |
Correct |
227 ms |
8568 KB |
Output is correct |
66 |
Correct |
183 ms |
8696 KB |
Output is correct |
67 |
Correct |
172 ms |
8568 KB |
Output is correct |
68 |
Correct |
95 ms |
8568 KB |
Output is correct |
69 |
Correct |
96 ms |
8568 KB |
Output is correct |
70 |
Correct |
82 ms |
8568 KB |
Output is correct |
71 |
Correct |
91 ms |
8572 KB |
Output is correct |
72 |
Correct |
80 ms |
8568 KB |
Output is correct |
73 |
Correct |
79 ms |
8672 KB |
Output is correct |
74 |
Correct |
119 ms |
8672 KB |
Output is correct |
75 |
Correct |
125 ms |
8708 KB |
Output is correct |
76 |
Correct |
109 ms |
8672 KB |
Output is correct |
77 |
Correct |
111 ms |
8628 KB |
Output is correct |
78 |
Correct |
129 ms |
8700 KB |
Output is correct |
79 |
Correct |
95 ms |
8700 KB |
Output is correct |
80 |
Correct |
107 ms |
8700 KB |
Output is correct |
81 |
Correct |
118 ms |
8700 KB |
Output is correct |
82 |
Correct |
109 ms |
8700 KB |
Output is correct |
83 |
Correct |
126 ms |
8732 KB |
Output is correct |
84 |
Correct |
115 ms |
8608 KB |
Output is correct |
85 |
Correct |
125 ms |
8736 KB |
Output is correct |
86 |
Correct |
124 ms |
8704 KB |
Output is correct |
87 |
Correct |
127 ms |
8640 KB |
Output is correct |
88 |
Correct |
126 ms |
8660 KB |
Output is correct |
89 |
Correct |
133 ms |
8864 KB |
Output is correct |
90 |
Correct |
128 ms |
8632 KB |
Output is correct |
91 |
Correct |
132 ms |
8620 KB |
Output is correct |
92 |
Correct |
136 ms |
8660 KB |
Output is correct |