結果

問題 No.2003 Frog on Grid
ユーザー SSRSSSRS
提出日時 2022-07-08 22:29:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 661 ms / 2,000 ms
コード長 2,192 bytes
コンパイル時間 2,519 ms
コンパイル使用メモリ 180,572 KB
実行使用メモリ 385,956 KB
最終ジャッジ日時 2023-08-27 22:14:18
合計ジャッジ時間 7,158 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 61 ms
19,508 KB
testcase_18 AC 189 ms
49,040 KB
testcase_19 AC 266 ms
152,276 KB
testcase_20 AC 661 ms
385,956 KB
testcase_21 AC 257 ms
152,456 KB
testcase_22 AC 642 ms
385,724 KB
testcase_23 AC 205 ms
49,064 KB
testcase_24 AC 208 ms
48,208 KB
testcase_25 AC 145 ms
48,408 KB
testcase_26 AC 147 ms
48,616 KB
testcase_27 AC 213 ms
48,740 KB
testcase_28 AC 145 ms
48,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<char>> C(H, vector<char>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> C[i][j];
    }
  }
  vector<vector<int>> R(H + W - 1);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      R[i + j].push_back(i);
    }
  }
  vector<vector<long long>> dp(H, vector<long long>(W, 0));
  vector<vector<long long>> tmp(H, vector<long long>(W, 0));
  vector<vector<long long>> sum1(H + 1, vector<long long>(W, 0));
  vector<vector<long long>> sum2(H, vector<long long>(W + 1, 0));
  vector<vector<long long>> sum(H + W - 1);
  dp[0][0] = 1;
  sum1[1][0] = 1;
  sum2[0][1] = 1;
  sum[0] = {0, 1};
  for (int i = 1; i < H + W - 1; i++){
    int cnt = R[i].size();
    sum[i].resize(cnt + 1);
    sum[i][0] = 0;
    for (int j = 0; j < cnt; j++){
      int r = R[i][j], c = i - r;
      if (r > 0){
        tmp[r][c] = tmp[r - 1][c] + dp[r - 1][c];
        tmp[r][c] += sum2[r][c] - sum2[r][max(c - K, 0)];
        if (i > K){
          int p1 = lower_bound(R[i - K - 1].begin(), R[i - K - 1].end(), max(r - K - 1, 0)) - R[i - K - 1].begin();
          int p2 = lower_bound(R[i - K - 1].begin(), R[i - K - 1].end(), r) - R[i - K - 1].begin();
          tmp[r][c] -= sum[i - K - 1][p2] - sum[i - K - 1][p1];
        }
      } else {
        tmp[r][c] = tmp[r][c - 1] + dp[r][c - 1];
        tmp[r][c] += sum1[r][c] - sum1[max(r - K, 0)][c];
        if (i > K){
          int p1 = lower_bound(R[i - K - 1].begin(), R[i - K - 1].end(), max(r - K, 0)) - R[i - K - 1].begin();
          int p2 = lower_bound(R[i - K - 1].begin(), R[i - K - 1].end(), r + 1) - R[i - K - 1].begin();
          tmp[r][c] -= sum[i - K - 1][p2] - sum[i - K - 1][p1];
        }
      }
      tmp[r][c] %= MOD;
      if (tmp[r][c] < 0){
        tmp[r][c] += MOD;
      }
      if (C[r][c] == '.'){
        dp[r][c] = tmp[r][c];
      }
      sum1[r + 1][c] = (sum1[r][c] + dp[r][c]) % MOD;
      sum2[r][c + 1] = (sum2[r][c] + dp[r][c]) % MOD;
      sum[i][j + 1] = (sum[i][j] + dp[r][c]) % MOD;
    }
  }
  cout << dp[H - 1][W - 1] << endl;
}
0