제출 #501342

#제출 시각아이디문제언어결과실행 시간메모리
501342sidonMecho (IOI09_mecho)C++17
100 / 100
181 ms6168 KiB
#include <bits/stdc++.h>
using namespace std;
#define c(X, Y) (X * N + Y)
 
const int INF = 1e8;
 
const int di[4] = {-1, 1, 0, 0};
const int dj[4] = {0, 0, -1, 1};
 
int N, S, h[1<<20], m[1<<20], ans = -1, e, r;
 
string g[1<<10];
 
void BFS(char src, int *d) {
	fill(d, d + N * N, INF);
	queue<int> q;
	for(int i = 0; i < N; i++) 
		for(int j = 0; j < N; j++)
			if(g[i][j] == src && ans < h[c(i, j)])
				d[c(i, j)] = 0, q.push(c(i, j));
			else if(g[i][j] == 'D')
				e = c(i, j), g[i][j] = 30;
 
	while(!empty(q)) {
		int k = q.front(), i = k / N, j = k % N; q.pop();

		for(int s = 0; s < 4; s++) {
			int x = i + di[s], y = j + dj[s], z = c(x, y);
 
			if(min(x, y) < 0 || max(x, y) >= N || d[z] != INF) continue;
			if((g[x][y] & 1 || g[x][y] == r) && (!r || (d[k] + 1) / S + ans < h[z]))
				d[z] = d[k] + 1, q.push(z);
		}
	}
}
 
signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> S;
 
	for(int i = 0; i < N; i++)
		cin >> g[i];
 
	BFS('H', h);

	for(int b = r = 30; --b >= 0; ) {
		ans += 1 << b;
		BFS('M', m);
		if(m[e] == INF)
			ans -= 1 << b;
	}
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...