結果

問題 No.86 TVザッピング(2)
ユーザー codershifthcodershifth
提出日時 2015-08-26 08:38:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 3,749 bytes
コンパイル時間 1,192 ms
コンパイル使用メモリ 149,000 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 15:01:54
合計ジャッジ時間 2,242 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class TvZapping2 {
public:
    void solve(void) {
            int N,M;
            cin>>N>>M;

            int cnt = 0;
            vector<string> A(N);
            REP(i,N)
            {
                cin>>A[i];
                // 通過可能なボタンの総数を数えておく
                REP(j,M) if (A[i][j] == '.')
                    ++cnt;
            }

            //
            // 左に曲がる回数は以下のようなケースで最大 6 回まで
            //
            // ++++++++ +++
            // +      + + +
            // +      S++ +
            // +          +
            // ++++++++++++
            //
            // 閉路に含まれるマス数は 4*(N+M) 以下なのでそれ以上なら return
            // よって閉路探索時間と '.' 総数は K = 4*(N+M) 以下となる。
            if (cnt > 4*(N+M))
            {
                cout<<"NO"<<endl;
                return;
            }

            // sample 3 より最大一回は右に曲がってよいとしたとき、サイクルができればよさそう。
            // 以下の条件があるので計算量を落とせる
            //  * 右に曲がれるのはスタート地点のみ。スタート地点毎に計算をすれば左 or 真っ直ぐにしか進めないとみなせる
            //  * 左に曲がるとその先はもう進めなくなるので、それ以上すすめなくなるまで真っ直ぐ進む。
            // O(K^2) (K=4*(N+M))  = O(N*M)

            // 右回り
            const int dx[] = {-1,0,1,0};
            const int dy[] = {0,1,0,-1};

            // O(N*M)
            int sx, sy;
            function<bool(int,int,int,int)> search = [&](int x, int y, int d, int c) {
                for (int dd = 0; dd >= -1; --dd)
                {
                    int nd = (4+d+dd)%4;
                    int nx = x+dx[nd];
                    int ny = y+dy[nd];

                    if (nx < 0 || M <= nx || ny < 0 || N <= ny)
                        continue;

                    if (nx==sx && ny==sy)
                    {
                        if (cnt == c)
                            return true;
                        return false;
                    }
                    // 進めないなら左へ
                    if (A[ny][nx] != '.')
                        continue;

                    A[ny][nx] = '@'; // 通過フラグを立てておく
                    bool found = search(nx,ny,nd,c+1);
                    A[ny][nx] = '.';

                    if (found)
                        return true;
                    // まっすぐか左かしか移動しないので break する
                    break;
                }
                return false;
            };
            // O(N^2*M^2)
            REP(d, 4)
            {
                REP(y,N)
                REP(x,M)
                {
                    if (A[y][x] != '.')
                        continue;
                    sx = x;
                    sy = y;
                    A[sy][sx] = '@';
                    if (search(sx,sy,d,1))
                    {
                        cout<<"YES"<<endl;
                        return;
                    }
                    A[sy][sx] = '.';
                }

            }
            cout<<"NO"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new TvZapping2();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0