Submission #97902

#TimeUsernameProblemLanguageResultExecution timeMemory
97902SuperJavaMecho (IOI09_mecho)C++17
41 / 100
1089 ms13136 KiB
//fold
#ifndef KHALIL
#include <bits/stdc++.h>
#else
#include "header.h"
#endif
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng);}
//endfold
#define  N  (805)
#define MOD (1000000000l + 7l)
#define OO (1050000000)
#define OOL (1100000000000000000)

int b[N][N];
int z[N][N];
int n,s;

int dr[4] = {1,-1,0,0};
int dc[4] = {0,0,1,-1};

pair<int,int> target;

void reset(){
	rep(i,0,n){
		rep(j,0,n){
			z[i][j] = b[i][j];
		}
	}
}

void simulate(int steps){
	if(steps == 0) return;
	queue<pair<pair<int,int>,int>> q;
	rep(i,0,n){
		rep(j,0,n){
			if(z[i][j] == 1){
				q.push({{i,j},0});
			}
		}
	}
	while(!q.empty()){
		auto t = q.front(); q.pop();
		int r = t.first.first;
		int c = t.first.second;
		rep(i,0,4){
			int nr = r+dr[i];
			int nc = c+dc[i];
			if(nr >= 0 && nr < n && nc >= 0 && nc < n){
				if(z[nr][nc] != -1 && z[nr][nc] != 1 && mp(nr,nc) != target){
					z[nr][nc] = 1;
					if(t.second != steps-1){
						q.push({{nr,nc},t.second+1});
					}
				}
			}
		}
	}
}

int simulate2(int steps){
	queue<pair<pair<int,int>,int>> q;
	rep(i,0,n){
		rep(j,0,n){
			if(z[i][j] == 2){
				q.push({{i,j},0});
			}
		}
	}
	if(q.size() == 0) return -1;
	while(!q.empty()){
		auto t = q.front(); q.pop();
		int r = t.first.first;
		int c = t.first.second;
		rep(i,0,4){
			int nr = r+dr[i];
			int nc = c+dc[i];
			if(nr >= 0 && nr < n && nc >= 0 && nc < n){
				if(z[nr][nc] == 0){
					z[nr][nc] = 2;
					if(t.second != steps-1){
						q.push({{nr,nc},t.second+1});
					}
				}
			}
		}
	}
	return 1;
}

int main(){
	//fold
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << setprecision(10);
	//endfold
	cin >> n >> s;
	rep(i,0,n){
		string q;
		cin >> q;
		map<char,int> pw = {{'T',-1},{'G',0},
					{'H',1},{'M',2},{'D',0}};
		rep(j,0,n){
			if(q[j] == 'D') target = {i,j};
			b[i][j] = pw[q[j]];
		}
	}
	int l = 1,r = n*n/2,best = -1;
	while(l <= r){
		int mid = (l+r)/2;
		reset();
		simulate(mid);
		bool good = false;
		while(simulate2(s) > 0){
			if(z[target.first][target.second] == 2){
				good = true;
				break;
			}
			if(z[target.first][target.second] == 1){
				good = false;
				break;
			}
			simulate(1);
			if(z[target.first][target.second] == 1){
				good = false;
				break;
			}
		}
		if(good){
			best = mid;
			l = mid+1;
		}else{
			r = mid-1;
		}
	}
	cout << best;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...