//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
//#include <cstdio>
//#include <iostream>
using namespace std;
#define MAX 100000007
#define pb push_back
//#define mp make_pair
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>>
const int mod = 1e9+7;
const int INF = 1e18;
// myMap.begin()->first : key
// myMap.begin()->second : value
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;ans%=mod;a%=mod;b>>=1;}return ans%mod;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n,s;
vector<vector<char>> grid(1000,vector<char>(1000));
bool valid_sq(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n &&
(grid[x][y] == 'G' || grid[x][y] == 'M');
}
bool mecho_reached(int mecho_dis, int bees_dis) {
return mecho_dis / s < bees_dis;
}
void solve() {
cin >> n >> s;
pii bear,home;
vector<pii> hives;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin >> grid[i][j];
if(grid[i][j]=='H') hives.pb({i,j});
if(grid[i][j]=='M') bear = {i,j};
if(grid[i][j]=='D') home = {i,j};
}
}
int l=0,r=n*n;
while(l<=r)
{
vector<vector<int>> d(n+2,vector<int>(n+2,INF));
queue<pii> q;
for(int i=0;i<hives.size();i++)
{
q.push({hives[i]});
d[hives[i].f][hives[i].s]=0;
}
while(q.size())
{
int x = q.front().first, y = q.front().second;
q.pop();
for (int i=0;i<4;i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny) && d[nx][ny]==INF)
{
d[nx][ny] = d[x][y] + 1;
q.push({nx, ny});
}
}
}
int time = (l+r)/2;
// queue<pii> q;
q = queue<pii>();
vector<vi> dbear(n+2,vi(n+2,INF));
q.push(bear);
dbear[bear.f][bear.s]=0;
if(d[bear.f][bear.s]<=time) q.pop();
while(q.size())
{
int x = q.front().first, y = q.front().second;
q.pop();
for (int i=0;i<4;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if (valid_sq(nx, ny) && dbear[nx][ny]==INF &&mecho_reached(dbear[x][y]+1,d[nx][ny]-time))
{
q.push({nx, ny});
dbear[nx][ny] = dbear[x][y] + 1;
}
}
}
bool flag = false;
for (int i = 0; i < 4; i++)
{
int nx = home.f + dx[i], ny = home.s + dy[i];
if (valid_sq(nx, ny) &&mecho_reached(dbear[nx][ny],d[nx][ny]-time) && dbear[nx][ny]!=INF) flag = true;
}
if (flag) l = time + 1;
else r = time - 1;
}
cout << l-1 << endl;
return;
}
int32_t main() {
// freopen("lasers.in", "r", stdin);
// freopen("lasers.out", "w", stdout);
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int t=1;
// cin >> t;
while (t--) solve();
return 0;
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:68:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0;i<hives.size();i++)
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
317 ms |
12272 KB |
Output is correct |
8 |
Correct |
1 ms |
1460 KB |
Output is correct |
9 |
Correct |
1 ms |
1464 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1368 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1432 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1368 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
33 ms |
3568 KB |
Output is correct |
34 |
Correct |
30 ms |
3580 KB |
Output is correct |
35 |
Correct |
52 ms |
3552 KB |
Output is correct |
36 |
Correct |
43 ms |
4192 KB |
Output is correct |
37 |
Correct |
39 ms |
4244 KB |
Output is correct |
38 |
Correct |
66 ms |
4196 KB |
Output is correct |
39 |
Correct |
52 ms |
4900 KB |
Output is correct |
40 |
Correct |
58 ms |
4956 KB |
Output is correct |
41 |
Correct |
81 ms |
4920 KB |
Output is correct |
42 |
Correct |
64 ms |
5740 KB |
Output is correct |
43 |
Correct |
61 ms |
5732 KB |
Output is correct |
44 |
Correct |
96 ms |
5688 KB |
Output is correct |
45 |
Correct |
85 ms |
6592 KB |
Output is correct |
46 |
Correct |
72 ms |
6608 KB |
Output is correct |
47 |
Correct |
116 ms |
6604 KB |
Output is correct |
48 |
Correct |
85 ms |
7588 KB |
Output is correct |
49 |
Correct |
86 ms |
7600 KB |
Output is correct |
50 |
Correct |
138 ms |
7540 KB |
Output is correct |
51 |
Correct |
114 ms |
8824 KB |
Output is correct |
52 |
Correct |
113 ms |
8604 KB |
Output is correct |
53 |
Correct |
166 ms |
8716 KB |
Output is correct |
54 |
Correct |
128 ms |
9748 KB |
Output is correct |
55 |
Correct |
115 ms |
9748 KB |
Output is correct |
56 |
Correct |
203 ms |
9728 KB |
Output is correct |
57 |
Correct |
166 ms |
10960 KB |
Output is correct |
58 |
Correct |
166 ms |
10928 KB |
Output is correct |
59 |
Correct |
232 ms |
10940 KB |
Output is correct |
60 |
Correct |
205 ms |
12224 KB |
Output is correct |
61 |
Correct |
179 ms |
12256 KB |
Output is correct |
62 |
Correct |
278 ms |
12212 KB |
Output is correct |
63 |
Correct |
403 ms |
12216 KB |
Output is correct |
64 |
Correct |
487 ms |
12244 KB |
Output is correct |
65 |
Correct |
441 ms |
12248 KB |
Output is correct |
66 |
Correct |
417 ms |
12256 KB |
Output is correct |
67 |
Correct |
436 ms |
12232 KB |
Output is correct |
68 |
Correct |
367 ms |
12512 KB |
Output is correct |
69 |
Correct |
361 ms |
12248 KB |
Output is correct |
70 |
Correct |
348 ms |
12272 KB |
Output is correct |
71 |
Correct |
361 ms |
12224 KB |
Output is correct |
72 |
Correct |
322 ms |
12264 KB |
Output is correct |
73 |
Correct |
377 ms |
12292 KB |
Output is correct |
74 |
Correct |
299 ms |
12308 KB |
Output is correct |
75 |
Correct |
337 ms |
12508 KB |
Output is correct |
76 |
Correct |
327 ms |
12284 KB |
Output is correct |
77 |
Correct |
342 ms |
12320 KB |
Output is correct |
78 |
Correct |
329 ms |
12292 KB |
Output is correct |
79 |
Correct |
314 ms |
12528 KB |
Output is correct |
80 |
Correct |
291 ms |
12288 KB |
Output is correct |
81 |
Correct |
304 ms |
12188 KB |
Output is correct |
82 |
Correct |
294 ms |
12156 KB |
Output is correct |
83 |
Correct |
336 ms |
12172 KB |
Output is correct |
84 |
Correct |
313 ms |
12164 KB |
Output is correct |
85 |
Correct |
324 ms |
12156 KB |
Output is correct |
86 |
Correct |
338 ms |
12124 KB |
Output is correct |
87 |
Correct |
346 ms |
12192 KB |
Output is correct |
88 |
Correct |
319 ms |
12340 KB |
Output is correct |
89 |
Correct |
310 ms |
12168 KB |
Output is correct |
90 |
Correct |
394 ms |
12168 KB |
Output is correct |
91 |
Correct |
341 ms |
12140 KB |
Output is correct |
92 |
Correct |
398 ms |
12160 KB |
Output is correct |