結果

問題 No.460 裏表ちわーわ
ユーザー fura
提出日時 2020-08-07 01:17:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 2,048 ms
コンパイル使用メモリ 196,252 KB
最終ジャッジ日時 2025-01-12 15:35:31
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |         int h,w; scanf("%d%d",&h,&w);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:19:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |                 int a; scanf("%d",&a);
      |                        ~~~~~^~~~~~~~~

ソースコード

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