結果

問題 No.226 0-1パズル
ユーザー koyumeishikoyumeishi
提出日時 2015-08-19 21:28:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,658 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 83,600 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-04 12:07:36
合計ジャッジ時間 1,634 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <bitset>
using namespace std;

#define MOD 1000000007

ostream& operator << (ostream& os, const vector<long long>& v){
	for(int i=0; i<v.size(); i++){
		os << v[i] << " ";
	}
	os << endl;
	return os;
}

int main(){
	int h,w;
	cin >> h >> w;
	vector<string> s(h);
	for(int i=0; i<h; i++){
		cin >> s[i];
	}

	auto check = [&](int r, int type) -> int{
		for(int i=0; i<w; i++){
			if(s[r][i] == '?') continue;
			if(s[r][i]-'0' != (i&1)^type ){
				return 0;
			}
		}
		return 1;
	};

	vector<vector<long long>> dp(h, vector<long long>(2, 0));
	dp[0][0] = check(0, 0);
	dp[0][1] = check(0, 1);

	for(int i=1; i<h; i++){
		for(int j=0; j<2; j++){
			if( check(i,j) ){
				dp[i][j] = (dp[i][j]+dp[i-1][j]) % MOD;
			}
			if( check(i,j^1) ){
				dp[i][j^1] = (dp[i][j^1]+dp[i-1][j]) % MOD;
			}
		}
	}

	long long ans = (dp[h-1][0] + dp[h-1][1]) % MOD;

	vector<vector<long long>> dp_(2, vector<long long>(2, 0));
	for(int i=0; i<w; i++){
		vector<vector<long long>> dp__(2, vector<long long>(2, 0));
		for(int k=0; k<2; k++){
			bool ok = true;
			for(int r=0; r<h; r++){
				if(s[r][i]=='?') continue;
				if(s[r][i]-'0'== k^(r&1) ) continue;
				ok = false;
				break;
			}
			if(ok){
				if(i==0){
					dp__[k][0] += 1;
				}else for(int j=0; j<2; j++){
					for(int l=0; l<2; l++){
						dp__[k][j==k || l] += dp_[j][l];
						dp__[k][j==k || l] %= MOD;
					}
				}
			}
		}
		swap(dp_, dp__);
	}

	ans += dp_[0][1] + dp_[1][1];

	cout << ans % MOD << endl;
	return 0;
}
0