結果

問題 No.86 TVザッピング(2)
ユーザー kimiyukikimiyuki
提出日時 2017-01-08 23:03:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,827 ms / 5,000 ms
コード長 1,621 bytes
コンパイル時間 798 ms
コンパイル使用メモリ 79,308 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 15:02:38
合計ジャッジ時間 5,841 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 296 ms
4,380 KB
testcase_10 AC 1,827 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 15 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1,459 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using namespace std;
bool solve(int h, int w, vector<string> const & f) {
    auto is_on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };
    auto pred = [&](int y, int x) { return is_on_field(y, x) and f[y][x] == '.'; };
    const int dy[] = { 0, -1, 0, 1 }; // counter-clockwise
    const int dx[] = { 1, 0, -1, 0 };
    int total = 0; repeat (y,h) repeat (x,w) total += pred(y, x);
    repeat (y0,h) repeat (x0,w) if (pred(y0, x0)) {
        repeat (d0,4) {
            int y = y0;
            int x = x0;
            set<pair<int, int> > used;
            for (int i = 0; i < 6; ) {
                if (not used.empty() and y == y0 and x == x0) {
                    if (used.size() == total) return true;
                    break;
                }
                int di = 0;
                for (; di < 2; ++ di) {
                    int ny = y + dy[(d0 + i + di) % 4];
                    int nx = x + dx[(d0 + i + di) % 4];
                    if (pred(ny, nx) and not used.count(make_pair(ny, nx))) {
                        y = ny;
                        x = nx;
                        i += di;
                        used.insert(make_pair(y, x));
                        break;
                    }
                }
                if (di == 2) break;
            }
        }
    }
    return false;
}
int main() {
    int h, w; cin >> h >> w;
    vector<string> f(h); repeat (y,h) cin >> f[y];
    cout << (solve(h, w, f) ? "YES" : "NO") << endl;
    return 0;
}
0