結果
| 問題 |
No.2003 Frog on Grid
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2022-06-12 14:37:51 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 167 ms / 2,000 ms |
| コード長 | 2,391 bytes |
| コンパイル時間 | 262 ms |
| コンパイル使用メモリ | 32,584 KB |
| 実行使用メモリ | 117,076 KB |
| 最終ジャッジ日時 | 2024-09-23 02:59:05 |
| 合計ジャッジ時間 | 2,431 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <stdio.h>
#include <stdlib.h>
const int Mod = 998244353;
int main()
{
int i, j, H, W, K;
char **c, **c_tmp;
scanf("%d %d %d", &H, &W, &K);
c_tmp = (char**)malloc(sizeof(char*) * (H + 2));
for (i = 1; i <= H; i++) {
c_tmp[i] = (char*)malloc(sizeof(char) * (W + 2));
scanf("%s", &(c_tmp[i][1]));
}
if (H <= W) {
W += K;
c = (char**)malloc(sizeof(char*) * (H + 2));
for (i = 0; i <= H + 1; i++) {
c[i] = (char*)malloc(sizeof(char) * (W + 2));
for (j = 0; j <= W + 1; j++) c[i][j] = 0;
}
for (i = 1; i <= H; i++) for (j = K + 1; j <= W; j++) c[i][j] = c_tmp[i][j-K];
} else {
H ^= W;
W ^= H;
H ^= W;
W += K;
c = (char**)malloc(sizeof(char*) * (H + 2));
for (i = 0; i <= H + 1; i++) {
c[i] = (char*)malloc(sizeof(char) * (W + 2));
for (j = 0; j <= W + 1; j++) c[i][j] = 0;
}
for (i = 1; i <= H; i++) for (j = K + 1; j <= W; j++) c[i][j] = c_tmp[j-K][i];
}
int tmp, **dp, **dp_sum_add, **dp_sum_sub;
dp = (int**)malloc(sizeof(int*) * (H + 2));
dp_sum_add = (int**)malloc(sizeof(int*) * (H + 2));
dp_sum_sub = (int**)malloc(sizeof(int*) * (H + 2));
for (i = 0; i <= H + 1; i++) {
dp[i] = (int*)malloc(sizeof(int) * (W + 2));
dp_sum_add[i] = (int*)malloc(sizeof(int) * (W + 2));
dp_sum_sub[i] = (int*)malloc(sizeof(int) * (W + 2));
}
for (i = 0; i <= H + 1; i++) {
for (j = 0; j <= W + 1; j++) {
dp[i][j] = 0;
dp_sum_add[i][j] = 0;
dp_sum_sub[i][j] = 0;
}
}
dp[1][K+1] = 1;
dp_sum_add[1][K+1] = 1;
dp_sum_sub[1][K+1] = 1;
for (i = 1, j = K + 2, tmp = 1; 1; j++) {
if (j > W) {
i++;
if (i > H) break;
j = 0;
tmp = 0;
}
tmp += dp_sum_add[i-1][j];
if (j >= K + 1) tmp -= dp_sum_sub[i][j-K-1];
if (tmp >= Mod) tmp -= Mod;
else if (tmp < 0) tmp += Mod;
if (c[i][j] == '.') dp[i][j] = tmp;
else dp[i][j] = 0;
dp_sum_add[i][j] = dp_sum_add[i-1][j] + dp[i][j];
if (i - K >= 1) dp_sum_add[i][j] -= dp[i-K][j];
if (dp_sum_add[i][j] >= Mod) dp_sum_add[i][j] -= Mod;
else if (dp_sum_add[i][j] < 0) dp_sum_add[i][j] += Mod;
dp_sum_sub[i][j] = dp_sum_sub[i-1][j+1] + dp[i][j];
if (i - K - 1 >= 1 && j + K + 1 <= W) dp_sum_sub[i][j] -= dp[i-K-1][j+K+1];
if (dp_sum_sub[i][j] >= Mod) dp_sum_sub[i][j] -= Mod;
else if (dp_sum_sub[i][j] < 0) dp_sum_sub[i][j] += Mod;
tmp += dp[i][j];
if (tmp >= Mod) tmp -= Mod;
}
printf("%d\n", dp[H][W]);
fflush(stdout);
return 0;
}