# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
869547 | dwuy | Mecho (IOI09_mecho) | C++14 | 290 ms | 8096 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.
/// dwuy: _,\,,,_\,__,\,,_
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK ""
#define int long long
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 808;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, s;
string a[MX];
int d[MX][MX];
bool vist[MX][MX][2];
void nhap(){
cin >> n >> s;
for(int i=1; i<=n; i++){
cin >> a[i];
a[i] = ' ' + a[i];
}
}
void solve(){
memset(d, 0x3f, sizeof(d));
queue<pii> Q;
int xA = 0, yA = 0;
int xB = 0, yB = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(a[i][j]=='H'){
Q.push({i, j});
d[i][j] = 0;
}
if(a[i][j]=='M') xA = i, yA = j;
if(a[i][j]=='D') xB = i, yB = j;
}
}
while(Q.size()){
int x, y;
tie(x, y) = Q.front();
Q.pop();
for(int i=0; i<4; i++){
int u = x + dx[i];
int v = y + dy[i];
if(u<1 || u>n || v<1 || v>n || a[u][v]=='T' || a[u][v]=='D') continue;
if(d[u][v] == d[0][0]){
d[u][v] = d[x][y] + 1;
Q.push({u, v});
}
}
}
int res = -1;
for(int lo=0, hi=n*n; lo<=hi;){
int mid = (lo+hi)>>1;
if(d[xA][yA]<=mid) {hi = mid - 1; continue;}
memset(vist, 0, sizeof(vist));
vist[xA][yA][0] = vist[xA][yA][1] = 1;
queue<pair<pii, pii>> Q;
Q.push({{xA, yA}, {0, mid}});
while(Q.size()){
int x, y; tie(x, y) = Q.front().fi;
int t, st; tie(t, st) = Q.front().se;
Q.pop();
for(int i=0; i<4; i++){
int u = x + dx[i];
int v = y + dy[i];
if(u<1 || u>n || v<1 || v>n || a[u][v]=='T' || a[u][v]=='H') continue;
if(d[u][v] > st && t<s && !vist[u][v][0]) vist[u][v][0] = 1, Q.push({{u, v}, {t+1, st}});
if(d[u][v] > st + 1 && !vist[u][v][1]) vist[u][v][1] = 1, Q.push({{u, v}, {0, st + 1}});
}
}
if(vist[xB][yB][1]) res = mid, lo = mid + 1;
else hi = mid - 1;
}
cout << res;
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |