結果

問題 No.460 裏表ちわーわ
ユーザー kotatsugame
提出日時 2020-03-05 10:52:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 709 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 68,344 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-10-14 00:52:15
合計ジャッジ時間 4,584 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    7 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
int H,W;
int A[9];
int dp[9][1<<16];
main()
{
	cin>>H>>W;
	for(int i=0;i<H;i++)
	{
		int t=0;
		for(int j=0;j<W;j++)
		{
			int a;cin>>a;
			t=t*2+a;
		}
		A[i]=t;
	}
	for(int i=0;i<=H;i++)for(int j=0;j<1<<2*W;j++)dp[i][j]=1e9;
	for(int i=0;i<1<<W;i++)dp[0][i<<W|A[0]]=0;
	for(int i=0;i<H;i++)for(int j=0;j<1<<2*W;j++)
	{
		for(int k=0;k<1<<W;k++)
		{
			int t=(k^k>>1^k<<1)&((1<<W)-1);
			if((j>>W)==t)
			{
				int nx=(j&(1<<W)-1^t)<<W|A[i+1]^t;
				dp[i+1][nx]=min(dp[i+1][nx],dp[i][j]+__builtin_popcount(k));
			}
		}
	}
	int ans=1e9;
	for(int j=0;j<1<<W;j++)ans=min(ans,dp[H][j]);
	if(ans<1e9)cout<<ans<<endl;
	else cout<<"Impossible"<<endl;
}
0