結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 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 44 ms
6,940 KB
testcase_06 AC 42 ms
6,940 KB
testcase_07 AC 41 ms
6,940 KB
testcase_08 AC 40 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 41 ms
6,940 KB
testcase_11 AC 44 ms
6,940 KB
testcase_12 AC 44 ms
6,944 KB
testcase_13 AC 43 ms
6,944 KB
testcase_14 AC 43 ms
6,940 KB
testcase_15 AC 43 ms
6,940 KB
testcase_16 AC 43 ms
6,944 KB
testcase_17 AC 43 ms
6,940 KB
testcase_18 AC 43 ms
6,940 KB
testcase_19 AC 43 ms
6,940 KB
testcase_20 AC 41 ms
6,944 KB
testcase_21 AC 42 ms
6,944 KB
testcase_22 AC 42 ms
6,940 KB
testcase_23 AC 42 ms
6,940 KB
testcase_24 AC 43 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int h, w;
    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++;
                for(int k = -1; k <= 1; k++){
                    for(int l = -1; l <= 1; l++){
                        if(inRange(0+k, 0, h)&&inRange(j+l, 0, w)){
                            t[0+k][j+l] = 1-t[0+k][j+l];
                        }
                    }
                }
            }
        }
        for(int i = 1; i < h; i++){
            if((s>>(w+i-1))&1){
                tmp++;
                for(int k = -1; k <= 1; k++){
                    for(int l = -1; l <= 1; l++){
                        if(inRange(i+k, 0, h)&&inRange(0+l, 0, w)){
                            t[i+k][0+l] = 1-t[i+k][0+l];
                        }
                    }
                }
            }
        }
        for(int i = 1; i < h; i++){
            for(int j = 1; j < w; j++){
                if(t[i-1][j-1]){
                    tmp++;
                    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)){
                                t[i+k][j+l] = 1-t[i+k][j+l];
                            }
                        }
                    }
                }
            }
        }
        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