結果

問題 No.179 塗り分け
ユーザー ふーらくたる
提出日時 2016-07-01 19:15:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,211 bytes
コンパイル時間 609 ms
コンパイル使用メモリ 56,780 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-03 03:26:21
合計ジャッジ時間 1,783 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5 WA * 1
other AC * 20 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

const int kMAX_H = 60;
const int kMAX_W = 60;

string S[kMAX_H];
int color[kMAX_H][kMAX_W];
bool used[kMAX_H][kMAX_H];

int H, W, red, blue;

int dr[] = {1, 0, -1, 0},
    dc[] = {0, 1, 0, -1};

bool DFS(int row, int col, int c) {
    used[row][col] = true;
    color[row][col] = c;
    if (c == 1) red++;
    else blue--;

    for (int i = 0; i < 4; i++) {
        int nr = row + dr[i], nc = col + dc[i];
        if (0 <= nr && nr < H && 0 <= nc && nc < W && !used[nr][nc] && !DFS(nr, nc, -c)) {
            return false;
        }
    }

    return true;
}

void Solve() {
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') used[i][j] = true;
        }
    }

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (!used[i][j] && !DFS(i, j, 1)) {
                cout << "NO" << endl;
                return;
            }
        }
    }

    if ((red + blue) % 2 == 0) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}

int main() {
    cin >> H >> W;

    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }

    Solve();

    return 0;
}
0