結果

問題 No.2708 Jewel holder
ユーザー ryota2357ryota2357
提出日時 2024-01-24 13:50:43
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,230 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 122,240 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-30 14:52:23
合計ジャッジ時間 2,369 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> a(h);
    for (int i = 0; i < h; ++i) {
        cin >> a[i];
    }
    assert(2 <= h && h <= 10);
    assert(2 <= w && w <= 10);
    for (int i = 0; i < h; ++i) {
        assert(a[i].length() == (size_t)w);
    }

    auto is_steppable = [&](int x, int y) {
        bool xx = 0 <= x && x < w;
        bool yy = 0 <= y && y < h;
        return xx && yy && (a[y][x] != '#');
    };

    int ans = 0;
    for (int bit = 0; bit < (1 << (h + w - 2)); ++bit) {
        int x = 0, y = 0, jewel = 1;
        for (int i = 0; i < (h + w - 2); ++i) {
            if (bit & (1 << i)) {
                x += 1;
            } else {
                y += 1;
            }
            if (!is_steppable(x, y)) {
                jewel = -1;
                break;
            }
            if (a[y][x] == 'o') {
                jewel += 1;
            } else { // a[y][x] == 'x'
                jewel -= 1;
            }
            if (jewel < 0) {
                break;
            }
        }
        if (jewel >= 0) {
            ans += 1;
        }
    }

    cout << ans << endl;
    return 0;
}
0