結果

問題 No.460 裏表ちわーわ
ユーザー mamekin
提出日時 2017-03-03 19:00:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,488 bytes
コンパイル時間 1,216 ms
コンパイル使用メモリ 111,720 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 05:13:40
合計ジャッジ時間 3,806 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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