結果

問題 No.226 0-1パズル
ユーザー umimelumimel
提出日時 2022-04-10 15:34:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,325 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 171,044 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-04 12:08:53
合計ジャッジ時間 2,526 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll N_MAX = 2e5;

ll pow_mod(ll x, ll n, ll mod){
    ll ret = 1;
    while(n > 0){
        if(n & 1) ret = (ret*x)%mod;
        x = x*x%mod;
        n >>=1;
    }
    return ret;
}

int main(){
    ll h, w;
    cin >> h >> w;

    vector<vector<ll>> s(h, vector<ll>(w));
    rep(i, h) rep(j, w){
        char c;
        cin >> c;
        if(c == '0') s[i][j] = 0;
        else if(c == '1') s[i][j] = 1;
        else s[i][j] = 2;
    }

    ll ans = 0;
    //列が0101...の場合
    bool flag1 = true;
    ll cnt1 = 0;
    for(ll j=0; j<w; j++){
        //0スタート
        bool flag1_1 = true;
        for(ll i=0; i<h; i++){
            if(s[i][j] == 2) continue;
            if(s[i][j] != i%2){
                flag1_1 = false;
                break;
            }
        }

        //1スタート
        bool flag1_2 = true;
        for(ll i=0; i<h; i++){
            if(s[i][j] == 2) continue;
            if(s[i][j] != (i+1)%2){
                flag1_2 = false;
                break;
            }
        }

        if(!flag1_1 && !flag1_2){
            flag1 = false;
            break;
        }

        bool flag1_3 = true;
        rep(i, h){
            if(s[i][j] != 2){
                flag1_3 = false;
                break;
            }
        }

        if(flag1_3){
            cnt1++;
        }
    }

    if(flag1){
        ans += pow_mod(2, cnt1, MOD);
        ans %= MOD;
    }

    //行が01010...の場合
    bool flag2 = true;
    ll cnt2 = 0;
    for(ll i=0; i<h; i++){
        //0スタート
        bool flag2_1 = true;
        for(ll j=0; j<w; j++){
            if(s[i][j] == 2) continue;
            if(s[i][j] != j%2){
                flag2_1 = false;
                break;
            }
        }

        //1スタート
        bool flag2_2 = true;
        for(ll j=0; j<w; j++){
            if(s[i][j] == 2) continue;
            if(s[i][j] != (j+1)%2){
                flag2_2 = false;
                break;
            }
        }

        if(!flag2_1 && !flag2_2){
            flag2 = false;
            break;
        }

        bool flag2_3 = true;
        rep(j, w){
            if(s[i][j] != 2){
                flag2_3 = false;
                break;
            }
        }

        if(flag2_3){
            cnt2++;
        }
    }

    if(flag2){
        ans += pow_mod(2, cnt2, MOD);
        ans %= MOD;
    }

    //重複を削除
    //0スタート
    bool flag3_1 = true;
    for(ll i=0; i<h; i++){
        for(ll j=0; j<w; j++){
            if(s[i][j] == 2) continue;
            if(s[i][j] != (i+j)%2){
                flag3_1 = false;
            }
        }
    }

    //1スタート
    bool flag3_2 = true;
    rep(i, h) rep(j, w){
        if(s[i][j] == 2) continue;
        if(s[i][j] != (i+j+1)%2){
            flag3_2 = false;
        }
    }

    if(flag3_1){
        ans = (ans-1+MOD)%MOD;
    }

    if(flag3_2){
        ans = (ans-1+MOD)%MOD;
    }

    cout << ans << endl;
}
0