結果

問題 No.611 Day of the Mountain
ユーザー pekempeypekempey
提出日時 2017-12-11 00:51:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 370 ms / 2,017 ms
コード長 2,643 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 77,480 KB
実行使用メモリ 158,928 KB
最終ジャッジ日時 2023-08-20 10:01:51
合計ジャッジ時間 3,417 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 336 ms
158,584 KB
testcase_01 AC 305 ms
158,636 KB
testcase_02 AC 291 ms
158,584 KB
testcase_03 AC 370 ms
158,688 KB
testcase_04 AC 41 ms
158,908 KB
testcase_05 AC 42 ms
158,692 KB
testcase_06 AC 42 ms
158,928 KB
testcase_07 AC 41 ms
158,880 KB
testcase_08 AC 42 ms
158,700 KB
testcase_09 AC 41 ms
158,672 KB
testcase_10 AC 41 ms
158,564 KB
testcase_11 AC 41 ms
158,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

constexpr int mod = 201712111;

struct Modint {
  int n;
  Modint(int n = 0) : n(n) {}
};

Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % mod); }
Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
Modint &operator*=(Modint &a, Modint b) { return a = a * b; }

Modint dp[303][1 << 17];
char g[1000][1000];

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

  for (int i = 0; i < h; i++) {
    scanf("%s", g[i]);
  }

  if (h < w) {
    for (int i = 0; i < h; i++) {
      for (int j = i; j < w; j++) {
        swap(g[i][j], g[j][i]);
      }
    }
    swap(h, w);
  }

  vector<vector<int>> dist(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int val = g[i][j] == '?' ? 1 : g[i][j] - '0';
      if (i == 0 && j == 0) {
        dist[i][j] = val;
      } else if (i == 0) {
        dist[i][j] = dist[i][j - 1] + val;
      } else if (j == 0) {
        dist[i][j] = dist[i - 1][j] + val;
      } else {
        dist[i][j] = min(dist[i - 1][j], dist[i][j - 1]) + val;
      }
    }
  }

  cout << dist[h - 1][w - 1] << endl;

  dp[1][0] = 1;
  for (int i = 1; i < h * w; i++) {
    for (int j = 0; j < 1 << w; j++) {
      int y = i / w;
      int x = i % w;
      //   a
      // b c

      if (y == 0) {
        if (j >> x - 1 & 1) {
          dp[i + 1][j | 1 << x] += dp[i][j];
        } else {
          dp[i + 1][j] += dp[i][j];
        }

        // change
        if (g[y][x] == '?') {
          dp[i + 1][j | 1 << x] += dp[i][j] * 8;
        }
      } else if (x == 0) {
        dp[i + 1][j] += dp[i][j];

        // change
        if (g[y][x] == '?') {
          dp[i + 1][j | 1 << x] += dp[i][j] * 8;
        }
      } else {
        int val = g[y][x] == '?' ? 1 : g[y][x] - '0';
        int a = dist[y - 1][x] + (j >> x & 1);
        int b = dist[y][x - 1] + (j >> x - 1 & 1);
        if (dist[y][x] < min(a, b) + val) {
          dp[i + 1][j | 1 << x] += dp[i][j];
        } else {
          dp[i + 1][j & ~(1 << x)] += dp[i][j];
        }

        // change
        if (g[y][x] == '?') {
          dp[i + 1][j | 1 << x] += dp[i][j] * 8;
        }
      }
    }
  }

  Modint ans = 0;
  for (int i = 0; i < 1 << w; i++) {
    if (~i >> (w - 1) & 1) {
      ans += dp[h*w][i];
    }
  }

  cout << ans.n << endl;
}
0