結果

問題 No.179 塗り分け
ユーザー @abcde@abcde
提出日時 2019-06-01 01:16:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 3,000 ms
コード長 4,063 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 167,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 21:11:55
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 63 ms
4,380 KB
testcase_09 AC 64 ms
4,376 KB
testcase_10 AC 72 ms
4,376 KB
testcase_11 AC 58 ms
4,380 KB
testcase_12 AC 64 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 71 ms
4,376 KB
testcase_19 AC 66 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 68 ms
4,376 KB
testcase_23 AC 80 ms
4,380 KB
testcase_24 AC 68 ms
4,380 KB
testcase_25 AC 69 ms
4,376 KB
testcase_26 AC 89 ms
4,376 KB
testcase_27 AC 106 ms
4,376 KB
testcase_28 AC 128 ms
4,376 KB
testcase_29 AC 125 ms
4,380 KB
testcase_30 AC 125 ms
4,376 KB
testcase_31 AC 141 ms
4,380 KB
testcase_32 AC 120 ms
4,376 KB
testcase_33 AC 123 ms
4,376 KB
testcase_34 AC 138 ms
4,376 KB
testcase_35 AC 135 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 4 ms
4,376 KB
testcase_44 AC 17 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 10_hand04.txt, 20_random01.txt, 20_random03.txt, 20_random05.txt, 20_random09.txt で, 
// WA版だったため, 再度, ロジック修正.
// ※ すべて, NO と出力されていた.
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // 1. 入力情報取得.
    int H, W;
    cin >> H >> W;
    char c[H][W];
    for(int i = 0; i < H; i++){
        string s;
        cin >> s;
        for(int j = 0; j < W; j++) c[i][j] = s[j];
    }

    // 2. '#' を カウント.
    int sharp = 0;
    for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) if(c[i][j] == '#') sharp++;
    
    // 2-1. '#' の 個数が, 奇数の場合.
    if(sharp % 2 != 0){
        cout << "NO" << endl;
        return 0;
    }
    
    // 2-2. '#' の 個数が, 0個の場合(※ロジック追加).
    if(sharp == 0){
        cout << "NO" << endl;
        return 0;
    }
    
    // 2-3. '#' の 個数が, 偶数の場合.
    // (仮称)青の領域で, 1点固定して, ベクトルを一つ固定し, 青の領域を全探索した結果, 
    // すべて, ベクトル方向に, '#' であれば, 塗り分けが存在すると判定.
    // ベクトルを, 1つ固定.
    // ※負数のベクトルも考慮するためのロジック修正.
    string ans = "NO";
    for(int vh = 1 - H; vh < H; vh++){
        for(int vw = 1 - W; vw < W; vw++){
            // ベクトル(0, 0) は, Skip.
            if(vh == 0 && vw == 0) continue;
            // 訪問したら, 1, 2 を セット.
            int memo[H][W] = {};
            for(int h = 0; h < H; h++){
                for(int w = 0; w < W; w++){
                    // ※境界条件を追加するためのロジック修正.
                    int uh = h + vh;
                    int uw = w + vw;
                    if(uh < 0 || uw < 0 || uh >= H || uw >= W) continue;
                    if(c[h][w] == '#' && memo[h][w] == 0 && c[uh][uw] == '#'){
                        memo[h][w] = 1;
                        memo[uh][uw] = 2;
                    }
                }
            }
            int one = 0, two = 0;
            for(int h = 0; h < H; h++){
                for(int w = 0; w < W; w++){
                    if(memo[h][w] == 1) one++;
                    if(memo[h][w] == 2) two++;
                }
            }
            // if(vh == -2 && vw == 3){
            //     for(int h = 0; h < H; h++){
            //         for(int w = 0; w < W; w++){
            //             cout << memo[h][w] << " ";
            //         }
            //         cout << endl;
            //     }
            // }
            // 塗り分け の 存在チェック.
            if(one == sharp / 2 && two == sharp / 2){
                ans = "YES";
                break;
            }
        }
    }

    // 3. 出力.
    // ex.
    // [入力例]
    // 11 17
    // .#.#.#.#.........
    // #.#.#.#.#..#.....
    // .#.#.#.#.##......
    // ..#.#.#..##......
    // ...#.#...........
    // .................
    // ......#.#.#.#....
    // .....#.#.#.#.#..#
    // ......#.#.#.#.##.
    // .......#.#.#..##.
    // ........#.#......
    // 
    // 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 
    // 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 0 
    // 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 
    // 0 0 1 0 1 0 1 0 0 1 1 0 0 0 0 0 0 
    // 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 
    // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    // 0 0 0 0 0 0 2 0 2 0 2 0 2 0 0 0 0 
    // 0 0 0 0 0 2 0 2 0 2 0 2 0 2 0 0 2 
    // 0 0 0 0 0 0 2 0 2 0 2 0 2 0 2 2 0 
    // 0 0 0 0 0 0 0 2 0 2 0 2 0 0 2 2 0 
    // 0 0 0 0 0 0 0 0 2 0 2 0 0 0 0 0 0 
    // YES
    // 
    // [入力例]
    // 3 4
    // ...#
    // ....
    // #...
    // 
    // 0 0 0 2 
    // 0 0 0 0 
    // 1 0 0 0 
    // YES
    // 
    // -> YES の はずだが, NO と出力された.
    // -> 10_hand04.txt, 20_random01.txt, 20_random03.txt, 20_random05.txt, 20_random09.txt で, WA版
    // となった原因と推測される.
    // -> ベクトルとしては, (-2, 3) のはず.
    cout << ans << endl;
    return 0;
    
}
0