#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std ;
const int N = 801 ;
const int T = 1000 ;
int n , s;
char a[N][N] ;
int dis[N][N] , vis[N][N] ;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
bool valid(int x , int y ) {
return x >= 0 && x < n && y >= 0 && y < n &&
(a[x][y] == 'G' || a[x][y] == 'M');}
void bfs(vector<int> startx , vector<int> starty ){
queue<pair<int,int>> q;
for (int i = 0; i < startx.size() ; i++)
{
q.push({startx[i] , starty[i] });
vis[startx[i]][starty[i]] = 1 ;
dis[startx[i]][starty[i]] = 0 ;
}
while(q.size()){
pair<int,int> p = q.front() ;
int x = p.first;
int y = p.second ;
q.pop() ;
for (int i = 0; i < 4; i++)
{
int newx = x+dx[i];
int newy = y+dy[i];
if(valid(newx , newy) && vis[newx][newy] == 0){
q.push({newx , newy });
dis[newx][newy] = dis[x][y]+1;
vis[newx][newy] = 1 ;
}
}
}
}
bool viss[N][N] ;int diss[N][N];
int homex , homey ;
bool bfss(int startx ,int starty , int TT ) {
viss[startx][starty] = 1 ;
diss[startx][starty] = 0 ;
queue<pair<int,int>> q;
q.push({startx , starty });
//if(dis[startx][starty] <= TT) { q.pop() ; }
while(q.size()){
auto &[x,y] = q.front() ;
q.pop();
for (int i = 0; i < 4; i++)
{
int newx = x+dx[i];
int newy = y+dy[i];
if(viss[newx][newy] ==0 && valid(newx, newy) && (dis[newx][newy]-TT) > (((diss[x][y]+1)/s) ) ) {
q.push( { newx, newy } );
diss[newx][newy] = diss[x][y]+1 ;
viss[newx][newy]= 1;
}
}
}
for (int i = 0; i < 4; i++)
{
int nx = homex+dx[i]; int ny=homey+dy[i] ;
if(valid(nx,ny)&& ( diss[nx][ny]/s ) < ( dis[nx][ny]-TT ) && viss[nx][ny] )return 1 ;
}
return 0 ;
}
void cls(){
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n ; j++)
{
viss[i][j]=vis[i][j]=0;
diss[i][j]=0 ;
dis[i][j]= 0 ;
}
}
}
main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> s ; int mx , my ;
vector<int> stx , sty ;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n ; j++)
{
cin >> a[i][j] ;
if(a[i][j]=='H'){
stx.push_back(i);
sty.push_back(j);
}else if(a[i][j]=='M'){mx=i;my=j;}else if(a[i][j]=='D'){homex=i;homey=j;}
}
}
int l =0 , r = n*n ;
while(l <= r )
{
cls();
int mid = (l+r)/2 ;
bfs(stx,sty);
if(bfss(mx,my,mid)) {
l = mid + 1;
}else r = mid - 1;
}
cout<<l-1<<endl;
return 0 ;
}
Compilation message
mecho.cpp: In function 'void bfs(std::vector<int>, std::vector<int>)':
mecho.cpp:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < startx.size() ; i++)
| ~~^~~~~~~~~~~~~~~
mecho.cpp: In function 'bool bfss(int, int, int)':
mecho.cpp:50:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | auto &[x,y] = q.front() ;
| ^
mecho.cpp: In function 'void cls()':
mecho.cpp:76:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
76 | viss[i][j]=vis[i][j]=0;
| ~~~~~~~~~^~
mecho.cpp: At global scope:
mecho.cpp:83:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
83 | main(){
| ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:104:8: warning: 'my' may be used uninitialized in this function [-Wmaybe-uninitialized]
104 | if(bfss(mx,my,mid)) {
| ~~~~^~~~~~~~~~~
mecho.cpp:104:8: warning: 'mx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6608 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
255 ms |
9200 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
14 |
Correct |
2 ms |
8852 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6496 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6620 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
8792 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8792 KB |
Output is correct |
28 |
Correct |
1 ms |
8796 KB |
Output is correct |
29 |
Correct |
1 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8804 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
19 ms |
8984 KB |
Output is correct |
34 |
Correct |
18 ms |
8800 KB |
Output is correct |
35 |
Correct |
35 ms |
8796 KB |
Output is correct |
36 |
Correct |
25 ms |
8796 KB |
Output is correct |
37 |
Correct |
24 ms |
9056 KB |
Output is correct |
38 |
Correct |
47 ms |
8792 KB |
Output is correct |
39 |
Correct |
35 ms |
9088 KB |
Output is correct |
40 |
Correct |
37 ms |
9056 KB |
Output is correct |
41 |
Correct |
62 ms |
9048 KB |
Output is correct |
42 |
Correct |
42 ms |
9308 KB |
Output is correct |
43 |
Correct |
36 ms |
9048 KB |
Output is correct |
44 |
Correct |
80 ms |
9052 KB |
Output is correct |
45 |
Correct |
51 ms |
9048 KB |
Output is correct |
46 |
Correct |
43 ms |
9052 KB |
Output is correct |
47 |
Correct |
89 ms |
9052 KB |
Output is correct |
48 |
Correct |
74 ms |
9308 KB |
Output is correct |
49 |
Correct |
54 ms |
9304 KB |
Output is correct |
50 |
Correct |
105 ms |
9048 KB |
Output is correct |
51 |
Correct |
69 ms |
9304 KB |
Output is correct |
52 |
Correct |
67 ms |
9372 KB |
Output is correct |
53 |
Correct |
135 ms |
9308 KB |
Output is correct |
54 |
Correct |
79 ms |
9304 KB |
Output is correct |
55 |
Correct |
75 ms |
9484 KB |
Output is correct |
56 |
Correct |
159 ms |
9304 KB |
Output is correct |
57 |
Correct |
90 ms |
9504 KB |
Output is correct |
58 |
Correct |
91 ms |
9560 KB |
Output is correct |
59 |
Correct |
175 ms |
9564 KB |
Output is correct |
60 |
Correct |
107 ms |
9816 KB |
Output is correct |
61 |
Correct |
96 ms |
9556 KB |
Output is correct |
62 |
Correct |
201 ms |
9644 KB |
Output is correct |
63 |
Correct |
296 ms |
9564 KB |
Output is correct |
64 |
Correct |
366 ms |
9564 KB |
Output is correct |
65 |
Correct |
330 ms |
9564 KB |
Output is correct |
66 |
Correct |
304 ms |
9564 KB |
Output is correct |
67 |
Correct |
297 ms |
9560 KB |
Output is correct |
68 |
Correct |
293 ms |
9564 KB |
Output is correct |
69 |
Correct |
307 ms |
9560 KB |
Output is correct |
70 |
Correct |
296 ms |
9556 KB |
Output is correct |
71 |
Correct |
311 ms |
9640 KB |
Output is correct |
72 |
Incorrect |
287 ms |
9564 KB |
Output isn't correct |
73 |
Incorrect |
243 ms |
9812 KB |
Output isn't correct |
74 |
Correct |
247 ms |
9972 KB |
Output is correct |
75 |
Correct |
275 ms |
9820 KB |
Output is correct |
76 |
Correct |
254 ms |
9956 KB |
Output is correct |
77 |
Correct |
266 ms |
9820 KB |
Output is correct |
78 |
Correct |
253 ms |
9820 KB |
Output is correct |
79 |
Correct |
289 ms |
9816 KB |
Output is correct |
80 |
Correct |
247 ms |
9884 KB |
Output is correct |
81 |
Correct |
282 ms |
9888 KB |
Output is correct |
82 |
Correct |
260 ms |
9816 KB |
Output is correct |
83 |
Correct |
314 ms |
9912 KB |
Output is correct |
84 |
Correct |
258 ms |
9696 KB |
Output is correct |
85 |
Correct |
282 ms |
9884 KB |
Output is correct |
86 |
Correct |
284 ms |
9908 KB |
Output is correct |
87 |
Correct |
265 ms |
9820 KB |
Output is correct |
88 |
Correct |
253 ms |
9820 KB |
Output is correct |
89 |
Correct |
262 ms |
9820 KB |
Output is correct |
90 |
Correct |
297 ms |
9816 KB |
Output is correct |
91 |
Correct |
318 ms |
9816 KB |
Output is correct |
92 |
Correct |
283 ms |
9852 KB |
Output is correct |