Submission #104095

#TimeUsernameProblemLanguageResultExecution timeMemory
104095ErkhemkhuuMecho (IOI09_mecho)C++17
100 / 100
270 ms6528 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
int movex[4] = {-1, 0, 1, 0};
int movey[4] = {0, 1, 0, -1};
char maps[805][805];
int beeStep[805][805], meStep[805][805], n, Mx, My, s, Dx, Dy;
queue <pair <int, int> > bee, me;
inline bool check(int x, int y) {
	if(x >= 0 && x < n && y >= 0 && y < n) return true;
	return false;
}
inline bool can(int mid) {
	if(mid >= beeStep[Mx][My]) return false;
	int curx, cury, i, j, i1, movexx, moveyy;
	memset(meStep, -1, sizeof(meStep));
	me.push(mp(Mx, My));
	meStep[Mx][My] = 0;
	while(!me.empty()) {
		curx = me.front().F;
		cury = me.front().S;
		me.pop();
		for(i = 0; i < 4; i++) {
			movexx = movex[i] + curx;
			moveyy = movey[i] + cury;
			if(check(movexx, moveyy) && maps[movexx][moveyy] == 'D') {
				while(!me.empty())
					me.pop();
				return true;	
			}
			if(check(movexx, moveyy) && (maps[movexx][moveyy] == 'G' || maps[movexx][moveyy] == 'D') && meStep[curx][cury] + 1 < s * (beeStep[movexx][moveyy] - mid) && meStep[movexx][moveyy] == -1) {
				meStep[movexx][moveyy] = meStep[curx][cury] + 1;
				me.push(mp(movexx, moveyy));
			} 
		}
	}
	return false;
}
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(NULL);
	int i, j, l, r, curx, cury, curstep, movexx, moveyy, mid;
	cin >> n >> s;
	memset(beeStep, -1, sizeof(beeStep));
	for(i = 0; i < n; i++) {
		cin >> maps[i];
		for(j = 0; j < n; j++) {
			if(maps[i][j] == 'H') {
				bee.push(mp(i, j));
				beeStep[i][j] = 0;
			}
			if(maps[i][j] == 'M') {
				Mx = i; My = j;
			}
			if(maps[i][j] == 'D') {
				Dx = i; Dy = j;
			}
		}
	}
	while(!bee.empty()) {
		curx = bee.front().F;
		cury = bee.front().S;
		bee.pop();
		for(i = 0; i < 4; i++) {
			movexx = movex[i] + curx;
			moveyy = movey[i] + cury;
			if((check(movexx, moveyy) && maps[movexx][moveyy] == 'G' || maps[movexx][moveyy] == 'M') && beeStep[movexx][moveyy] == -1) {
				beeStep[movexx][moveyy] = beeStep[curx][cury] + 1;
				bee.push(mp(movexx, moveyy));
			}
		}
	}
	if(!can(0)) {
		cout << "-1\n";
		return 0;
	}
	l = 0; r = n * n;
	while(l != r) {
		mid = (l + r + 1) / 2;
		if(can(mid)) l = mid;
		else r = mid - 1;
	}
	cout << l << "\n";
	return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'bool can(int)':
mecho.cpp:19:21: warning: unused variable 'j' [-Wunused-variable]
  int curx, cury, i, j, i1, movexx, moveyy;
                     ^
mecho.cpp:19:24: warning: unused variable 'i1' [-Wunused-variable]
  int curx, cury, i, j, i1, movexx, moveyy;
                        ^~
mecho.cpp: In function 'int main()':
mecho.cpp:72:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
    if((check(movexx, moveyy) && maps[movexx][moveyy] == 'G' || maps[movexx][moveyy] == 'M') && beeStep[movexx][moveyy] == -1) {
        ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:47:30: warning: unused variable 'curstep' [-Wunused-variable]
  int i, j, l, r, curx, cury, curstep, movexx, moveyy, mid;
                              ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...