#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <stack>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <unordered_map>
#include <string>
#include <fstream>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Mat = vector<vector<ll>>;
const ll MOD = 2019201997LL;
const ll FACTOR1 = 2019201913LL;
const ll FACTOR2 = 2019201949LL;
const int MAXN = 1e3 + 11;
int n, s, l, r;
char t[MAXN][MAXN];
vector<pair<int,int>>hives;
int dx[]{ 0,0,-1,1 };
int dy[]{ 1,-1,0,0 };
int dist[MAXN][MAXN];
pair<int, int> bear,house;
bool within(int x, int y)
{
return (x >= 0 && y >= 0 && x < n&& y < n);
}
bool reached(int mecho, int bees)
{
return mecho / s < bees;
}
void action()
{
queue<pair<int, int>> q;
vector<vector<int>> vis(n, vector<int>(n));
for (auto h : hives)
{
q.push(h);
vis[h.first][h.second] = true;
}
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int d = 0; d < 4; d++)
{
int newX = x + dx[d];
int newY = y + dy[d];
if (!within(newX, newY) || (t[newX][newY] != 'G' && t[newX][newY] != 'M')
|| vis[newX][newY])
continue;
vis[newX][newY] = true;
q.push({ newX,newY });
dist[newX][newY] = dist[x][y] + 1;
}
}
}
bool check(int mins)
{
queue<pair<int,int>> q;
vector<vector<int>>bear_dist(n, vector<int>(n));
vector<vector<int>> vis(n, vector<int>(n));
q.push(bear);
vis[bear.first][bear.second] = true;
if (dist[bear.first][bear.second] <= mins)
q.pop();
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
//cout << x << " " << y << " " << bear_dist[x][y] <<" VS "<<
// dist[x][y]<<'\n';
for (int d = 0; d < 4; d++)
{
int newX = x + dx[d];
int newY = y + dy[d];
if (!within(newX, newY) || (t[newX][newY] != 'G' && t[newX][newY] != 'M') || (
!reached(bear_dist[x][y]+1,dist[newX][newY]-mins)) || vis[newX][newY])
continue;
vis[newX][newY] = true;
q.push({ newX,newY });
bear_dist[newX][newY] = bear_dist[x][y] + 1;
}
}
int flag = 0;
for (int d = 0; d < 4; d++)
{
int Hx = house.first + dx[d];
int Hy = house.second + dy[d];
if (!within(Hx, Hy) || (t[Hx][Hy] != 'G' && t[Hx][Hy]!='M'))
continue;
if (vis[Hx][Hy] && reached(bear_dist[Hx][Hy],dist[Hx][Hy]-mins))
flag = 1;
}
return flag;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
//freopen("lasers.in", "r", stdin);
//freopen("lasers.out", "w", stdout);
cin >> n >> s;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> t[i][j];
if (t[i][j] == 'H')
hives.push_back({ i,j });
if (t[i][j] == 'M')
bear = { i,j };
if (t[i][j] == 'D')
house = { i,j };
}
}
action();
int l = 0, r = n * n;
while (l < r)
{
int m = (l + r + 1) / 2;
if (check(m))
l = m;
else
r = m - 1;
}
if (l == n * n)
cout << -1 << '\n';
else
cout << l << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
100 ms |
10340 KB |
Output is correct |
8 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2904 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
4732 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
9 ms |
4940 KB |
Output is correct |
34 |
Correct |
8 ms |
5720 KB |
Output is correct |
35 |
Correct |
23 ms |
4952 KB |
Output is correct |
36 |
Correct |
11 ms |
5544 KB |
Output is correct |
37 |
Correct |
16 ms |
6184 KB |
Output is correct |
38 |
Correct |
32 ms |
5532 KB |
Output is correct |
39 |
Correct |
13 ms |
6132 KB |
Output is correct |
40 |
Correct |
14 ms |
6592 KB |
Output is correct |
41 |
Correct |
44 ms |
6072 KB |
Output is correct |
42 |
Correct |
17 ms |
6728 KB |
Output is correct |
43 |
Correct |
18 ms |
6980 KB |
Output is correct |
44 |
Correct |
55 ms |
6672 KB |
Output is correct |
45 |
Correct |
21 ms |
7400 KB |
Output is correct |
46 |
Correct |
28 ms |
7872 KB |
Output is correct |
47 |
Correct |
59 ms |
7556 KB |
Output is correct |
48 |
Correct |
24 ms |
8016 KB |
Output is correct |
49 |
Correct |
24 ms |
7956 KB |
Output is correct |
50 |
Correct |
73 ms |
7932 KB |
Output is correct |
51 |
Correct |
28 ms |
8524 KB |
Output is correct |
52 |
Correct |
29 ms |
8500 KB |
Output is correct |
53 |
Correct |
111 ms |
8500 KB |
Output is correct |
54 |
Correct |
32 ms |
9068 KB |
Output is correct |
55 |
Correct |
33 ms |
9100 KB |
Output is correct |
56 |
Correct |
103 ms |
9056 KB |
Output is correct |
57 |
Correct |
37 ms |
9836 KB |
Output is correct |
58 |
Correct |
38 ms |
9720 KB |
Output is correct |
59 |
Correct |
141 ms |
9848 KB |
Output is correct |
60 |
Correct |
45 ms |
10416 KB |
Output is correct |
61 |
Correct |
46 ms |
10668 KB |
Output is correct |
62 |
Correct |
134 ms |
10324 KB |
Output is correct |
63 |
Correct |
106 ms |
10440 KB |
Output is correct |
64 |
Correct |
196 ms |
10584 KB |
Output is correct |
65 |
Correct |
151 ms |
10352 KB |
Output is correct |
66 |
Correct |
125 ms |
10400 KB |
Output is correct |
67 |
Incorrect |
122 ms |
10356 KB |
Output isn't correct |
68 |
Correct |
61 ms |
10508 KB |
Output is correct |
69 |
Correct |
52 ms |
10364 KB |
Output is correct |
70 |
Correct |
51 ms |
10368 KB |
Output is correct |
71 |
Correct |
47 ms |
10448 KB |
Output is correct |
72 |
Correct |
40 ms |
10376 KB |
Output is correct |
73 |
Correct |
42 ms |
10392 KB |
Output is correct |
74 |
Correct |
76 ms |
10400 KB |
Output is correct |
75 |
Correct |
84 ms |
10392 KB |
Output is correct |
76 |
Correct |
77 ms |
10456 KB |
Output is correct |
77 |
Correct |
82 ms |
10372 KB |
Output is correct |
78 |
Incorrect |
82 ms |
10376 KB |
Output isn't correct |
79 |
Correct |
64 ms |
10392 KB |
Output is correct |
80 |
Correct |
68 ms |
10392 KB |
Output is correct |
81 |
Correct |
75 ms |
10360 KB |
Output is correct |
82 |
Correct |
67 ms |
10388 KB |
Output is correct |
83 |
Correct |
93 ms |
10368 KB |
Output is correct |
84 |
Correct |
100 ms |
10388 KB |
Output is correct |
85 |
Correct |
89 ms |
10356 KB |
Output is correct |
86 |
Correct |
94 ms |
10392 KB |
Output is correct |
87 |
Correct |
85 ms |
10396 KB |
Output is correct |
88 |
Correct |
105 ms |
10472 KB |
Output is correct |
89 |
Correct |
130 ms |
10372 KB |
Output is correct |
90 |
Correct |
102 ms |
10372 KB |
Output is correct |
91 |
Correct |
92 ms |
10376 KB |
Output is correct |
92 |
Correct |
93 ms |
10416 KB |
Output is correct |