#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 mtim[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;
int bvis[maxn][maxn];
bool valid_sq(int nx, int ny) {
return nx >= 0 && nx < n && ny >= 0 && ny < n &&
(a[nx][ny] == 'G' || a[nx][ny] == 'M');
}
void gocheck(int i, int j, int cost)
{
if(i < 1 || j < 1 || i > n || j > n || bvis[i][j] == 1 || a[i][j] == 'D' || a[i][j] == 'T') return;
bvis[i][j] = 1;
tim[i][j] = cost;
s.push_back({{i, j}, cost});
}
void spread(int i, int j, int cost, int st)
{
//if(vis[hi][hj] == 1) return;
if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1) return;
if(tim[i][j] - st > cost / m)
{
s.push_back({{i, j}, cost});
vis[i][j] = 1;
mtim[i][j] = cost;
}
}
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)
{
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;
mtim[i][j] = 0;
}
}
s.clear();
s.push_back({{sti, stj}, 0});
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++)
{
spread(i + x[f], j + y[f], c+1, st);
//if(vis[hi][hj] == 1) break;
}
}
// if(st == 0) s.push_back({ {sti, stj},{2, 0} });
for(int f = 0; f < 4; f++)
{
int nx = hi + x[f], ny = hj + y[f];
if(valid_sq(nx, ny) && vis[nx][ny] == 1 && mtim[nx][ny] / m < tim[nx][ny] - st) return true;
}
return false;
}
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' || a[i][j] == 'D' ? 0 : 1;
if(a[i][j] == 'D')
{
hi = i;
hj = j;
}
if(a[i][j] == 'H')
{
s.push_back({{i, j}, 0});
bvis[i][j] = 1;
}
}
}
Bfs();
//s.clear();
//cout << checked(1);
int low = 0, high = n*n, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(tim[sti][stj] <= mid) high = mid-1;
if(checked(mid)) low = mid +1;
else high = mid - 1;
}
cout << high;
/*for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << mtim[i][j] << " ";
}
cout << '\n';
}
cout << '\n';
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << tim[i][j] << " ";
}
cout << '\n';
}*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
7 |
Correct |
89 ms |
11364 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
8792 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6616 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
1 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
8924 KB |
Output is correct |
33 |
Correct |
7 ms |
10844 KB |
Output is correct |
34 |
Correct |
7 ms |
9820 KB |
Output is correct |
35 |
Incorrect |
20 ms |
10996 KB |
Output isn't correct |
36 |
Correct |
9 ms |
10844 KB |
Output is correct |
37 |
Correct |
11 ms |
9820 KB |
Output is correct |
38 |
Incorrect |
33 ms |
10972 KB |
Output isn't correct |
39 |
Correct |
10 ms |
10844 KB |
Output is correct |
40 |
Correct |
11 ms |
10072 KB |
Output is correct |
41 |
Incorrect |
34 ms |
11096 KB |
Output isn't correct |
42 |
Correct |
13 ms |
10844 KB |
Output is correct |
43 |
Correct |
14 ms |
10332 KB |
Output is correct |
44 |
Incorrect |
43 ms |
11068 KB |
Output isn't correct |
45 |
Correct |
16 ms |
10840 KB |
Output is correct |
46 |
Correct |
16 ms |
10416 KB |
Output is correct |
47 |
Incorrect |
56 ms |
11096 KB |
Output isn't correct |
48 |
Correct |
19 ms |
11100 KB |
Output is correct |
49 |
Correct |
19 ms |
10784 KB |
Output is correct |
50 |
Incorrect |
59 ms |
11100 KB |
Output isn't correct |
51 |
Correct |
20 ms |
11096 KB |
Output is correct |
52 |
Correct |
22 ms |
10796 KB |
Output is correct |
53 |
Incorrect |
70 ms |
11220 KB |
Output isn't correct |
54 |
Correct |
23 ms |
11180 KB |
Output is correct |
55 |
Correct |
25 ms |
11096 KB |
Output is correct |
56 |
Incorrect |
91 ms |
11100 KB |
Output isn't correct |
57 |
Correct |
27 ms |
11144 KB |
Output is correct |
58 |
Correct |
30 ms |
11372 KB |
Output is correct |
59 |
Incorrect |
106 ms |
11360 KB |
Output isn't correct |
60 |
Correct |
33 ms |
11600 KB |
Output is correct |
61 |
Correct |
33 ms |
11344 KB |
Output is correct |
62 |
Incorrect |
156 ms |
11344 KB |
Output isn't correct |
63 |
Incorrect |
90 ms |
11300 KB |
Output isn't correct |
64 |
Incorrect |
178 ms |
11600 KB |
Output isn't correct |
65 |
Incorrect |
158 ms |
11352 KB |
Output isn't correct |
66 |
Incorrect |
126 ms |
11352 KB |
Output isn't correct |
67 |
Correct |
96 ms |
11356 KB |
Output is correct |
68 |
Incorrect |
51 ms |
11604 KB |
Output isn't correct |
69 |
Incorrect |
56 ms |
11568 KB |
Output isn't correct |
70 |
Incorrect |
57 ms |
11612 KB |
Output isn't correct |
71 |
Incorrect |
44 ms |
11608 KB |
Output isn't correct |
72 |
Incorrect |
64 ms |
11612 KB |
Output isn't correct |
73 |
Correct |
47 ms |
11864 KB |
Output is correct |
74 |
Correct |
65 ms |
11960 KB |
Output is correct |
75 |
Correct |
82 ms |
11868 KB |
Output is correct |
76 |
Correct |
62 ms |
11864 KB |
Output is correct |
77 |
Correct |
62 ms |
11992 KB |
Output is correct |
78 |
Correct |
73 ms |
11944 KB |
Output is correct |
79 |
Correct |
54 ms |
11836 KB |
Output is correct |
80 |
Correct |
84 ms |
11868 KB |
Output is correct |
81 |
Correct |
68 ms |
11864 KB |
Output is correct |
82 |
Correct |
64 ms |
11928 KB |
Output is correct |
83 |
Correct |
85 ms |
11872 KB |
Output is correct |
84 |
Correct |
76 ms |
11708 KB |
Output is correct |
85 |
Correct |
76 ms |
11688 KB |
Output is correct |
86 |
Correct |
75 ms |
11884 KB |
Output is correct |
87 |
Correct |
74 ms |
11856 KB |
Output is correct |
88 |
Correct |
79 ms |
11608 KB |
Output is correct |
89 |
Correct |
96 ms |
11700 KB |
Output is correct |
90 |
Correct |
78 ms |
11612 KB |
Output is correct |
91 |
Correct |
80 ms |
11884 KB |
Output is correct |
92 |
Correct |
82 ms |
11612 KB |
Output is correct |