# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1084370 | PhamKhanhHuyen | Mecho (IOI09_mecho) | C++14 | 148 ms | 7160 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 ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
int n, s;
const int maxn = 1000;
const int inf = 1e9;
char a[maxn + 3][maxn + 3];
pii st, en;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool valid(int i, int j)
{
return (i >= 1 && j >= 1 && i <= n && j <= n);
}
bool reach(int x, int y)
{
return x/s < y;
}
int main()
{
// freopen("metro.in", "r", stdin);
// freopen("metro.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
vector<vector<int>>time(n + 3, vector<int>(n + 3, inf));
queue<pii>q;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
cin >> a[i][j];
if(a[i][j] == 'M') st.fi = i, st.se = j;
else if(a[i][j] == 'D')
{
en.fi = i, en.se = j;
}
else if(a[i][j] == 'H')
{
q.push({i, j});
time[i][j] = 0;
}
}
}
while(!q.empty())
{
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int k = 0; k < 4; k++)
{
int i = x + dx[k];
int j = y + dy[k];
if(valid(i, j) && (a[i][j] == 'G' || a[i][j] == 'M') && time[i][j] == inf)
{
q.push({i, j});
time[i][j] = time[x][y] + 1;
}
}
}
/*for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << time[i][j] << ' ';
} cout << '\n';
}*/
int l = 0, r = n * n, mid, res = 0;
while(l <= r)
{
mid = (l + r) / 2;
vector<vector<int>>mecho_time(n + 3, vector<int>(n + 3, inf));
mecho_time[st.fi][st.se] = 0;
q.push(st);
if(time[st.fi][st.se] <= mid){
q.pop();
}
while(!q.empty()){
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int k = 0; k < 4; k++)
{
int i = x + dx[k];
int j = y + dy[k];
if(valid(i, j) && (a[i][j] == 'G' || a[i][j] == 'M') && mecho_time[i][j] == inf && reach(mecho_time[x][y] + 1, time[i][j] - mid))
{
q.push({i, j});
mecho_time[i][j] = mecho_time[x][y] + 1;
}
}
}
bool Reach = false;
for(int i = 0; i < 4; i++)
{
int nx = en.fi + dx[i], ny = en.se + dy[i];
if(valid(nx, ny) && (a[nx][ny] == 'G' || a[nx][ny] == 'M') && reach(mecho_time[nx][ny], time[nx][ny] - mid))
{
Reach = true;
break;
}
}
if(Reach) {
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
if(res)cout << res;
else cout << -1;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |