# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
543870 | timreizin | Mecho (IOI09_mecho) | C++17 | 228 ms | 6888 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 <iostream>
#include <vector>
#include <array>
#include <numeric>
#include <cassert>
#include <tuple>
#include <queue>
using namespace std;
using ll = long long;
const int INF = 1e9;
const array<int, 4> dx{1, -1, 0, 0}, dy{0, 0, 1, -1};
bool check(int w, int s, vector<string> &forest, vector<vector<int>> &beeD)
{
vector<vector<int>> dist(beeD.size(), vector<int>(beeD.size(), INF));
queue<pair<int, int>> q;
for (int i = 0; i < beeD.size(); ++i)
{
for (int j = 0; j < beeD.size(); ++j)
{
if (forest[i][j] == 'M')
{
dist[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty())
{
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k)
{
if (i + dx[k] >= 0 && i + dx[k] < beeD.size() && j + dy[k] >= 0 && j + dy[k] < beeD.size())
{
if (dist[i + dx[k]][j + dy[k]] > dist[i][j] + 1 && forest[i + dx[k]][j + dy[k]] != 'T')
{
if (forest[i + dx[k]][j + dy[k]] == 'D')
{
if (dist[i][j] / s + w < beeD[i + dx[k]][j + dy[k]])
{
dist[i + dx[k]][j + dy[k]] = dist[i][j] + 1;
q.push({i + dx[k], j + dy[k]});
}
}
else
{
if ((dist[i][j] + 1) / s + w < beeD[i + dx[k]][j + dy[k]])
{
dist[i + dx[k]][j + dy[k]] = dist[i][j] + 1;
q.push({i + dx[k], j + dy[k]});
}
}
}
}
}
}
for (int i = 0; i < beeD.size(); ++i) for (int j = 0; j < beeD.size(); ++j) if (forest[i][j] == 'D') return dist[i][j] != INF;
return false;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, s;
cin >> n >> s;
vector<string> forest(n);
for (string &i : forest) cin >> i;
vector<vector<int>> beeD(n, vector<int>(n, INF));
queue<pair<int, int>> q;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (forest[i][j] == 'H')
{
beeD[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty())
{
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k)
{
if (i + dx[k] >= 0 && i + dx[k] < n && j + dy[k] >= 0 && j + dy[k] < n)
{
if (beeD[i + dx[k]][j + dy[k]] > beeD[i][j] + 1 && forest[i + dx[k]][j + dy[k]] != 'T')
{
beeD[i + dx[k]][j + dy[k]] = beeD[i][j] + 1;
q.push({i + dx[k], j + dy[k]});
}
}
}
}
int l = 0, r = n * n;
while (l < r)
{
int m = (l + r) >> 1;
if (check(m, s, forest, beeD)) l = m + 1;
else r = m;
}
cout << l - 1;
return 0;
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |