結果

問題 No.307 最近色塗る問題多くない?
ユーザー mamekinmamekin
提出日時 2015-12-20 20:25:48
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 170 ms / 4,000 ms
コード長 1,938 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 101,604 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-21 22:14:21
合計ジャッジ時間 3,075 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

int main()
{
    int h, w;
    cin >> h >> w;
    vector<vector<int> > a(h+2, vector<int>(w+2, -1));
    vector<int> cnt(2, 0);
    for(int y=1; y<=h; ++y){
        for(int x=1; x<=w; ++x){
            cin >> a[y][x];
            ++ cnt[a[y][x]];
        }
    }

    int query;
    cin >> query;
    while(--query >= 0){
        int y, x, b;
        cin >> y >> x >> b;
        if(a[y][x] == b)
            continue;

        if(cnt[0] == h * w || cnt[1] == h * w){
            if(query == 0){
                for(int y=1; y<=h; ++y){
                    for(int x=1; x<=w; ++x){
                        a[y][x] = b;
                    }
                }
            }
            continue;
        }

        queue<pair<int, int> > q;
        q.push(make_pair(y, x));
        a[y][x] = b;
        ++ cnt[b];
        -- cnt[b^1];

        while(!q.empty()){
            int y2 = q.front().first;
            int x2 = q.front().second;
            q.pop();

            for(int i=0; i<4; ++i){
                int y3 = y2 + dy[i];
                int x3 = x2 + dx[i];
                if(a[y3][x3] == (b ^ 1)){
                    q.push(make_pair(y3, x3));
                    a[y3][x3] = b;
                    ++ cnt[b];
                    -- cnt[b^1];
                }
            }
        }
    }
    
    for(int y=1; y<=h; ++y){
        cout << a[y][1];
        for(int x=2; x<=w; ++x){
            cout << ' ' << a[y][x];
        }
        cout << endl;
    }

    return 0;
}
0