結果

問題 No.460 裏表ちわーわ
ユーザー furafura
提出日時 2020-08-07 01:17:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 204,456 KB
実行使用メモリ 5,880 KB
最終ジャッジ日時 2023-10-22 04:18:50
合計ジャッジ時間 2,845 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

inline int popcount(unsigned int x){
	x-=(x>>1)&0x55555555;
	x=(x&0x33333333)+((x>>2)&0x33333333);
	return ((x+(x>>4)&0x0f0f0f0f)*0x01010101)>>24;
}

int main(){
	int h,w; scanf("%d%d",&h,&w);
	int B[8]={};
	rep(i,h) rep(j,w) {
		int a; scanf("%d",&a);
		B[i]|=a<<j;
	}

	vector<int> cost(1<<w,INF);
	rep(S,1<<w){
		int pttn={};
		rep(j,w) if(S>>j&1) {
			if(j> 0 ) pttn^=1<<(j-1);
			pttn^=1<<j;
			if(j<w-1) pttn^=1<<(j+1);
		}
		cost[pttn]=min(cost[pttn],popcount(S));
	}

	if(h==1){
		if(cost[B[0]]<INF) printf("%d\n",cost[B[0]]);
		else               puts("Impossible");
		return 0;
	}

	static int dp[8][1<<8][1<<8];
	rep(i,h) rep(S,1<<w) rep(T,1<<w) dp[i][S][T]=INF;
	rep(S,1<<w) dp[0][B[0]^S][B[1]^S]=cost[S];
	for(int i=1;i<h-1;i++){
		rep(S,1<<w) rep(T,1<<w) {
			dp[i][T^S][B[i+1]^S]=min(dp[i][T^S][B[i+1]^S],dp[i-1][S][T]+cost[S]);
		}
	}

	int ans=INF;
	rep(S,1<<w) ans=min(ans,dp[h-2][S][S]+cost[S]);
	if(ans<INF) printf("%d\n",ans);
	else        puts("Impossible");

	return 0;
}
0