結果

問題 No.2641 draw X
ユーザー InTheBloomInTheBloom
提出日時 2024-02-26 00:39:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 4,891 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 102,148 KB
実行使用メモリ 65,280 KB
最終ジャッジ日時 2024-02-26 00:39:15
合計ジャッジ時間 5,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 150 ms
65,280 KB
testcase_16 AC 171 ms
65,280 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 14 ms
7,680 KB
testcase_21 AC 30 ms
12,928 KB
testcase_22 AC 12 ms
7,424 KB
testcase_23 AC 22 ms
11,520 KB
testcase_24 AC 9 ms
6,912 KB
testcase_25 AC 7 ms
6,676 KB
testcase_26 AC 30 ms
13,312 KB
testcase_27 AC 7 ms
6,676 KB
testcase_28 AC 56 ms
24,516 KB
testcase_29 AC 116 ms
50,972 KB
testcase_30 AC 106 ms
43,160 KB
testcase_31 AC 77 ms
29,776 KB
testcase_32 AC 127 ms
51,128 KB
testcase_33 AC 131 ms
50,704 KB
testcase_34 AC 76 ms
30,520 KB
testcase_35 AC 64 ms
27,256 KB
testcase_36 AC 174 ms
58,752 KB
testcase_37 AC 172 ms
62,208 KB
testcase_38 AC 154 ms
54,912 KB
testcase_39 AC 181 ms
64,256 KB
testcase_40 AC 167 ms
61,184 KB
testcase_41 AC 141 ms
55,168 KB
testcase_42 AC 169 ms
58,240 KB
testcase_43 AC 168 ms
55,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void solve (int H, int W, vector<string>& S) {
    // 場所からできるだけデカいバツを書いていったとき、同じになるか?
    enum direction {
        left_up,
        left_down,
        right_up,
        right_down,
    };

    vector<vector<vector<int>>> maximum_stretch(H, vector<vector<int>>(W, vector<int>(4)));

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') maximum_stretch[i][j][left_up] = -1;

            if (S[i][j] == '#') {
                if (i == 0 || j == 0) {
                    maximum_stretch[i][j][left_up] = 0;
                }
                else {
                    maximum_stretch[i][j][left_up] = maximum_stretch[i-1][j-1][left_up] + 1;
                }
            }
        }
    }

    for (int i = H-1; 0 <= i; i--) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') maximum_stretch[i][j][left_down] = -1;

            if (S[i][j] == '#') {
                if (i == H-1 || j == 0) {
                    maximum_stretch[i][j][left_down] = 0;
                }
                else {
                    maximum_stretch[i][j][left_down] = maximum_stretch[i+1][j-1][left_down] + 1;
                }
            }
        }
    }

    for (int i = 0; i < H; i++) {
        for (int j = W-1; 0 <= j; j--) {
            if (S[i][j] == '.') maximum_stretch[i][j][right_up] = -1;

            if (S[i][j] == '#') {
                if (i == 0 || j == W-1) {
                    maximum_stretch[i][j][right_up] = 0;
                }
                else {
                    maximum_stretch[i][j][right_up] = maximum_stretch[i-1][j+1][right_up] + 1;
                }
            }
        }
    }

    for (int i = H-1; 0 <= i; i--) {
        for (int j = W-1; 0 <= j; j--) {
            if (S[i][j] == '.') maximum_stretch[i][j][right_down] = -1;

            if (S[i][j] == '#') {
                if (i == H-1 || j == W-1) {
                    maximum_stretch[i][j][right_down] = 0;
                }
                else {
                    maximum_stretch[i][j][right_down] = maximum_stretch[i+1][j+1][right_down] + 1;
                }
            }
        }
    }

    vector<string> T(H, string(W, 0));
    vector<vector<int>> draw_table(H, vector<int>(W));

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) draw_table[i][j] = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') T[i][j] = '.';
            if (S[i][j] == '#') {
                int len = H*W;
                for (int k = 0; k < 4; k++) len = min(len, maximum_stretch[i][j][k]);

                if (len == 0) continue;
                draw_table[i-len][j-len] += 1;
                if (i+len+1 < H && j+len+1 < W) draw_table[i+len+1][j+len+1] += -1;
            }
        }
    }

    // 塗る
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
        if (0 < i && 0 < j) draw_table[i][j] += draw_table[i-1][j-1];

        if (T[i][j] == '#') continue;
        if (0 < draw_table[i][j]) {
            T[i][j] = '#';
        }
        else {
            T[i][j] = '.';
        }
    }

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) draw_table[i][j] = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') T[i][j] = '.';
            if (S[i][j] == '#') {
                int len = H*W;
                for (int k = 0; k < 4; k++) len = min(len, maximum_stretch[i][j][k]);

                if (len == 0) continue;
                draw_table[i+len][j-len] += 1;
                if (0 <= i-len-1 && j+len+1 < W) draw_table[i-len-1][j+len+1] += -1;
            }
        }
    }

    // 塗る
    for (int i = H-1; 0 <= i; i--) for (int j = 0; j < W; j++) {
        if (i < H-1 && 0 < j) draw_table[i][j] += draw_table[i+1][j-1];

        if (T[i][j] == '#') continue;
        if (0 < draw_table[i][j]) {
            T[i][j] = '#';
        }
        else {
            T[i][j] = '.';
        }
    }

    bool ok = true;
    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (S[i][j] != T[i][j]) ok = false;

    /* visualize
    for (int k = 0; k < 4; k++) {
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cout << maximum_stretch[i][j][k] << " ";
            }
            cout << "\n";
        }
        cout << "\n";
    }
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cout << T[i][j];
        }
        cout << "\n";
    }
    // */


    if (ok) {
        cout << "Yes\n";
    }
    else {
        cout << "No\n";
    }
}

int main () {
    int H, W; cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) cin >> S[i];

    solve(H, W, S);
}
0