제출 #98200

#제출 시각아이디문제언어결과실행 시간메모리
98200SuperJavaMecho (IOI09_mecho)C++17
100 / 100
580 ms6520 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;
queue<pair<pair<int,int>,int>> q;
queue<pair<pair<int,int>,int>> q1;
 
void reset(){
	while(!q.empty()) q.pop();
	while(!q1.empty()) q1.pop();
	rep(i,0,n){
		rep(j,0,n){
			z[i][j] = b[i][j];
			if(z[i][j] == 1) q1.push({{i,j},0});
			if(z[i][j] == 2) q.push({{i,j},0});
		}
	}
}
 
void simulate(int steps){
	if(steps == 0) return;
	while(!q1.empty()){
		if(q1.front().second == steps) break;
		auto t = q1.front(); q1.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 && !(nr == target.first && nc == target.second)){
					z[nr][nc] = 1;
					q1.push({{nr,nc},t.second+1});
				}
			}
		}
	}
}
 
int simulate2(int steps){
	if(q.size() == 0) return -1;
	while(!q.empty()){
		if(q.front().second == steps) break;
		auto t = q.front(); q.pop();
		int r = t.first.first;
		int c = t.first.second;
		if(z[r][c] != 2) continue;
		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;
					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;
		rep(j,0,n){
			if(q[j] == 'D') target = {i,j};
			if(q[j] == 'T') b[i][j] = -1;
			if(q[j] == 'H') b[i][j] = 1;
			if(q[j] == 'M') b[i][j] = 2;
		}
	}
	int l = 0,r = n*n,best = -1;
	while(l <= r){
		int mid = (l+r)/2;
		reset();
		int we = mid;
		int ew = s;
		simulate(we++);
		bool good = false;
		while(simulate2(ew) > 0){
			if(z[target.first][target.second] == 2){
				good = true;
				break;
			}
			ew += s;
			simulate(we++);
		}
		if(good){
			best = mid;
			l = mid+1;
		}else{
			r = mid-1;
		}
	}
	cout << best;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...