結果

問題 No.611 Day of the Mountain
ユーザー mamekinmamekin
提出日時 2018-01-14 11:55:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,017 ms
コード長 2,757 bytes
コンパイル時間 1,134 ms
コンパイル使用メモリ 113,284 KB
実行使用メモリ 4,492 KB
最終ジャッジ日時 2023-08-25 17:10:58
合計ジャッジ時間 2,655 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
4,376 KB
testcase_01 AC 161 ms
4,492 KB
testcase_02 AC 155 ms
4,396 KB
testcase_03 AC 182 ms
4,404 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 201712111;
const int INF = INT_MAX / 2;

int main()
{
    int h, w;
    cin >> h >> w;
    vector<vector<int> > grid(h, vector<int>(w, -1));
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
             char c;
             cin >> c;
             if(c != '?')
                 grid[y][x] = c - '0';
        }
    }

    if(h < w){
        swap(h, w);
        vector<vector<int> > grid2(h, vector<int>(w));
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                grid2[y][x] = grid[x][y];
            }
        }
        grid.swap(grid2);
    }

    vector<vector<bool> > isUnknown(h, vector<bool>(w, false));
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            if(grid[y][x] == -1){
                grid[y][x] = 1;
                isUnknown[y][x] = true;
            }
        }
    }

    vector<vector<int> > minCost(h, vector<int>(w, INF));
    minCost[0][0] = grid[0][0];
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            if(y > 0)
                minCost[y][x] = min(minCost[y][x], minCost[y-1][x] + grid[y][x]);
            if(x > 0)
                minCost[y][x] = min(minCost[y][x], minCost[y][x-1] + grid[y][x]);
        }
    }

    vector<int> dp(1<<w, 0);
    dp[1] = 1;
    for(int y=0; y<h; ++y){
        for(int x=0; x<w; ++x){
            if(y == 0 && x == 0)
                continue;

            vector<int> nextDp(1<<w, 0);
            for(int i=0; i<(1<<w); ++i){
                bitset<32> bs(i);
                bitset<32> bs2 = (bs << 1) & bitset<32>((1 << w) - 1);
                if(isUnknown[y][x]){
                    nextDp[bs2.to_ulong()] += dp[i] * 8;
                    nextDp[bs2.to_ulong()] %= MOD;
                }

                if((y > 0 && bs[w-1] && minCost[y-1][x] + grid[y][x] == minCost[y][x]) ||
                   (x > 0 && bs[0] && minCost[y][x-1] + grid[y][x] == minCost[y][x])){
                    bs2[0] = true;
                }
                nextDp[bs2.to_ulong()] += dp[i];
                nextDp[bs2.to_ulong()] %= MOD;
            }
            dp.swap(nextDp);
        }
    }

    int ans = 0;
    for(int i=1; i<(1<<w); i+=2){
        ans += dp[i];
        ans %= MOD;
    }
    cout << minCost[h-1][w-1] << endl << ans << endl;

    return 0;
}
0