# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
312948 | 2020-10-14T20:04:21 Z | fergonzgut21 | Mecho (IOI09_mecho) | C++17 | 0 ms | 0 KB |
#include <iostream> #include <queue> #define MAX 805 using namespace std; struct bee { int x; int y; int t; int r; }; short int N, S, sx, sy, ex, ey, B[MAX][MAX], vis[MAX][MAX][2], rilres = -1; char A[MAX][MAX]; queue <bee> qiu, qiu2; bee temp; void solve () { while (!qiu.empty()) { temp = qiu.front(); qiu.pop(); temp.t++; if (B[temp.x - 1][temp.y] == -1 && A[temp.x - 1][temp.y] != 'T' && A[temp.x - 1][temp.y] != 'D') { B[temp.x - 1][temp.y] = temp.t; temp.x--; qiu.push(temp); temp.x++; } if (B[temp.x + 1][temp.y] == -1 && A[temp.x + 1][temp.y] != 'T' && A[temp.x + 1][temp.y] != 'D') { B[temp.x + 1][temp.y] = temp.t; temp.x++; qiu.push(temp); temp.x--; } if (B[temp.x][temp.y - 1] == -1 && A[temp.x][temp.y - 1] != 'T' && A[temp.x][temp.y - 1] != 'D') { B[temp.x][temp.y - 1] = temp.t; temp.y--; qiu.push(temp); temp.y++; } if (B[temp.x][temp.y + 1] == -1 && A[temp.x][temp.y + 1] != 'T' && A[temp.x][temp.y + 1] != 'D') { B[temp.x][temp.y + 1] = temp.t; temp.y++; qiu.push(temp); temp.y--; } } } int solve2(int x, int y, int t, int r) { temp.x = x; temp.y = y; temp.t = t; temp.r = r; qiu2.push(temp); while (!qiu2.empty()) { temp = qiu2.front(); qiu2.pop(); //cout << temp.x << " " << temp.y << " " << temp.t << " " << temp.r << endl; if (temp.x == ex && temp.y == ey) rilres = max(rilres,temp.r); if (temp.r + (temp.t / S) >= B[temp.x][temp.y]) temp.r -= ((temp.r + (temp.t / S)) - B[temp.x][temp.y] + 1); //cout << temp.x << " " << temp.y << " " << temp.t << " " << temp.r << endl; if (temp.r < 0 || temp.r <= rilres) continue; temp.t++; //cout << vis[temp.x][temp.y][0] << " " << vis[temp.x][temp.y][1] << endl; if (vis[temp.x + 1][temp.y][1] <= temp.r && A[temp.x + 1][temp.y] != 'T') { if ((vis[temp.x + 1][temp.y][1] == temp.r && vis[temp.x + 1][temp.y][0] > temp.t) || vis[temp.x + 1][temp.y][1] < temp.r) { //cout << "SI" << endl; vis[temp.x + 1][temp.y][1] = temp.r; vis[temp.x + 1][temp.y][0] = temp.t; temp.x++; qiu2.push(temp); temp.x--; } } if (vis[temp.x - 1][temp.y][1] <= temp.r && A[temp.x - 1][temp.y] != 'T') { if ((vis[temp.x - 1][temp.y][1] == temp.r && vis[temp.x - 1][temp.y][0] > temp.t) || vis[temp.x - 1][temp.y][1] < temp.r) { vis[temp.x - 1][temp.y][1] = temp.r; vis[temp.x - 1][temp.y][0] = temp.t; temp.x--; qiu2.push(temp); temp.x++; } } if (vis[temp.x][temp.y + 1][1] <= temp.r && A[temp.x][temp.y + 1] != 'T') { if ((vis[temp.x][temp.y + 1][1] == temp.r && vis[temp.x][temp.y + 1][0] > temp.t) || vis[temp.x][temp.y + 1][1] < temp.r) { vis[temp.x][temp.y + 1][1] = temp.r; vis[temp.x][temp.y + 1][0] = temp.t; temp.y++; qiu2.push(temp); temp.y--; } } if (vis[temp.x][temp.y - 1][1] <= temp.r && A[temp.x][temp.y - 1] != 'T') { if ((vis[temp.x][temp.y - 1][1] == temp.r && vis[temp.x][temp.y - 1][0] > temp.t) || vis[temp.x][temp.y - 1][1] < temp.r) { vis[temp.x][temp.y - 1][1] = temp.r; vis[temp.x][temp.y - 1][0] = temp.t; temp.y--; qiu2.push(temp); temp.y++; } } } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> S; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) { cin >> A[i][j]; vis[i][j][1] = -1; vis[i][j][0] = 32700; B[i][j] = -1; if (A[i][j] == 'M') { sx = i; sy = j; } if (A[i][j] == 'D') { ex = i; ey = j; } if (A[i][j] == 'H') { temp.x = i; temp.y = j; temp.t = 0; B[i][j] = 0; qiu.push(temp); } } solve(); /*cout << endl; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { cout << B[i][j] << " "; } cout << endl; }*/ vis[sx][sy][1] = 32700; vis[sx][sy][0] = 0; solve2(sx, sy, 0, B[sx][sy] - 1); cout << rilres; return 0; }