結果

問題 No.2708 Jewel holder
ユーザー futamegawafutamegawa
提出日時 2024-03-31 14:14:10
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,692 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 95,956 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-31 14:14:13
合計ジャッジ時間 2,080 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

const int MAX_H = 10;
const int MAX_W = 10;

int H, W;
vector<vector<char>> grid;
int count_paths = 0;

void dfs(int i, int j, int gems) {
    // マス(i, j)が右下のマス(H-1, W-1)であり、かつ宝石を没収されない経路であれば、経路の数をインクリメントする
    if (i == H - 1 && j == W - 1 && gems >= 0) {
        count_paths++;
        return;
    }

    // マス(i, j)がグリッドの範囲内であり、かつ壁(#)でない場合
    if (0 <= i && i < H && 0 <= j && j < W && grid[i][j] != '#') {
        // 右に進む
        if (j + 1 < W) {
            int new_gems = gems;
            if (grid[i][j + 1] == 'o') new_gems++;
            else if (grid[i][j + 1] == 'x') new_gems--;

            // 宝石が負の値になる場合は経路を無効にする
            if (new_gems >= 0) {
                dfs(i, j + 1, new_gems);
            }
        }
        // 下に進む
        if (i + 1 < H) {
            int new_gems = gems;
            if (grid[i + 1][j] == 'o') new_gems++;
            else if (grid[i + 1][j] == 'x') new_gems--;

            // 宝石が負の値になる場合は経路を無効にする
            if (new_gems >= 0) {
                dfs(i + 1, j, new_gems);
            }
        }
    }
}

int main() {
    cin >> H >> W;

    // グリッドの入力
    grid.resize(H, vector<char>(W));
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cin >> grid[i][j];
        }
    }

    // 初期位置は(0, 0)で、宝石の初期個数は1つ
    dfs(0, 0, 1);

    cout << count_paths << endl;

    return 0;
}
0