結果

問題 No.611 Day of the Mountain
ユーザー rickythetarickytheta
提出日時 2017-12-11 19:26:18
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,432 bytes
コンパイル時間 1,190 ms
コンパイル使用メモリ 146,948 KB
実行使用メモリ 4,716 KB
最終ジャッジ日時 2023-08-20 10:08:26
合計ジャッジ時間 3,051 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 218 ms
4,632 KB
testcase_02 AC 201 ms
4,540 KB
testcase_03 AC 307 ms
4,540 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 201712111ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int h,w;
char mp[353][353];

int dist[353][353];
bool pass[353][353];

int dp[2][1<<18];

int main(){
  scanf("%d%d",&h,&w);
  REP(i,h)scanf("%s",mp[i]);
  if(h<w){
    swap(h,w);
    REP(i,353)REP(j,i)swap(mp[i][j], mp[j][i]);
  }
  assert(w <= 18);
  // precalc
  dist[0][0] = mp[0][0]=='?' ? 1 : mp[0][0]-'0';
  REP(i,h)REP(j,w)if(i!=0 || j!=0){
    dist[i][j] = 252521;
    int c = mp[i][j]=='?' ? 1 : mp[i][j]-'0';
    if(i>0)CHMIN(dist[i][j], dist[i-1][j]+c);
    if(j>0)CHMIN(dist[i][j], dist[i][j-1]+c);
  }
  pass[h-1][w-1] = true;
  FORR(i,0,h)FORR(j,0,w)if(i!=h-1 || j!=w-1){
    if(i<h-1){
      int ni = i+1, nj = j;
      int c = mp[ni][nj]=='?' ? 1 : mp[ni][nj]-'0';
      if(dist[ni][nj]==dist[i][j]+c && pass[ni][nj])pass[i][j] = true;
    }
    if(j<w-1){
      int ni = i, nj = j+1;
      int c = mp[ni][nj]=='?' ? 1 : mp[ni][nj]-'0';
      if(dist[ni][nj]==dist[i][j]+c && pass[ni][nj])pass[i][j] = true;
    }
  }
  // bit dp
  REP(i,1<<w)dp[0][i] = 0;
  dp[0][1] = 1;
  REP(y,h)REP(x,w){
    REP(i,1<<w)dp[1][i] = 0;
    int del = ((1<<w)-1) ^ (1<<x);
    int chk = 1<<x | (x>0 ? (1<<(x-1)) : 0);
    REP(msk,1<<w){
      int nbit = (msk & chk) > 0 && pass[y][x];
      int nmsk = (msk & del) ^ (nbit << x);
      dp[1][nmsk] = (dp[1][nmsk] + dp[0][msk]) % MOD;
    }
    if(mp[y][x]=='?'){
      REP(msk,1<<w){
        dp[1][msk&del] = (dp[1][msk&del] + dp[0][msk]*8 % MOD) % MOD;
      }
    }
    REP(i,1<<w)dp[0][i] = dp[1][i];
  }
  int ans = 0;
  REP(i,1<<w)if((i>>(w-1))&1){
    ans = (ans + dp[0][i]) % MOD;
  }
  printf("%d\n%d\n",dist[h-1][w-1],ans);
  return 0;
}
0