Submission #281951

#TimeUsernameProblemLanguageResultExecution timeMemory
281951PlurmMecho (IOI09_mecho)C++11
100 / 100
432 ms6308 KiB
#include <bits/stdc++.h>
using namespace std;
char t[1024][1024];
char test[1024][1024];
int dist[1024][1024];
int n, s;
class state{
public:
	int x,y,cw;
	state(int a, int b, int c): x(a), y(b), cw(c) {}
};
void propagateBees(queue<state>& q, int limit){
	while(!q.empty() && q.front().cw < limit){
		state cur = q.front();
		q.pop();
		int x = cur.x;
		int y = cur.y;
		int w = cur.cw+1;
		if(x > 1 && (test[x-1][y] == 'G' && dist[x-1][y] > w) || test[x-1][y] == 'M'){
			dist[x-1][y] = w;
			test[x-1][y] = 'H';
			q.emplace(x-1, y, w);
		}
		if(x < n && (test[x+1][y] == 'G' && dist[x+1][y] > w) || test[x+1][y] == 'M'){
			dist[x+1][y] = w;
			test[x+1][y] = 'H';
			q.emplace(x+1, y, w);
		}
		if(y > 1 && (test[x][y-1] == 'G' && dist[x][y-1] > w) || test[x][y-1] == 'M'){
			dist[x][y-1] = w;
			test[x][y-1] = 'H';
			q.emplace(x, y-1, w);
		}
		if(y < n && (test[x][y+1] == 'G' && dist[x][y+1] > w) || test[x][y+1] == 'M'){
			dist[x][y+1] = w;
			test[x][y+1] = 'H';
			q.emplace(x, y+1, w);
		}
	}
}
void propagateMecho(queue<state>& q, int limit){
	while(!q.empty() && q.front().cw < limit){
		state cur = q.front();
		q.pop();
		int x = cur.x;
		int y = cur.y;
		if(test[x][y] != 'M') continue;
		int w = cur.cw+1;
		if(x > 1 && (test[x-1][y] == 'G' || test[x-1][y] == 'D') && dist[x-1][y] > w){
			dist[x-1][y] = w;
			test[x-1][y] = 'M';
			q.emplace(x-1, y, w);
		}
		if(x < n && (test[x+1][y] == 'G' || test[x+1][y] == 'D') && dist[x+1][y] > w){
			dist[x+1][y] = w;
			test[x+1][y] = 'M';
			q.emplace(x+1, y, w);
		}
		if(y > 1 && (test[x][y-1] == 'G' || test[x][y-1] == 'D') && dist[x][y-1] > w){
			dist[x][y-1] = w;
			test[x][y-1] = 'M';
			q.emplace(x, y-1, w);
		}
		if(y < n && (test[x][y+1] == 'G' || test[x][y+1] == 'D') && dist[x][y+1] > w){
			dist[x][y+1] = w;
			test[x][y+1] = 'M';
			q.emplace(x, y+1, w);
		}
	}
}
bool escapePossible(int days){
	if(days == -1) return true;
	queue<state> q, mq;
	int dx, dy;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			test[i][j] = t[i][j];
			dist[i][j] = 1e9;
			if(t[i][j] == 'H'){
				q.emplace(i,j,0);
				dist[i][j] = 0;
			}else if(t[i][j] == 'D'){
				dx = i;
				dy = j;
			}else if(t[i][j] == 'M'){
				mq.emplace(i,j,0);
				dist[i][j] = 0;
			}
		}
	}
	propagateBees(q, days);
	int lm = 0;
	while(!q.empty() || !mq.empty()){
		lm += s;
		propagateMecho(mq, lm);
		if(test[dx][dy] == 'M') return true;
		days++;
		propagateBees(q, days);
	}
	return false;
}
int main(){
	scanf("%d%d",&n,&s);
	for(int i = 1; i <= n; i++){
		scanf("%s", t[i]+1);
	}
	int lo = -1;
	int hi = n*n;
	int mid;
	while(lo < hi){
		mid = (lo+hi+1)/2;
		if(escapePossible(mid)){
			lo = mid;
		}else{
			hi = mid-1;
		}
	}
	printf("%d\n", lo);
	return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void propagateBees(std::queue<state>&, int)':
mecho.cpp:19:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   19 |   if(x > 1 && (test[x-1][y] == 'G' && dist[x-1][y] > w) || test[x-1][y] == 'M'){
      |      ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:24:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   24 |   if(x < n && (test[x+1][y] == 'G' && dist[x+1][y] > w) || test[x+1][y] == 'M'){
      |      ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:29:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   29 |   if(y > 1 && (test[x][y-1] == 'G' && dist[x][y-1] > w) || test[x][y-1] == 'M'){
      |      ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:34:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   34 |   if(y < n && (test[x][y+1] == 'G' && dist[x][y+1] > w) || test[x][y+1] == 'M'){
      |      ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'int main()':
mecho.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  103 |  scanf("%d%d",&n,&s);
      |  ~~~~~^~~~~~~~~~~~~~
mecho.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |   scanf("%s", t[i]+1);
      |   ~~~~~^~~~~~~~~~~~~~
mecho.cpp: In function 'bool escapePossible(int)':
mecho.cpp:96:17: warning: 'dy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |   if(test[dx][dy] == 'M') return true;
      |      ~~~~~~~~~~~^
mecho.cpp:96:17: warning: 'dx' may be used uninitialized in this function [-Wmaybe-uninitialized]
#Verdict Execution timeMemoryGrader output
Fetching results...