結果

問題 No.460 裏表ちわーわ
ユーザー mamekinmamekin
提出日時 2017-03-03 19:00:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,488 bytes
コンパイル時間 2,301 ms
コンパイル使用メモリ 110,076 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 05:48:57
合計ジャッジ時間 4,586 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 60 ms
4,380 KB
testcase_06 AC 58 ms
4,380 KB
testcase_07 AC 64 ms
4,380 KB
testcase_08 AC 66 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 67 ms
4,380 KB
testcase_11 AC 60 ms
4,380 KB
testcase_12 AC 64 ms
4,380 KB
testcase_13 AC 68 ms
4,376 KB
testcase_14 AC 68 ms
4,380 KB
testcase_15 AC 68 ms
4,376 KB
testcase_16 AC 59 ms
4,380 KB
testcase_17 AC 68 ms
4,380 KB
testcase_18 AC 69 ms
4,376 KB
testcase_19 AC 69 ms
4,376 KB
testcase_20 AC 58 ms
4,376 KB
testcase_21 AC 67 ms
4,380 KB
testcase_22 AC 68 ms
4,376 KB
testcase_23 AC 67 ms
4,380 KB
testcase_24 AC 70 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#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>
#include <iterator>
using namespace std;

void flip(vector<vector<bool> >& a, int y, int x)
{
	int h = a.size();
	int w = a[0].size();

	for(int dy=-1; dy<=1; ++dy){
		for(int dx=-1; dx<=1; ++dx){
			int y2 = y + dy;
			int x2 = x + dx;
			if(0 <= y2 && y2 < h && 0 <= x2 && x2 < w)
				a[y2][x2] = !a[y2][x2];
		}
	}
}

int main()
{
	int h, w;
	cin >> h >> w;

	vector<vector<bool> > a(h, vector<bool>(w));
	for(int y=0; y<h; ++y){
		for(int x=0; x<w; ++x){
			int b;
			cin >> b;
			a[y][x] = (b == 1);
		}
	}

	int ans = INT_MAX;
	for(int i=0; i<(1<<(h+w-1)); ++i){
		bitset<32> bs(i);
		vector<vector<bool> > b = a;

		for(int y=0; y<h; ++y){
			if(bs[y])
				flip(b, y, 0);
		}
		for(int x=1; x<w; ++x){
			if(bs[h-1+x])
				flip(b, 0, x);
		}
		
		int cnt = bs.count();
		for(int y=1; y<h; ++y){
			for(int x=1; x<w; ++x){
				if(b[y-1][x-1]){
					flip(b, y, x);
					++ cnt;
				}
			}
		}

		if(b == vector<vector<bool> >(h, vector<bool>(w, false)))
			ans = min(ans, cnt);
	}

	if(ans < INT_MAX)
		cout << ans << endl;
	else
		cout << "Impossible" << endl;

	return 0;
}
0