#include <bits/stdc++.h>
#define lli long long
#define ld long double
#define tmt angel
using namespace std;
const int maxn = 802;
int vis[maxn][maxn], n, m;
char a[maxn][maxn];
int tim[maxn][maxn];
int hi, hj, sti, stj;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
deque<pair<pair<int, int>, int>> s;
bool flag = false;
void gocheck(int i, int j, int cost)
{
if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1 || a[i][j] == 'D') return;
vis[i][j] = 1;
tim[i][j] = cost;
s.push_back({{i, j}, cost});
}
void spread(int i, int j, int cost, int step)
{
if(vis[hi][hj] == 1) return;
if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1 || tim[i][j] < cost) return;
vis[i][j] = 1;
// cout << i << " " << j << " " << cost << " " << step << '\n';
if(vis[hi][hj] == 1) return;
if(step == m)
{
//tim[1][i][j] = cost;
s.push_back({{i, j}, cost});
}
else
{
// tim[1][i][j] = cost;
for(int f = 0; f < 4; f++)
{
spread(i + x[f], j + y[f], cost, step + 1);
if(vis[hi][hj] == 1) break;
}
}
}
void Bfs()
{
while(!s.empty())
{
int i = s.front().first.first;
int j = s.front().first.second;
int c = s.front().second;
s.pop_front();
for(int f = 0; f < 4; f++)
{
gocheck(i + x[f], j + y[f], c+1);
}
}
}
bool checked(int st)
{
s.clear();
flag = false;
// if(st == 0) s.push_back({ {sti, stj},{2, 0} });
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
vis[i][j] = a[i][j] == 'G' || a[i][j] == 'D' ? 0 : 1;
}
}
s.push_back({{sti, stj}, st});
while(!s.empty() && vis[hi][hj] == 0)
{
int i = s.front().first.first;
int j = s.front().first.second;
int c = s.front().second;
s.pop_front();
for(int f = 0; f < 4; f++)
{
spread(i + x[f], j + y[f], c+1, 1);
if(vis[hi][hj] == 1) break;
}
}
return vis[hi][hj] == 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen(".INP", "r" , stdin);
// freopen(".OUT", "w" , stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
if(a[i][j] == 'M')
{
vis[i][j] = 1;
sti = i;
stj = j;
}
else vis[i][j] = a[i][j] == 'G' ? 0 : 1;
if(a[i][j] == 'D')
{
hi = i;
hj = j;
}
if(a[i][j] == 'H')
{
s.push_back({{i, j}, 0});
vis[i][j] = 1;
}
}
}
Bfs();
tim[hi][hj] = 1e18;
//cout << checked(1);
int low = 0, high = n, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(checked(mid)) low = mid+1;
else high = mid - 1;
}
cout << high;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:125:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
125 | tim[hi][hj] = 1e18;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
2392 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Incorrect |
83 ms |
6320 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Incorrect |
1 ms |
2516 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
2516 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2508 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
4560 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
4564 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
4700 KB |
Output isn't correct |
34 |
Incorrect |
4 ms |
4700 KB |
Output isn't correct |
35 |
Correct |
19 ms |
4832 KB |
Output is correct |
36 |
Incorrect |
5 ms |
4956 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
4912 KB |
Output isn't correct |
38 |
Correct |
23 ms |
5016 KB |
Output is correct |
39 |
Incorrect |
6 ms |
4956 KB |
Output isn't correct |
40 |
Incorrect |
7 ms |
4956 KB |
Output isn't correct |
41 |
Correct |
31 ms |
5212 KB |
Output is correct |
42 |
Incorrect |
8 ms |
5212 KB |
Output isn't correct |
43 |
Incorrect |
8 ms |
5212 KB |
Output isn't correct |
44 |
Correct |
39 ms |
5424 KB |
Output is correct |
45 |
Incorrect |
8 ms |
5464 KB |
Output isn't correct |
46 |
Incorrect |
9 ms |
5468 KB |
Output isn't correct |
47 |
Correct |
46 ms |
5832 KB |
Output is correct |
48 |
Incorrect |
11 ms |
5564 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
5724 KB |
Output isn't correct |
50 |
Correct |
55 ms |
5832 KB |
Output is correct |
51 |
Incorrect |
13 ms |
5724 KB |
Output isn't correct |
52 |
Incorrect |
13 ms |
5952 KB |
Output isn't correct |
53 |
Correct |
69 ms |
6152 KB |
Output is correct |
54 |
Incorrect |
17 ms |
5980 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
5980 KB |
Output isn't correct |
56 |
Correct |
82 ms |
6320 KB |
Output is correct |
57 |
Incorrect |
17 ms |
6228 KB |
Output isn't correct |
58 |
Incorrect |
16 ms |
6236 KB |
Output isn't correct |
59 |
Correct |
92 ms |
6492 KB |
Output is correct |
60 |
Incorrect |
17 ms |
6488 KB |
Output isn't correct |
61 |
Incorrect |
18 ms |
6408 KB |
Output isn't correct |
62 |
Correct |
101 ms |
6764 KB |
Output is correct |
63 |
Correct |
82 ms |
6492 KB |
Output is correct |
64 |
Incorrect |
115 ms |
6492 KB |
Output isn't correct |
65 |
Correct |
115 ms |
6488 KB |
Output is correct |
66 |
Incorrect |
99 ms |
6488 KB |
Output isn't correct |
67 |
Correct |
85 ms |
6492 KB |
Output is correct |
68 |
Correct |
40 ms |
6588 KB |
Output is correct |
69 |
Correct |
42 ms |
6488 KB |
Output is correct |
70 |
Correct |
32 ms |
6492 KB |
Output is correct |
71 |
Correct |
33 ms |
6488 KB |
Output is correct |
72 |
Incorrect |
48 ms |
6492 KB |
Output isn't correct |
73 |
Incorrect |
63 ms |
6744 KB |
Output isn't correct |
74 |
Incorrect |
70 ms |
6748 KB |
Output isn't correct |
75 |
Incorrect |
67 ms |
6988 KB |
Output isn't correct |
76 |
Incorrect |
57 ms |
6900 KB |
Output isn't correct |
77 |
Incorrect |
69 ms |
6748 KB |
Output isn't correct |
78 |
Correct |
74 ms |
6992 KB |
Output is correct |
79 |
Incorrect |
64 ms |
6744 KB |
Output isn't correct |
80 |
Incorrect |
73 ms |
7000 KB |
Output isn't correct |
81 |
Incorrect |
65 ms |
6748 KB |
Output isn't correct |
82 |
Incorrect |
63 ms |
6872 KB |
Output isn't correct |
83 |
Incorrect |
72 ms |
6748 KB |
Output isn't correct |
84 |
Incorrect |
76 ms |
6500 KB |
Output isn't correct |
85 |
Incorrect |
72 ms |
6736 KB |
Output isn't correct |
86 |
Incorrect |
71 ms |
6736 KB |
Output isn't correct |
87 |
Incorrect |
83 ms |
6748 KB |
Output isn't correct |
88 |
Incorrect |
74 ms |
6744 KB |
Output isn't correct |
89 |
Incorrect |
89 ms |
6792 KB |
Output isn't correct |
90 |
Incorrect |
80 ms |
6784 KB |
Output isn't correct |
91 |
Incorrect |
82 ms |
6760 KB |
Output isn't correct |
92 |
Incorrect |
78 ms |
6632 KB |
Output isn't correct |