#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
const ll MOD = 1e9 + 7;
const int INF = 1e9 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)
// HERE IS THE SOLUTION
const int N = 801;
int n , s;
char grid[N][N];
vi xx = {1 , -1 ,0 , 0};
vi yy = {0 , 0 , 1 , -1};
vector<vi> dBees , dMecho;
int mi , mj , di , dj;
bool notinside(int i , int j)
{
return !(i<n && j< n && j>=0&&i>=0);
}
vector<vi> modifiedBfs(pii strt , int time)
{
vector<vi> dist(n , vi(n , INF));
queue<pii> q;
q.push(strt);
dist[strt.fi][strt.se] = 0;
while(!q.empty())
{
pii cur = q.front();
int i = cur.fi , j = cur.se;
q.pop();
fore( k , 4)
{
int ni = i + xx[k] , nj = j + yy[k];
int minutes = (dist[i][j] + 1)/s;
if(notinside(ni , nj)||grid[ni][nj] == 'T' || grid[ni][nj] == 'H' || grid[ni][nj] == 'D')continue;
if(dist[ni][nj] > dist[i][j] + 1 && dBees[ni][nj] > minutes + time )
{
dist[ni][nj] = dist[i][j] + 1;
q.push({ni , nj});
}
}
}
return dist;
}
vector<vi> bfs(vii cur , bool acc)
{
vector<vi> dist(n , vi(n , INF));
queue<pii> q;
for(auto x : cur)
{
dist[x.fi][x.se] = 0;
q.push(x);
}
while(!q.empty())
{
pii node = q.front();
int i = node.fi , j = node.se;
q.pop();
fore(k, 4)
{
int ni = i + xx[k] , nj = j + yy[k];
if(notinside(ni , nj)||grid[ni][nj] == 'T' || grid[ni][nj] == 'H' || grid[ni][nj] == 'D')continue;
if(dist[ni][nj] > dist[i][j] + 1)
{
dist[ni][nj] = dist[i][j] + 1;
q.push({ni , nj});
}
}
}
return dist;
}
bool check(int HoneyTime)
{
if(HoneyTime >= dBees[mi][mj])
return 0;
dMecho = modifiedBfs({mi , mj} , HoneyTime);
fore(k , 4)
{
if(!notinside(xx[k] + di , yy[k] + dj) && dMecho[xx[k] + di][yy[k] +dj] != INF)
return 1;
}
return 0;
}
int main()
{
cin.tie(0);
cout.tie(0);
boost;
cin>>n>>s;
vii MechoV , beesV;
fore(i , n)
{
fore(j , n)
{
cin>>grid[i][j];
char c = grid[i][j];
if(c == 'M') {
mi = i , mj = j;
MechoV.pb({i, j});
}
if(c == 'H')
{
beesV.pb({i , j});
}
if(c == 'D')
di = i , dj = j;
}
}
dBees = bfs(beesV ,1);
int l = -1, r = n*n + 1;
while(l + 1 < r)
{
int mid = (l + r)/2;
if(check(mid))
{
l = mid;
}
else
{
r = mid;
}
}
cout<<l<<nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
75 ms |
9184 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
5 ms |
2200 KB |
Output is correct |
34 |
Correct |
5 ms |
2192 KB |
Output is correct |
35 |
Correct |
18 ms |
2128 KB |
Output is correct |
36 |
Correct |
6 ms |
2644 KB |
Output is correct |
37 |
Correct |
6 ms |
2644 KB |
Output is correct |
38 |
Correct |
24 ms |
2800 KB |
Output is correct |
39 |
Correct |
7 ms |
3292 KB |
Output is correct |
40 |
Correct |
8 ms |
3292 KB |
Output is correct |
41 |
Correct |
32 ms |
3296 KB |
Output is correct |
42 |
Correct |
9 ms |
3944 KB |
Output is correct |
43 |
Correct |
10 ms |
3964 KB |
Output is correct |
44 |
Correct |
39 ms |
3956 KB |
Output is correct |
45 |
Correct |
16 ms |
4652 KB |
Output is correct |
46 |
Correct |
11 ms |
4656 KB |
Output is correct |
47 |
Correct |
44 ms |
4644 KB |
Output is correct |
48 |
Correct |
21 ms |
5452 KB |
Output is correct |
49 |
Correct |
13 ms |
5464 KB |
Output is correct |
50 |
Correct |
60 ms |
5548 KB |
Output is correct |
51 |
Correct |
16 ms |
6248 KB |
Output is correct |
52 |
Correct |
16 ms |
6252 KB |
Output is correct |
53 |
Correct |
67 ms |
6256 KB |
Output is correct |
54 |
Correct |
18 ms |
7172 KB |
Output is correct |
55 |
Correct |
22 ms |
7192 KB |
Output is correct |
56 |
Correct |
93 ms |
7184 KB |
Output is correct |
57 |
Correct |
20 ms |
8148 KB |
Output is correct |
58 |
Correct |
21 ms |
8136 KB |
Output is correct |
59 |
Correct |
110 ms |
8192 KB |
Output is correct |
60 |
Correct |
24 ms |
9176 KB |
Output is correct |
61 |
Correct |
23 ms |
9172 KB |
Output is correct |
62 |
Correct |
103 ms |
9188 KB |
Output is correct |
63 |
Correct |
102 ms |
9168 KB |
Output is correct |
64 |
Correct |
169 ms |
9300 KB |
Output is correct |
65 |
Correct |
126 ms |
9172 KB |
Output is correct |
66 |
Correct |
102 ms |
9192 KB |
Output is correct |
67 |
Correct |
101 ms |
9204 KB |
Output is correct |
68 |
Correct |
42 ms |
9180 KB |
Output is correct |
69 |
Correct |
41 ms |
9168 KB |
Output is correct |
70 |
Correct |
32 ms |
9212 KB |
Output is correct |
71 |
Correct |
38 ms |
9208 KB |
Output is correct |
72 |
Correct |
27 ms |
9152 KB |
Output is correct |
73 |
Correct |
28 ms |
6608 KB |
Output is correct |
74 |
Correct |
51 ms |
9216 KB |
Output is correct |
75 |
Correct |
63 ms |
9204 KB |
Output is correct |
76 |
Correct |
67 ms |
9204 KB |
Output is correct |
77 |
Correct |
62 ms |
9220 KB |
Output is correct |
78 |
Correct |
70 ms |
9220 KB |
Output is correct |
79 |
Correct |
44 ms |
9212 KB |
Output is correct |
80 |
Correct |
57 ms |
9224 KB |
Output is correct |
81 |
Correct |
68 ms |
9220 KB |
Output is correct |
82 |
Correct |
55 ms |
9212 KB |
Output is correct |
83 |
Correct |
91 ms |
9140 KB |
Output is correct |
84 |
Correct |
63 ms |
9224 KB |
Output is correct |
85 |
Correct |
70 ms |
9216 KB |
Output is correct |
86 |
Correct |
67 ms |
9208 KB |
Output is correct |
87 |
Correct |
74 ms |
9220 KB |
Output is correct |
88 |
Correct |
76 ms |
9204 KB |
Output is correct |
89 |
Correct |
91 ms |
9200 KB |
Output is correct |
90 |
Correct |
94 ms |
9204 KB |
Output is correct |
91 |
Correct |
87 ms |
9208 KB |
Output is correct |
92 |
Correct |
81 ms |
9204 KB |
Output is correct |