제출 #1324101

#제출 시각아이디문제언어결과실행 시간메모리
1324101kasamchiMecho (IOI09_mecho)C++20
84 / 100
310 ms2068 KiB
#include <bits/stdc++.h>
using namespace std;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

bool check(int m, int S, vector<string> G) {
    int N = G.size();
    queue<pair<int, int>> hqu;
    queue<pair<int, int>> mqu;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (G[i][j] == 'H') {
                hqu.push(make_pair(i, j));
            } else if (G[i][j] == 'M') {
                mqu.push(make_pair(i, j));
            }
        }
    }
    for (int tm = 0; ; tm++) {
        if (tm >= m) {
            for (int s = 0; s < S; s++) {
                int sz = mqu.size();
                for (int t = 0; t < sz; t++) {
                    int x = mqu.front().first, y = mqu.front().second;
                    mqu.pop();
                    if (G[x][y] == 'H') {
                        continue;
                    }
                    for (int d = 0; d < 4; d++) {
                        int nx = x + dx[d], ny = y + dy[d];
                        if (nx >= 0 && nx < N && ny >= 0 && ny < N && G[nx][ny] != 'T' && G[nx][ny] != 'H' && G[nx][ny] != 'M') {
                            if (G[nx][ny] == 'D') {
                                return true;
                            }
                            G[nx][ny] = 'M';
                            mqu.push(make_pair(nx, ny));
                        }
                    }
                }
            }
        }
        
        int sz = hqu.size();
        for (int t = 0; t < sz; t++) {
            int x = hqu.front().first, y = hqu.front().second;
            hqu.pop();
            for (int d = 0; d < 4; d++) {
                int nx = x + dx[d], ny = y + dy[d];
                if (nx >= 0 && nx < N && ny >= 0 && ny < N && G[nx][ny] != 'T' && G[nx][ny] != 'H') {
                    if (G[nx][ny] == 'D') {
                        return false;
                    }
                    G[nx][ny] = 'H';
                    hqu.push(make_pair(nx, ny));
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, S;
    cin >> N >> S;

    vector<string> G(N);
    for (int i = 0; i < N; i++) {
        cin >> G[i];
    }

    int ll = -1, rr = N * N + 1;
    while (ll + 1 < rr) {
        int mm = ll + (rr - ll) / 2;
        if (check(mm, S, G)) {
            ll = mm;
        } else {
            rr = mm;
        }
    }
    cout << ll << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...