結果

問題 No.2412 YOU Grow Bigger!
ユーザー AngrySadEightAngrySadEight
提出日時 2023-07-14 12:29:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 6,000 ms
コード長 3,747 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 93,724 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 06:33:05
合計ジャッジ時間 3,440 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 197 ms
6,940 KB
testcase_04 AC 198 ms
6,940 KB
testcase_05 AC 145 ms
6,944 KB
testcase_06 AC 184 ms
6,944 KB
testcase_07 AC 206 ms
6,940 KB
testcase_08 AC 191 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 6 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 9 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 28 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 6 ms
6,944 KB
testcase_22 AC 8 ms
6,944 KB
testcase_23 AC 57 ms
6,940 KB
testcase_24 AC 111 ms
6,940 KB
testcase_25 AC 49 ms
6,940 KB
testcase_26 AC 5 ms
6,944 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 15 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }
    queue<pii> que;
    vector<vector<int>> isvisited(H - 2, vector<int>(W - 2, false));
    que.push(pii(0, 0));
    isvisited[0][0] = true;
    vector<int> dx = {1, 1, 0, -1, -1, -1, 0, 1};
    vector<int> dy = {0, 1, 1, 1, 0, -1, -1, -1};

    vector<vector<bool>> pos(H - 2, vector<bool>(W - 2, true));
    for (int i = 0; i < H - 2; i++) {
        for (int j = 0; j < W - 2; j++) {
            for (int k = 0; k <= 2; k++) {
                for (int l = 0; l <= 2; l++) {
                    if (S[i + k][j + l] == '#') {
                        pos[i][j] = false;
                    }
                }
            }
        }
    }

    while (que.size()) {
        pii p = que.front();
        que.pop();
        for (int i = 0; i < 8; i++) {
            if (p.first + dx[i] >= 0 && p.first + dx[i] < H - 2 &&
                p.second + dy[i] >= 0 && p.second + dy[i] < W - 2) {
                if (isvisited[p.first + dx[i]][p.second + dy[i]]) {
                    continue;
                }
                if (pos[p.first + dx[i]][p.second + dy[i]]) {
                    isvisited[p.first + dx[i]][p.second + dy[i]] = true;
                    que.push(pii(p.first + dx[i], p.second + dy[i]));
                }
            }
        }
    }
    if (!isvisited[H - 3][W - 3]) {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '#') {
                continue;
            }
            if (i >= 0 && i <= 2 && j >= 0 && j <= 2) {
                continue;
            }
            if (i >= H - 3 && i < H && j >= W - 3 && j < W) {
                continue;
            }
            S[i][j] = '#';
            que.push(pii(0, 0));
            for (int k = 0; k < H - 2; k++) {
                for (int l = 0; l < W - 2; l++) {
                    isvisited[k][l] = false;
                }
            }
            isvisited[0][0] = true;
            for (int k = 0; k < H - 2; k++) {
                for (int l = 0; l < W - 2; l++) {
                    pos[k][l] = true;
                }
            }
            for (int k = 0; k < H - 2; k++) {
                for (int l = 0; l < W - 2; l++) {
                    for (int m = 0; m < 3; m++) {
                        for (int n = 0; n < 3; n++) {
                            if (S[k + m][l + n] == '#') {
                                pos[k][l] = false;
                            }
                        }
                    }
                }
            }
            while (que.size()) {
                pii p = que.front();
                que.pop();
                for (int k = 0; k < 8; k++) {
                    if (p.first + dx[k] >= 0 && p.first + dx[k] < H - 2 &&
                        p.second + dy[k] >= 0 && p.second + dy[k] < W - 2) {
                        if (isvisited[p.first + dx[k]][p.second + dy[k]]) {
                            continue;
                        }
                        if (pos[p.first + dx[k]][p.second + dy[k]]) {
                            isvisited[p.first + dx[k]][p.second + dy[k]] = true;
                            que.push(pii(p.first + dx[k], p.second + dy[k]));
                        }
                    }
                }
            }
            if (!isvisited[H - 3][W - 3]) {
                cout << 1 << endl;
                return 0;
            }
            S[i][j] = '.';
        }
    }
    cout << 2 << endl;
}
0