# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
470807 | Yazan_Alattar | Mecho (IOI09_mecho) | C++14 | 266 ms | 9136 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 <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
queue < pair <int,int> > tq;
pair <int,int> st, en;
int n, s, d[M][M], dist[M][M];
char a[M][M];
int main()
{
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> s;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
if(a[i][j] == 'H') tq.push({i, j}), d[i][j] = s;
if(a[i][j] == 'D') en = {i, j};
if(a[i][j] == 'M') st = {i, j};
}
}
while(!tq.empty()){
int x = tq.front().S;
int y = tq.front().F;
tq.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || a[ny][nx] == 'D' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
d[ny][nx] = d[y][x] + s;
tq.push({ny, nx});
}
}
d[en.F][en.S] = 1e9;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << d[i][j] << " ";
// cout << endl;
// }
// cout << endl;
queue < pair < pair <int,int>, int > > q;
int l = 0, r = 1e4;
while(l < r){
memset(dist, 0, sizeof dist);
int mid = (l + r) / 2;
bool check = 0;
q.push({st, 1e9});
dist[st.F][st.S] = 1;
while(!q.empty()){
int x = q.front().F.S;
int y = q.front().F.F;
int cost = q.front().S;
q.pop();
// if(mid == 1) cout << x << " " << y << " " << cost << " " << dist[y][x] << endl;
if(y == en.F && x == en.S){
check = 1;
continue;
}
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
int ncost = min(cost, (d[ny][nx] - s - dist[y][x]) / s);
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || ncost < mid || dist[ny][nx] || ny > n || nx > n || ny < 1 || nx < 1) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({{ny, nx}, ncost});
}
}
// if(mid == 4) for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
// cout << endl;
// }
if(check) l = mid + 1;
else r = mid;
}
cout << l - 1 << endl;
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |