結果

問題 No.307 最近色塗る問題多くない?
ユーザー Mister
提出日時 2020-09-10 12:04:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 438 ms / 4,000 ms
コード長 1,649 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 83,992 KB
最終ジャッジ日時 2025-01-14 09:14:57
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

const std::vector<std::pair<int, int>> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void solve() {
    int h, w;
    std::cin >> h >> w;

    auto ss = vec(h, vec(w, 0));
    for (auto& s : ss) {
        for (auto& x : s) std::cin >> x;
    }

    bool allsame = false;

    int q;
    std::cin >> q;
    while (q--) {
        int sx, sy, c;
        std::cin >> sx >> sy >> c;

        if (allsame) {
            ss[0][0] = c;
            continue;
        }

        if (ss[--sx][--sy] == c) continue;

        std::queue<std::pair<int, int>> que;
        que.emplace(sx, sy);
        ss[sx][sy] = c;

        while (!que.empty()) {
            auto [x, y] = que.front();
            que.pop();

            for (auto [dx, dy] : dxys) {
                int nx = x + dx,
                    ny = y + dy;

                if (nx < 0 || h <= nx ||
                    ny < 0 || w <= ny ||
                    ss[nx][ny] == c) continue;

                ss[nx][ny] = c;
                que.emplace(nx, ny);
            }
        }

        bool judge = true;
        for (const auto& s : ss) {
            for (auto x : s) {
                if (x != ss[0][0]) judge = false;
            }
        }
        if (judge) allsame = true;
    }

    for (const auto& s : ss) {
        for (auto x : s) std::cout << (allsame ? ss[0][0] : x) << " ";
        std::cout << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0