# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
709637 | aggrovector | Mecho (IOI09_mecho) | C++17 | 174 ms | 12260 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>
using namespace std;
long long n,s,i,j,d[805][805],x,y,vi[805][805],tl,tr,mid;
char a[805][805];
vector<pair<long long,long long>> h;
queue<pair<long long,long long>> q;
pair<long long,long long> st,e;
int main() {
cin >> n >> s;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
cin >> a[i][j];
if (a[i][j]=='H') {
h.push_back(make_pair(i,j));
}
if (a[i][j]=='M') {
st=make_pair(i,j);
}
if (a[i][j]=='D') {
e=make_pair(i,j);
}
}
}
for (i=0;i<h.size();i++) {
q.push(make_pair(h[i].first,h[i].second));
vi[h[i].first][h[i].second]=1;
}
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==0 && a[x-1][y]!='T') {
d[x-1][y]=d[x][y]+s;
vi[x-1][y]=1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==0 && a[x+1][y]!='T') {
d[x+1][y]=d[x][y]+s;
vi[x+1][y]=1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==0 && a[x][y-1]!='T') {
d[x][y-1]=d[x][y]+s;
vi[x][y-1]=1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==0 && a[x][y+1]!='T') {
d[x][y+1]=d[x][y]+s;
vi[x][y+1]=1;
q.push(make_pair(x,y+1));
}
}
// for (i=1;i<=n;i++) {
// for (j=1;j<=n;j++) {
// cout << d[i][j] << ' ';
// }
// cout << endl;
// }
// cout << endl;
tl=0;
tr=n*2;
while(tl<tr) {
mid=(tl+tr)/2+1;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
vi[i][j]=-1;
}
}
q.push(st);
vi[st.first][st.second]=0;
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==-1 && a[x-1][y]!='T' && d[x-1][y]>vi[x][y]+1+mid*s) {
vi[x-1][y]=vi[x][y]+1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==-1 && a[x+1][y]!='T' && d[x+1][y]>vi[x][y]+1+mid*s) {
vi[x+1][y]=vi[x][y]+1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==-1 && a[x][y-1]!='T' && d[x][y-1]>vi[x][y]+1+mid*s) {
vi[x][y-1]=vi[x][y]+1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==-1 && a[x][y+1]!='T' && d[x][y+1]>vi[x][y]+1+mid*s) {
vi[x][y+1]=vi[x][y]+1;
q.push(make_pair(x,y+1));
}
}
// cout << tl << ' ' << tr << ' ' << mid << endl;
// for (i=1;i<=n;i++) {
// for (j=1;j<=n;j++) {
// cout << vi[i][j] << ' ';
// }
// cout << endl;
// }
// cout << endl;
if (vi[e.first][e.second]==-1) {
tr=mid-1;
}
else {
tl=mid;
}
}
mid=tl;
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
vi[i][j]=-1;
}
}
q.push(st);
vi[st.first][st.second]=0;
while(!q.empty()) {
x=q.front().first;
y=q.front().second;
q.pop();
if (x>1 && vi[x-1][y]==-1 && a[x-1][y]!='T' && d[x-1][y]>vi[x][y]+1+mid*s) {
vi[x-1][y]=vi[x][y]+1;
q.push(make_pair(x-1,y));
}
if (x<n && vi[x+1][y]==-1 && a[x+1][y]!='T' && d[x+1][y]>vi[x][y]+1+mid*s) {
vi[x+1][y]=vi[x][y]+1;
q.push(make_pair(x+1,y));
}
if (y>1 && vi[x][y-1]==-1 && a[x][y-1]!='T' && d[x][y-1]>vi[x][y]+1+mid*s) {
vi[x][y-1]=vi[x][y]+1;
q.push(make_pair(x,y-1));
}
if (y<n && vi[x][y+1]==-1 && a[x][y+1]!='T' && d[x][y+1]>vi[x][y]+1+mid*s) {
vi[x][y+1]=vi[x][y]+1;
q.push(make_pair(x,y+1));
}
}
if (vi[e.first][e.second]==-1) {
cout << -1 << endl;
}
else {
cout << tl << endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |