//#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 calc(int x)
{
int ans=0;
while(x)
{
ans += x%10;
x/=10;
}
return ans;
}
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};
}
}
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 l=0,r=n*n;
while(l<=r)
{
int time = (l+r)/2;
queue<pii> q;
vector<vi> dbear(n+2,vi(n+2,INF));
q.push(bear);
dbear[bear.f][bear.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) && 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:81:15: 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]
81 | for(int i=0;i<hives.size();i++)
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1292 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 |
1368 KB |
Output is correct |
7 |
Correct |
93 ms |
12356 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1444 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Incorrect |
1 ms |
1372 KB |
Output isn't correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1468 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
8 ms |
3524 KB |
Output is correct |
34 |
Correct |
8 ms |
3580 KB |
Output is correct |
35 |
Correct |
23 ms |
3580 KB |
Output is correct |
36 |
Correct |
10 ms |
4188 KB |
Output is correct |
37 |
Correct |
14 ms |
4192 KB |
Output is correct |
38 |
Correct |
26 ms |
4240 KB |
Output is correct |
39 |
Correct |
13 ms |
4892 KB |
Output is correct |
40 |
Correct |
16 ms |
4956 KB |
Output is correct |
41 |
Correct |
38 ms |
4908 KB |
Output is correct |
42 |
Correct |
17 ms |
5740 KB |
Output is correct |
43 |
Correct |
18 ms |
5736 KB |
Output is correct |
44 |
Correct |
48 ms |
5732 KB |
Output is correct |
45 |
Correct |
24 ms |
6576 KB |
Output is correct |
46 |
Correct |
21 ms |
6616 KB |
Output is correct |
47 |
Correct |
56 ms |
6608 KB |
Output is correct |
48 |
Correct |
24 ms |
7620 KB |
Output is correct |
49 |
Correct |
30 ms |
7568 KB |
Output is correct |
50 |
Correct |
67 ms |
7588 KB |
Output is correct |
51 |
Correct |
30 ms |
8668 KB |
Output is correct |
52 |
Correct |
31 ms |
8692 KB |
Output is correct |
53 |
Correct |
84 ms |
8652 KB |
Output is correct |
54 |
Correct |
34 ms |
9792 KB |
Output is correct |
55 |
Correct |
35 ms |
9776 KB |
Output is correct |
56 |
Correct |
109 ms |
9744 KB |
Output is correct |
57 |
Correct |
39 ms |
11080 KB |
Output is correct |
58 |
Correct |
38 ms |
11032 KB |
Output is correct |
59 |
Correct |
109 ms |
10988 KB |
Output is correct |
60 |
Correct |
61 ms |
12300 KB |
Output is correct |
61 |
Correct |
46 ms |
12332 KB |
Output is correct |
62 |
Correct |
126 ms |
12268 KB |
Output is correct |
63 |
Correct |
114 ms |
12276 KB |
Output is correct |
64 |
Correct |
196 ms |
12304 KB |
Output is correct |
65 |
Correct |
168 ms |
12240 KB |
Output is correct |
66 |
Correct |
127 ms |
12344 KB |
Output is correct |
67 |
Correct |
124 ms |
12320 KB |
Output is correct |
68 |
Correct |
67 ms |
12368 KB |
Output is correct |
69 |
Correct |
69 ms |
12412 KB |
Output is correct |
70 |
Correct |
74 ms |
12400 KB |
Output is correct |
71 |
Correct |
78 ms |
12300 KB |
Output is correct |
72 |
Incorrect |
53 ms |
12356 KB |
Output isn't correct |
73 |
Incorrect |
58 ms |
12412 KB |
Output isn't correct |
74 |
Correct |
78 ms |
12388 KB |
Output is correct |
75 |
Correct |
100 ms |
12420 KB |
Output is correct |
76 |
Correct |
97 ms |
12352 KB |
Output is correct |
77 |
Correct |
74 ms |
12396 KB |
Output is correct |
78 |
Correct |
83 ms |
12336 KB |
Output is correct |
79 |
Correct |
67 ms |
12372 KB |
Output is correct |
80 |
Correct |
79 ms |
12368 KB |
Output is correct |
81 |
Correct |
73 ms |
12340 KB |
Output is correct |
82 |
Correct |
75 ms |
12360 KB |
Output is correct |
83 |
Correct |
89 ms |
12364 KB |
Output is correct |
84 |
Correct |
83 ms |
12380 KB |
Output is correct |
85 |
Correct |
92 ms |
12344 KB |
Output is correct |
86 |
Correct |
87 ms |
12332 KB |
Output is correct |
87 |
Correct |
88 ms |
12340 KB |
Output is correct |
88 |
Correct |
99 ms |
12340 KB |
Output is correct |
89 |
Correct |
96 ms |
12340 KB |
Output is correct |
90 |
Correct |
94 ms |
12352 KB |
Output is correct |
91 |
Correct |
93 ms |
12320 KB |
Output is correct |
92 |
Correct |
114 ms |
12336 KB |
Output is correct |