結果

問題 No.460 裏表ちわーわ
ユーザー face4face4
提出日時 2019-11-27 22:02:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,443 bytes
コンパイル時間 698 ms
コンパイル使用メモリ 73,672 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 14:28:40
合計ジャッジ時間 2,349 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 39 ms
6,940 KB
testcase_06 AC 38 ms
6,944 KB
testcase_07 AC 38 ms
6,944 KB
testcase_08 AC 37 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 37 ms
6,940 KB
testcase_11 AC 40 ms
6,944 KB
testcase_12 AC 40 ms
6,940 KB
testcase_13 AC 39 ms
6,944 KB
testcase_14 AC 39 ms
6,940 KB
testcase_15 AC 40 ms
6,944 KB
testcase_16 AC 40 ms
6,940 KB
testcase_17 AC 39 ms
6,944 KB
testcase_18 AC 39 ms
6,940 KB
testcase_19 AC 40 ms
6,940 KB
testcase_20 AC 38 ms
6,944 KB
testcase_21 AC 38 ms
6,944 KB
testcase_22 AC 38 ms
6,940 KB
testcase_23 AC 38 ms
6,940 KB
testcase_24 AC 38 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)

int h, w;

void flip(int i, int j, vector<vector<int>> &v){
    for(int k = -1; k <= 1; k++){
        for(int l = -1; l <= 1; l++){
            if(inRange(i+k, 0, h)&&inRange(j+l, 0, w)){
                v[i+k][j+l] = 1-v[i+k][j+l];
            }
        }
    }
}

int main(){
    cin >> h >> w;
    vector<vector<int>> v(h, vector<int>(w, 0));
    for(int i = 0; i < h*w; i++)    cin >> v[i/w][i%w];
    int ans = 1<<30;
    for(int s = 0; s < 1<<(h+w-1); s++){
        int tmp = 0;
        vector<vector<int>> t = v;
        for(int j = 0; j < w; j++){
            if((s>>j)&1){
                tmp++;
                flip(0, j, t);
            }
        }
        for(int i = 1; i < h; i++){
            if((s>>(w+i-1))&1){
                tmp++;
                flip(i, 0, t);
            }
        }
        for(int i = 1; i < h; i++){
            for(int j = 1; j < w; j++){
                if(t[i-1][j-1]){
                    tmp++;
                    flip(i, j, t);
                }
            }
        }
        bool valid = true;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                valid &= t[i][j]==0;
            }
        }
        if(valid)   ans = min(ans, tmp);
    }
    if(ans == 1<<30)    cout << "Impossible" << endl;
    else                cout << ans << endl;
    return 0;
}
0