結果

問題 No.86 TVザッピング(2)
コンテスト
ユーザー Kitsune Minako
提出日時 2026-02-19 19:12:03
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3,706 ms / 5,000 ms
コード長 2,400 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,011 ms
コンパイル使用メモリ 220,936 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-19 19:12:12
合計ジャッジ時間 7,810 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using arr = array<int, 3>;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

void Tsukikage() {
    int n, m;
    cin >> n >> m;
    vector g(n + 1, vector<char>(m + 1));
    int cnt = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
            cnt += g[i][j] == '.';
        }
    
    int res = 0, sx, sy;
    bool ok = 0;
    auto dfs = [&](auto dfs, int x, int y, int d) -> void {
        for (int i = 0; i < 2; i++) {
            int xx = x + dx[(d + i) % 4], yy = y + dy[(d + i) % 4];
            if (xx < 1 or xx > n or yy < 1 or yy > m) continue;
            if (g[xx][yy] != '.') continue;

            res++;
            g[xx][yy] = '@';
            if (xx == sx and yy == sy) {
                ok = 1;
                return;
            }
            dfs(dfs, xx, yy, (d + i) % 4);
                
            break;
        }
    };

    auto undo = [&](auto undo, int x, int y) -> void {
        if (g[x][y] != '@') return;
        g[x][y] = '.';

        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i], yy = y + dy[i];
            if (xx < 1 or xx > n or yy < 1 or yy > m) continue;
            if (g[xx][yy] != '@') continue;
            undo(undo, xx, yy);
        }
    };

    auto Write = [&]() {  // debug
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) cout << g[i][j];
            cout << "\n";
        }
    };

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (g[i][j] == '#') continue;

            // Write();

            for (int d = 0; d < 4; d++) {
                res = 0, sx = i, sy = j;
                ok = 0;
                
                dfs(dfs, i, j, d);
                
                if (res == cnt and ok) {
                    cout << "YES\n";
                    return;
                }
                
                g[i][j] = '@';  // 填充逻辑是最后一个填充起点,假如填充失败则起点是'.'而非'@',导致undo也会失败,所以直接给起点覆盖'@'
                undo(undo, i, j);
            }
        }

    cout << "NO\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // int _;
    // for (cin >> _; _; _--) 
        Tsukikage();
    return 0;
}
0