結果

問題 No.226 0-1パズル
ユーザー Yang33Yang33
提出日時 2018-08-24 14:42:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,687 bytes
コンパイル時間 1,521 ms
コンパイル使用メモリ 152,264 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-04 12:07:42
合計ジャッジ時間 2,233 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 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 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 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,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/24  Problem: yukicoder 226  / Link: http://yukicoder.me/problems/no/226  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----

DPかとおもったけどお絵かきしたらおーってなった

----解説ここまで---- */

template<class T>void rotate2dR(vector<vector<T>>& a) { // rorateToR
	vector<vector<T>> res(a[0].size(), vector<T>(a.size()));
	FOR(i, 0, (int)a.size())FOR(j, 0, (int)a[0].size())res[j][a.size() - i - 1] = a[i][j];
	a = res;
}

LL decideTopGrid(const vector<vector<char>>& a) {
	LL ret = 1; // e 
	int H = SZ(a), W = SZ(a[0]);
	// 2^W個ありえます
	FOR(j, 0, W) {
		int state = 0;
		FOR(b, 0, 2) {
			char top = '0' + b;
			if (isdigit(a[0][j]) && a[0][j] != top)continue;

			bool ok = 1;
			FOR(i, 1, H) {
				if (a[i][j] == '?')continue;
				if (i & 1)ok &= (a[i][j] != top);
				else ok &= (a[i][j] == top);
			}
			state += ok;
		}

		ret *= state;
		ret %= MOD;
	}
	return ret;
}

// [0,2] 市松模様の個数
LL countCheckeredPattern(const vector<vector<char>>& a) {
	int H = SZ(a), W = SZ(a[0]);
	LL ret = 0;
	FOR(b, 0, 2) {
		bool ok = 1;
		char top = '0' + b;
		if (a[0][0] == '?' || a[0][0] == top) {
			FOR(j, 0, W) {
				FOR(i, 0, H) {
					if (a[i][j] == '?')continue;
					if ((i^j) & 1) {
						ok &= (a[i][j] != top);
					}
					else {
						ok &= (a[i][j] == top);
					}
				}
			}
			ret += ok;
		}
	}
	return ret;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int H, W; cin >> H >> W;
	vector<vector<char>>a(H, vector<char>(W));
	FOR(i, 0, H) {
		FOR(j, 0, W) {
			cin >> a[i][j];
		}
	}

	LL ans = decideTopGrid(a);
	rotate2dR(a);
	ans += decideTopGrid(a);
	ans %= MOD;
	ans -= countCheckeredPattern(a);

	ans += MOD; ans %= MOD;
	cout << ans << "\n";

	return 0;
}
0