Submission #1100109

# Submission time Handle Problem Language Result Execution time Memory
1100109 2024-10-12T18:05:12 Z egccge Mecho (IOI09_mecho) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 800;
vector<string> field(MAX_N);
int n, s;

bool valid_sq(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < n &&
	       (field[x][y] == 'G' || field[x][y] == 'M');
}

bool mecho_reached(int mecho_dis, int bees_dis) { return mecho_dis / s < bees_dis; }

int main() {
	cin >> n >> s;
	for (int i = 0; i < n; i++) { cin >> field[i]; }

	vector<pair<int, int>> hives;
	int mechox, mechoy, home_x, home_y;
	// find x and y coordinates for for Mecho, the bees and the cave
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (field[i][j] == 'M') {
				mechox = i;
				mechoy = j;
			} else if (field[i][j] == 'H') {
				hives.push_back({i, j});
			} else if (field[i][j] == 'D') {
				home_x = i;
				home_y = j;
			}
		}
	}

	int dx[] = {-1, 0, 1, 0};
	int dy[] = {0, -1, 0, 1};

	// binary search on waiting time
	int l = 0;
	int r = n * n;
    vector<vector<bool>> bees_visited(n, vector<bool>(n));
    vector<vector<int>> bees_time(n, vector<int>(n));
    queue<pair<int, int>> q;
    for (auto i : hives) {
			q.push({i.first, i.second});
			bees_visited[i.first][i.second] = true;
		}

		while (!q.empty()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i], ny = y + dy[i];
				if (valid_sq(nx, ny) && !bees_visited[nx][ny]) {
					bees_time[nx][ny] = bees_time[x][y] + 1;
					q.push({nx, ny});
					bees_visited[nx][ny] = true;
				}
			}
		}
	while (l <= r) {
		vector<vector<bool>> mecho_visited(n, vector<bool>(n));
		vector<vector<int>> mecho_time(n, vector<int>(n));
		queue<pair<int, int>> q;

		int eating_time = (l + r) / 2;



		// move Mecho
		q.push({mechox, mechoy});
		mecho_visited[mechox][mechoy] = true;
		if (bees_time[mechox][mechoy] <= eating_time) { q.pop(); }

		while (!q.empty()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i], ny = y + dy[i];
				/*
				 * check if mecho reaces node[x][y] before the bees
				 * divide the time mecho takes to reach a node by s, since
				 * mecho walks s steps at a time.
				 * substract the eating time from the time taken for the
				 * bees to reach the node, because that time was used by
				 * mecho for eating
				 */
				if (valid_sq(nx, ny) && !mecho_visited[nx][ny] &&
				    mecho_reached(mecho_time[x][y] + 1,
				                  bees_time[nx][ny] - eating_time)) {
					mecho_visited[nx][ny] = true;
					q.push({nx, ny});
					mecho_time[nx][ny] = mecho_time[x][y] + 1;
				}
			}
		}

		// check if mecho reached a node surrounding his cave before the bees
		bool reached = false;
		for (int i = 0; i < 4; i++) {
			int nx = home_x + dx[i], ny = home_y + dy[i];
			if (valid_sq(nx, ny) &&
			    mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) &&
			    mecho_visited[nx][ny]) {
				reached = true;
			}
		}
		if (reached) {
			l = eating_time + 1;
		} else {
			r = eating_time - 1;
		}
	}

	cout << l - 1 << '\n';
}#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 800;
vector<string> field(MAX_N);
int n, s;

bool valid_sq(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < n &&
	       (field[x][y] == 'G' || field[x][y] == 'M');
}

bool mecho_reached(int mecho_dis, int bees_dis) { return mecho_dis / s < bees_dis; }

int main() {
	cin >> n >> s;
	for (int i = 0; i < n; i++) { cin >> field[i]; }

	vector<pair<int, int>> hives;
	int mechox, mechoy, home_x, home_y;
	// find x and y coordinates for for Mecho, the bees and the cave
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (field[i][j] == 'M') {
				mechox = i;
				mechoy = j;
			} else if (field[i][j] == 'H') {
				hives.push_back({i, j});
			} else if (field[i][j] == 'D') {
				home_x = i;
				home_y = j;
			}
		}
	}

	int dx[] = {-1, 0, 1, 0};
	int dy[] = {0, -1, 0, 1};

	// binary search on waiting time
	int l = 0;
	int r = n * n;
    vector<vector<bool>> bees_visited(n, vector<bool>(n));
    vector<vector<int>> bees_time(n, vector<int>(n));
    queue<pair<int, int>> q;
    for (auto i : hives) {
			q.push({i.first, i.second});
			bees_visited[i.first][i.second] = true;
		}

		while (!q.empty()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i], ny = y + dy[i];
				if (valid_sq(nx, ny) && !bees_visited[nx][ny]) {
					bees_time[nx][ny] = bees_time[x][y] + 1;
					q.push({nx, ny});
					bees_visited[nx][ny] = true;
				}
			}
		}
	while (l <= r) {
		vector<vector<bool>> mecho_visited(n, vector<bool>(n));
		vector<vector<int>> mecho_time(n, vector<int>(n));
		queue<pair<int, int>> q;

		int eating_time = (l + r) / 2;



		// move Mecho
		q.push({mechox, mechoy});
		mecho_visited[mechox][mechoy] = true;
		if (bees_time[mechox][mechoy] <= eating_time) { q.pop(); }

		while (!q.empty()) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i], ny = y + dy[i];
				/*
				 * check if mecho reaces node[x][y] before the bees
				 * divide the time mecho takes to reach a node by s, since
				 * mecho walks s steps at a time.
				 * substract the eating time from the time taken for the
				 * bees to reach the node, because that time was used by
				 * mecho for eating
				 */
				if (valid_sq(nx, ny) && !mecho_visited[nx][ny] &&
				    mecho_reached(mecho_time[x][y] + 1,
				                  bees_time[nx][ny] - eating_time)) {
					mecho_visited[nx][ny] = true;
					q.push({nx, ny});
					mecho_time[nx][ny] = mecho_time[x][y] + 1;
				}
			}
		}

		// check if mecho reached a node surrounding his cave before the bees
		bool reached = false;
		for (int i = 0; i < 4; i++) {
			int nx = home_x + dx[i], ny = home_y + dy[i];
			if (valid_sq(nx, ny) &&
			    mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) &&
			    mecho_visited[nx][ny]) {
				reached = true;
			}
		}
		if (reached) {
			l = eating_time + 1;
		} else {
			r = eating_time - 1;
		}
	}

	cout << l - 1 << '\n';
}

Compilation message

mecho.cpp:117:2: error: stray '#' in program
  117 | }#include <bits/stdc++.h>
      |  ^
mecho.cpp:117:3: error: 'include' does not name a type
  117 | }#include <bits/stdc++.h>
      |   ^~~~~~~
mecho.cpp:120:11: error: redefinition of 'const int MAX_N'
  120 | const int MAX_N = 800;
      |           ^~~~~
mecho.cpp:4:11: note: 'const int MAX_N' previously defined here
    4 | const int MAX_N = 800;
      |           ^~~~~
mecho.cpp:121:16: error: redefinition of 'std::vector<std::__cxx11::basic_string<char> > field'
  121 | vector<string> field(MAX_N);
      |                ^~~~~
mecho.cpp:5:16: note: 'std::vector<std::__cxx11::basic_string<char> > field' previously declared here
    5 | vector<string> field(MAX_N);
      |                ^~~~~
mecho.cpp:122:5: error: redefinition of 'int n'
  122 | int n, s;
      |     ^
mecho.cpp:6:5: note: 'int n' previously declared here
    6 | int n, s;
      |     ^
mecho.cpp:122:8: error: redefinition of 'int s'
  122 | int n, s;
      |        ^
mecho.cpp:6:8: note: 'int s' previously declared here
    6 | int n, s;
      |        ^
mecho.cpp:124:6: error: redefinition of 'bool valid_sq(int, int)'
  124 | bool valid_sq(int x, int y) {
      |      ^~~~~~~~
mecho.cpp:8:6: note: 'bool valid_sq(int, int)' previously defined here
    8 | bool valid_sq(int x, int y) {
      |      ^~~~~~~~
mecho.cpp:129:6: error: redefinition of 'bool mecho_reached(int, int)'
  129 | bool mecho_reached(int mecho_dis, int bees_dis) { return mecho_dis / s < bees_dis; }
      |      ^~~~~~~~~~~~~
mecho.cpp:13:6: note: 'bool mecho_reached(int, int)' previously defined here
   13 | bool mecho_reached(int mecho_dis, int bees_dis) { return mecho_dis / s < bees_dis; }
      |      ^~~~~~~~~~~~~
mecho.cpp:131:5: error: redefinition of 'int main()'
  131 | int main() {
      |     ^~~~
mecho.cpp:15:5: note: 'int main()' previously defined here
   15 | int main() {
      |     ^~~~