結果

問題 No.307 最近色塗る問題多くない?
ユーザー roarisroaris
提出日時 2019-12-08 10:41:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 338 ms / 4,000 ms
コード長 1,815 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 175,168 KB
実行使用メモリ 4,512 KB
最終ジャッジ日時 2023-08-28 16:21:14
合計ジャッジ時間 5,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 26 ms
4,376 KB
testcase_09 AC 10 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 9 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 22 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 13 ms
4,376 KB
testcase_18 AC 33 ms
4,376 KB
testcase_19 AC 29 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 25 ms
4,380 KB
testcase_22 AC 23 ms
4,376 KB
testcase_23 AC 157 ms
4,376 KB
testcase_24 AC 63 ms
4,380 KB
testcase_25 AC 105 ms
4,380 KB
testcase_26 AC 193 ms
4,512 KB
testcase_27 AC 338 ms
4,380 KB
testcase_28 AC 36 ms
4,380 KB
testcase_29 AC 15 ms
4,380 KB
testcase_30 AC 195 ms
4,376 KB
testcase_31 AC 125 ms
4,376 KB
testcase_32 AC 37 ms
4,380 KB
testcase_33 AC 11 ms
4,376 KB
testcase_34 AC 45 ms
4,380 KB
testcase_35 AC 45 ms
4,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int H, W;
int A[210][210];

void bfs(int sx, int sy, int c1, int c2) {
    if (c1==c2) return;
    
    queue<P> q;
    q.push(P(sx, sy));
    A[sx][sy] = c2;
    
    while (q.size()) {
        P p = q.front(); q.pop();
        int cx=p.first, cy=p.second;
        
        for (int i=0; i<4; i++) {
            int nx=cx+dx[i], ny=cy+dy[i];
            
            if (!(0<=nx && nx<H && 0<=ny && ny<W)) continue;
            
            if (A[nx][ny]==c1) {
                q.push(P(nx, ny));
                A[nx][ny] = c2;
            }
        }
    }
    
    return;
}

bool check() {
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            if (A[i][j]!=A[0][0]) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    cin >> H >> W;
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            cin >> A[i][j];
        }
    }
    
    int Q; cin >> Q;
    vector<T> RCX;
    
    for (int i=0; i<Q; i++) {
        int Ri, Ci, Xi; cin >> Ri >> Ci >> Xi;
        RCX.push_back(make_tuple(Ri, Ci, Xi));
    }
    
    for (int i=0; i<Q; i++) {
        int Ri=get<0>(RCX[i]), Ci=get<1>(RCX[i]), Xi=get<2>(RCX[i]);
        bfs(Ri-1, Ci-1, A[Ri-1][Ci-1], Xi);
        
        if (check()) {
            for (int i=0; i<H; i++) {
                for (int j=0; j<W; j++) {
                    cout << get<2>(RCX[Q-1]) << " ";
                }
                cout << endl;
            }
            exit(0);
        }
    }
    
    for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            cout << A[i][j] << " ";
        }
        cout << endl;
    }
}
0