# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1098825 | damamila | Mecho (IOI09_mecho) | C++14 | 1095 ms | 65536 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s;
int x3, x2, y3, y2;
vector<vector<char>> grid;
vector<vector<int>> tim; //saves time the bees get there
vector<vector<int>> dist; //saves latest time can leave current spot to get to house on time
vector<vector<int>> steps;
vector<pair<int, int>> direc = {
{1, 0},
{-1, 0},
{0, 1},
{0, -1}
};
bool check(int i, int j) {
if (i < 0 || j < 0 || i == n || j == n) return false; //out of bounds
if (grid[i][j] == 'M' || grid[i][j] == 'G') return true; //good next field
return false;
}
bool check2(int i, int j) {
if (i < 0 || j < 0 || i == n || j == n) return false; //out of bounds
if (grid[i][j] == 'M' || grid[i][j] == 'G' || grid[i][j] == 'D') return true; //good next field
return false;
}
void bfs1() {
queue<tuple<int, int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'H') q.push({0, i, j});
}
}
while (!q.empty()) {
auto [d, i, j] = q.front();
q.pop();
if (tim[i][j] <= d) continue;
tim[i][j] = d;
for (auto [x, y] : direc) {
if (check(i+x, j+y)) q.push({d+1, i+x, j+y});
}
}
}
void bfs2() {
priority_queue<tuple<int, int, int, int>> pq; //dist, i, j, steps left
pq.push({1e9, x2, y2, s});
while (!pq.empty()) {
auto [d, i, j, step] = pq.top();
//~ cout << d << " " << i << " " << j << " " << step << endl;
pq.pop();
if (tim[i][j] <= d) {
d = tim[i][j]-1;
step = s;
}
if (step == 0) {
d--;
step = s;
}
if (dist[i][j] > d || (dist[i][j] == d && steps[i][j] >= d)) continue;
dist[i][j] = d;
steps[i][j] = step;
for (auto [x, y] : direc) {
if (check(i+x, j+y)) pq.push({d, i+x, j+y, step-1});
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
grid = vector<vector<char>> (n, vector<char> (n));
tim = vector<vector<int>> (n, vector<int> (n, 1e9));
dist = vector<vector<int>> (n, vector<int> (n, -1));
steps = vector<vector<int>> (n, vector<int> (n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'D') {
x2 = i; y2 = j;
}
if (grid[i][j] == 'M') {
x3 = i; y3 = j;
}
}
}
bfs1(); //calculates how long it takes for fields to be infected with bees
//~ for (int i = 0; i < n; i++) {
//~ for (int j = 0; j < n; j++) {
//~ cout << tim[i][j] << " ";
//~ }
//~ cout << endl;
//~ }
//now need to calc movement somehow
bfs2();
//~ for (int i = 0; i < n; i++) {
//~ for (int j = 0; j < n; j++) {
//~ cout << dist[i][j] << " ";
//~ }
//~ cout << endl;
//~ }
//~ int ans = 0;
//~ for (auto [x, y] : direc) {
//~ if (check2(x3+x, y3+y)) ans = max(ans, dist[x3+x][y3+y]);
//~ }
//~ cout << ans << endl;
cout << dist[x3][y3] << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |