結果

問題 No.611 Day of the Mountain
ユーザー mamekinmamekin
提出日時 2018-01-14 11:52:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,190 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 121,668 KB
実行使用メモリ 4,516 KB
最終ジャッジ日時 2023-08-25 17:10:55
合計ジャッジ時間 2,848 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
4,380 KB
testcase_01 AC 138 ms
4,512 KB
testcase_02 AC 129 ms
4,516 KB
testcase_03 AC 167 ms
4,472 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 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;
const int dy[] = {-1, 1, 0, 0};
const int dx[] = {0, 0, -1, 1};

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));
    multiset<tuple<int, int, int> > ms;
    minCost[0][0] = grid[0][0];
    ms.insert(make_tuple(minCost[0][0], 0, 0));
    while(!ms.empty()){
        int cost, y, x;
        tie(cost, y, x) = *ms.begin();
        ms.erase(ms.begin());
        if(minCost[y][x] < cost)
            continue;

        for(int i=0; i<4; ++i){
            int y2 = y + dy[i];
            int x2 = x + dx[i];
            if(!(0 <= y2 && y2 < h && 0 <= x2 && x2 < w))
                continue;

            int cost2 = cost + grid[y2][x2];
            if(cost2 < minCost[y2][x2]){
                minCost[y2][x2] = cost2;
                ms.insert(make_tuple(cost2, y2, x2));
            }
        }
    }

    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