Submission #1221249

#TimeUsernameProblemLanguageResultExecution timeMemory
1221249ChuanChenMecho (IOI09_mecho)C++20
37 / 100
199 ms6408 KiB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 808;

int n, s, fx, fy;
char g[MAXN][MAXN];

#define ff first
#define ss second
int dist[MAXN][MAXN], type[MAXN][MAXN];
//type: 0 -- grass, 1 -- mecho, 2 -- hive, 3 -- tree;

int dx[] = {0, 1, 0, -1},
	dy[] = {1, 0, -1, 0};
bool inbound(int nx, int ny){
	return 1<=nx && nx <= n && 1 <= ny && ny <= n;
}
queue<pair<int, int>> bq, mq;

void expandBee(){
	if(bq.empty()) return;
	int x = bq.front().ff, y = bq.front().ss;
	int dini = dist[x][y];
	while(!bq.empty()){
		int x = bq.front().ff, y = bq.front().ss;
		if(dist[x][y] > dini) break;
		bq.pop();
		for(int i = 0; i < 4; i++){
			int nx = x+dx[i], ny = y+dy[i];
			if(!inbound(nx, ny)) continue;
			if(type[nx][ny] > 1) continue;
			type[nx][ny] = 2;
			dist[nx][ny] = dist[x][y]+1;
			bq.push({nx, ny});
		}
	}
}

void expandMecho(){	
	if(mq.empty()) return;
	int x = mq.front().ff, y = mq.front().ss;
	int dini = dist[x][y];
	while(!mq.empty()){
		int x = mq.front().ff, y = mq.front().ss;
		if(dist[x][y] >= dini+s) break;
		mq.pop();
		if(type[x][y] > 1) continue;
		for(int i = 0; i < 4; i++){
			int nx = x+dx[i], ny = y+dy[i];
			if(!inbound(nx, ny)) continue;
			if(type[nx][ny] > 0) continue;
			type[nx][ny] = 1;
			dist[nx][ny] = dist[x][y]+1;
			mq.push({nx, ny});
		}
	}
}

bool guess(int m){
	while(mq.size()) mq.pop();
	while(bq.size()) bq.pop();
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			dist[i][j] = 0;
			if(g[i][j] == 'D') type[i][j] = 0;
			else if(g[i][j] == 'G') type[i][j] = 0;
			else if(g[i][j] == 'M') type[i][j] = 1, mq.push({i, j});
			else if(g[i][j] == 'H') type[i][j] = 2, bq.push({i, j});
			else if(g[i][j] == 'T') type[i][j] = 3;
		}
	}
	for(int i = 1; i <= m; i++){
		expandBee();
		if(dist[fx][fy]) return false;
	}	
	while(!dist[fx][fy]){
		expandMecho();
		if(dist[fx][fy]) return true;
		expandBee();
	}
	return false;
}

int main(){
	cin >> n >> s;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++){
			cin >> g[i][j];
			if(g[i][j] == 'D') fx = i, fy = j;
		}

	if(!guess(0)){
		cout << -1;
		return 0;
	}
	int l = 0, r = 2000;
	while(l < r){
		int m = (l+r+1)/2;
		if(guess(m)) l = m;
		else r = m-1;
	}
	cout << l << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...