結果
| 問題 |
No.611 Day of the Mountain
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-01-14 11:52:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,190 bytes |
| コンパイル時間 | 1,161 ms |
| コンパイル使用メモリ | 122,268 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-24 02:31:56 |
| 合計ジャッジ時間 | 2,577 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 7 WA * 2 |
ソースコード
#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;
}