제출 #730132

#제출 시각아이디문제언어결과실행 시간메모리
730132CutSandstoneMecho (IOI09_mecho)C++17
25 / 100
1091 ms11692 KiB
#include <bits/stdc++.h>
#define f first
#define s second
using ll = long long;
using namespace std;
const int inf = 1<<30;
int bees[800][800];
bool go[800][800];
int bfs[640000][3];
int p1, p2, len;
int ra, rb, rc;
pair<int, int> st, en;
pair<int, int> d4[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int N, S;
void push(int a, int b, int c){
	bfs[p2][0] = a;
	bfs[p2][1] = b;
	bfs[p2][2] = c;
	p2 = (p2+1)%640000;
	len++;
}
vector<int> poll(){
	vector<int> ret = {bfs[p1][0], bfs[p1][1], bfs[p1][2]};
	p1 = (p1+1)%640000;
	len--;
	return ret;
}
bool works(int t){
	p1 = p2 = len = 0;
	push(st.f, st.s, 0);
	while(len) {
		vector<int> curr = poll();
		for(pair<int, int> a: d4) {
			vector<int> next = {curr[0]+a.f, curr[1]+a.s, curr[2]+1};
			if(next[0] >= 0 && next[0] < N && next[1] >= 0 && next[1] < N && 
			go[next[0]][next[1]] && next[2]/S < bees[next[0]][next[1]]-t) {
				if(next[0] == en.f && next[1] == en.s) return 1;
				push(next[0], next[1], next[2]);
			}
		}
	}
	return 0;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	p1 = p2 = len = 0;
	cin >> N >> S;
	for(int i = 0; i<N; i++) for(int j = 0; j<N; j++) bees[i][j] = inf;
	for(int i = 0; i<N; i++) {
	    string c; cin >> c;
		for(int j = 0; j<N; j++) {
			go[i][j] = c[j] != 'T';
			if(c[j] == 'M') st = {i, j};
			if(c[j] == 'D') en = {i, j};
			if(c[j] == 'H') {
				push(i, j, 0);
				bees[i][j] = 0;
			}
		}
	}
	while(len) {
	    vector<int> curr = poll();
		int c = bees[curr[0]][curr[1]];
		for(pair<int, int> a: d4) {
		    vector<int> next = {curr[0]+a.f, curr[1]+a.s};
			if(next[0] >= 0 && next[1] >= 0 && next[0] < N && next[1] < N && 
			go[next[0]][next[1]] && bees[next[0]][next[1]] == inf && (next[0] != en.f || next[1] != en.s)) {
				bees[next[0]][next[1]] = c+1;
				push(next[0], next[1], 0);
			}
		}
	}
	int lo = -1, hi = bees[st.f][st.s]+1;
	while (lo < hi) {
		int mid = lo + (hi - lo + 1) / 2;
		if (works(mid)) lo = mid;
		else hi = mid - 1;
	}
	cout << lo;
}
#Verdict Execution timeMemoryGrader output
Fetching results...