# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
536087 | ssense | Mecho (IOI09_mecho) | C++14 | 141 ms | 7500 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define nax 200005
#define pb push_back
#define sc second
#define fr first
#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//ssense
int n, s;
vector<vector<char>> grid;
vector<pair<int, int>> hives;
int startx, starty, endx, endy;
vector<vector<int>> bees;
bool get(int mins)
{
vector<vector<bool>> vis;
vis.assign(n, vector<bool>(n, false));
queue<pair<pair<int, int>, int>> q;
if(bees[startx][starty] <= s*mins)
{
return false;
}
q.push({{startx, starty}, mins*s});
while(!q.empty())
{
int x = q.front().fr.fr;
int y = q.front().fr.sc;
int dist = q.front().sc;
q.pop();
if(x > 0 && !vis[x-1][y] && (grid[x-1][y] == 'G' || grid[x-1][y] == 'D') && dist+1 < bees[x-1][y])
{
vis[x-1][y] = true;
q.push({{x-1, y}, dist+1});
}
if(x < n-1 && !vis[x+1][y] && (grid[x+1][y] == 'G' || grid[x+1][y] == 'D') && dist+1 < bees[x+1][y])
{
vis[x+1][y] = true;
q.push({{x+1, y}, dist+1});
}
if(y > 0 && !vis[x][y-1] && (grid[x][y-1] == 'G' || grid[x][y-1] == 'D') && dist+1 < bees[x][y-1])
{
vis[x][y-1] = true;
q.push({{x, y-1}, dist+1});
}
if(y < n-1 && !vis[x][y+1] && (grid[x][y+1] == 'G' || grid[x][y+1] == 'D') && dist+1 < bees[x][y+1])
{
vis[x][y+1] = true;
q.push({{x, y+1}, dist+1});
}
}
return vis[endx][endy];
}
void solve()
{
cin >> n >> s;
grid.assign(n, vector<char>(n));
queue<pair<pair<int, int>, int>> q;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin >> grid[i][j];
if(grid[i][j] == 'M')
{
startx = i;
starty = j;
}
if(grid[i][j] == 'H')
{
q.push({{i, j}, 0});
}
if(grid[i][j] == 'D')
{
endx = i;
endy = j;
}
}
}
bees.assign(n, vector<int>(n, -1));
while(!q.empty())
{
int x = q.front().fr.fr;
int y = q.front().fr.sc;
int dist = q.front().sc;
q.pop();
if(x > 0 && bees[x-1][y] == -1 && (grid[x-1][y] == 'G' || grid[x-1][y] == 'M'))
{
bees[x-1][y]=dist+s;
q.push({{x-1, y}, dist+s});
}
if(x < n-1 && bees[x+1][y] == -1 && (grid[x+1][y] == 'G' || grid[x+1][y] == 'M'))
{
bees[x+1][y]=dist+s;
q.push({{x+1, y}, dist+s});
}
if(y > 0 && bees[x][y-1] == -1 && (grid[x][y-1] == 'G' || grid[x][y-1] == 'M'))
{
bees[x][y-1]=dist+s;
q.push({{x, y-1}, dist+s});
}
if(y < n-1 && bees[x][y+1] == -1 && (grid[x][y+1] == 'G' || grid[x][y+1] == 'M'))
{
bees[x][y+1]=dist+s;
q.push({{x, y+1},dist+s});
}
}
bees[endx][endy] = MX;
int l = 0, r = 1e6;
while(l <= r)
{
int mid = r+l>>1;
if(get(mid))
{
l = mid+1;
}
else
{
r = mid-1;
}
}
if(r >= 1e6-1)
{
cout << -1 << endl;
return;
}
cout << r << endl;
}
int32_t main(){
startt
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |