# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
724714 | rastervc | Mecho (IOI09_mecho) | C++17 | 1 ms | 340 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("mecho.in");
ofstream fout("mecho.out");
constexpr int EMPTY = 0;
constexpr int BARRIER = 1;
constexpr int MECHO = 2;
constexpr int DANGER = 3;
constexpr int dx[] = { 0, 1, 0, -1 };
constexpr int dy[] = { 1, 0, -1, 0 };
inline bool test(
int N, int S, int wait, pair<int, int> start, pair<int, int> finish,
vector<pair<int, int>> trees, vector<pair<int, int>> hives
) {
vector<vector<int>> mat(N + 2, vector<int>(N + 2, 0));
queue<pair<int, int>> bees, pos;
for (int i = 0; i <= N + 1; ++i)
mat[i][0] = mat[0][i] = mat[i][N + 1] = mat[N + 1][i] = BARRIER;
for (const auto[x, y] : trees)
mat[x][y] = BARRIER;
for (const auto[x, y] : hives) {
mat[x][y] = DANGER;
bees.emplace(x, y);
}
mat[start.first][start.second] = MECHO;
pos.push(start);
for (int time = 1; !bees.empty() && !pos.empty(); ++time) {
if (time > wait) {
for (int step = 1; step <= S; ++step) {
const int sz = pos.size();
for (int i = 0; i < sz; ++i) {
const auto[x0, y0] = pos.front();
pos.pop();
for (int j = 0; j < 4; ++j) {
const int x1 = x0 + dx[j];
const int y1 = y0 + dy[j];
if (mat[x1][y1] == EMPTY) {
mat[x1][y1] = MECHO;
pos.emplace(x1, y1);
}
}
}
}
}
const int sz = bees.size();
for (int i = 0; i < sz; ++i) {
const auto[x0, y0] = bees.front();
bees.pop();
for (int j = 0; j < 4; ++j) {
const int x1 = x0 + dx[j];
const int y1 = y0 + dy[j];
if (mat[x1][y1] == EMPTY || mat[x1][y1] == MECHO) {
mat[x1][y1] = DANGER;
bees.emplace(x1, y1);
}
}
}
if (mat[finish.first][finish.second] == MECHO)
return true;
}
return false;
}
int main() {
vector<pair<int, int>> trees, hives;
pair<int, int> start, finish;
int N, S;
char ch;
fin >> N >> S;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
fin >> ch;
if (ch == 'T')
trees.emplace_back(i, j);
else if (ch == 'H')
hives.emplace_back(i, j);
else if (ch == 'M')
start = { i, j };
else if (ch == 'D')
finish = { i, j };
}
}
int l = 0, r = N * N, ans = -1;
while (l <= r) {
const int mid = (l + r) >> 1;
if (test(N, S, mid, start, finish, trees, hives)) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
fout << ans;
fin.close();
fout.close();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |