# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
581547 | Saeed_247 | Mecho (IOI09_mecho) | C++17 | 728 ms | 11864 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
template<typename T> void dout(string name, T arg) {
cerr << name << " = " << arg << "\e[39m" << endl;
}
template<typename T1, typename... T2> void dout(string names, T1 arg, T2... args) {
cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
dout(names.substr(names.find(',') + 2), args...);
}
#define debug(...) cerr << "\e[91m" << "[" << __LINE__ << ":" << __func__ << "]=> ", dout(#__VA_ARGS__, __VA_ARGS__);
const pair<int, int> dir[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int N = 800;
bool visited[N][N];
long long n, s;
vector<vector<char>> arr;
vector<pair<long long, long long>> hives;
pair<long long, long long> m, d;
queue<pair<long long, long long>> q;
bool move(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < n;
}
inline long long get_tim(long long x) {
return x / s + (x % s != 0);
}
bool check(long long tim, vector<vector<long long>>& swarm) {
vector<vector<long long>> dist(n, vector<long long>(n, 1e18));
dist[m.first][m.second] = 0;
q.push(m);
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' || arr[x + xx][y + yy] == 'D')
&& dist[x + xx][y + yy] > dist[x][y] + 1 && get_tim(dist[x][y] + 1) + tim <= swarm[x + xx][y + yy]) {
dist[x + xx][y + yy] = dist[x][y] + 1;
q.push({x + xx, y + yy});
}
}
return dist[d.first][d.second] < 1e18;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
arr.resize(n, vector<char> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
if (arr[i][j] == 'M') {
m = {i, j};
}
if (arr[i][j] == 'H') {
hives.push_back({i, j});
}
if (arr[i][j] == 'D') {
d = {i, j};
}
}
}
vector<vector<long long>> swarm(n, vector<long long>(n, 1e18));
for (auto [x, y] : hives) {
swarm[x][y] = 0;
q.push({x, y});
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' || arr[x + xx][y + yy] == 'M')
&& swarm[x + xx][y + yy] > swarm[x][y] + 1) {
swarm[x + xx][y + yy] = swarm[x][y] + 1;
q.push({x + xx, y + yy});
}
}
long long l = -1e9, h = 1e9;
while (h - l > 1) {
long long mid = (h + l) / 2;
if (check(mid, swarm)) l = mid;
else h = mid;
}
if (l < 0)
cout << -1 << '\n';
else
cout << l << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |