제출 #492199

#제출 시각아이디문제언어결과실행 시간메모리
492199EvangMecho (IOI09_mecho)C++17
100 / 100
442 ms6668 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb(x) push_back(x)
#define eb(args...) emplace_back(args)
#define ff first
#define ss second
#define DIE exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


const int N = 805;
int n, s, b[N][N], m[N][N];
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};
char a[N][N];
pi st;
vpi h;

bool good(int r, int c){
	return 0 <= r && r < n && 0 <= c && c < n;
}

bool ok(int t){
	dout(t);
	memset(b, -1, sizeof b);
	memset(m, -1, sizeof m);
	queue<pi> q;
	for(auto x: h){
		q.push(x);
		b[x.ff][x.ss] = 0;
	}

	while(!q.empty()){
		auto[r, c] = q.front();

		if(b[r][c]==t)
			break;
		q.pop();

		for(int i = 0; i < 4; ++i){
			int r2 = r + dr[i];
			int c2 = c + dc[i];
			if(good(r2, c2)&&(a[r2][c2]=='M'||a[r2][c2]=='G')&&b[r2][c2]==-1){
				b[r2][c2] = b[r][c] + 1;
				q.emplace(r2, c2);
			}
		}
	}

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j)
			clog << b[i][j] << sp;
		clog << el;
	}
	clog << el;
	#endif

	queue<pi> q2;  // mecho
	q2.push(st);
	m[st.ff][st.ss] = 0;
	int t2 = t;
	while(!q2.empty()){
		t2++;
		while(!q2.empty()){
			auto[r, c] = q2.front();
			if(m[r][c]==(t2-t)*s)
				break;
			q2.pop();
			if(~b[r][c])
				continue;
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];
				if(good(r2, c2)&&b[r2][c2]==-1&&m[r2][c2]==-1&&
						(a[r2][c2]=='G'||a[r2][c2]=='D')){
					if(a[r2][c2]=='D')
						return 1;
					m[r2][c2] = m[r][c] + 1;
					q2.emplace(r2, c2);
				}
			}
		}
		#ifdef _DEBUG
		clog << "Line " << __LINE__ << ":\n";
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j)
				clog << m[i][j] << sp;
			clog << el;
		}
		clog << el;
		#endif
		while(!q.empty()){
			auto[r, c] = q.front();

			if(b[r][c]==t2)
				break;
			q.pop();
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];

				if(good(r2, c2)&&b[r2][c2]==-1&&(a[r2][c2]=='G'||a[r2][c2]=='M')){
					b[r2][c2] = b[r][c] + 1;
					q.emplace(r2, c2);
				}
			}
		}
	}
	return 0;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cout << fixed; clog << fixed;
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("debug.txt", "w", stderr);
#endif

	cin >> n >> s;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j){
			cin >> a[i][j];
			if(a[i][j]=='M')
				st = {i, j};
			else if(a[i][j]=='H')
				h.eb(i, j);
		}

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j)
			clog << a[i][j];
		clog << el;
	}
	clog << el;
	#endif

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(auto[x, y]: h)
		clog << x << sp << y << el;
	#endif
	dout(st.ff);
	dout(st.ss);

	int ans = -1;
	for(int u = int(1e6); u > 0; u /= 2)
		while(ans + u <= int(1e6) && ok(ans + u))
			ans += u;
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...