結果

問題 No.86 TVザッピング(2)
ユーザー codershifthcodershifth
提出日時 2015-08-25 01:56:11
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,882 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 154,624 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-09-25 16:54:01
合計ジャッジ時間 8,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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 sx,sy, cnt;
            sx = sy = -1;
            cnt = 0;
            vector<string> A(N);
            REP(i,N)
            {
                cin>>A[i];
                // 通過可能なボタンの総数を数えておく
                REP(j,M) if (A[i][j] == '.')
                    ++cnt;
                // 押せるボタンの位置どれか一つを取得しておく
                if (sx < 0)
                {
                    REP(j,M) if (A[i][j] == '.')
                    {
                        sx = j;
                        sy = i;
                        break;
                    }
                }
            }

            // sample 3 より最大一回は右に曲がってよいとしたとき、サイクルができればよさそう。
            vector<vector<bool>> vis(N,vector<bool>(M,false));
            const int dx[] = {-1,0,1,0};
            const int dy[] = {0,1,0,-1};

            // O(N*M)
            function<bool(int,int,int,int,int)> rec = [&](int x, int y, int d, int r, int c) {
                for (int dd = -1; dd <= 1; ++dd)
                {
                    // 右にすでに一回まがっていたらもう曲がれない
                    if (dd==1 && r==1)
                        break;
                    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;
                        continue;
                    }
                    if (A[ny][nx]!='.')
                        continue;
                    if (vis[ny][nx])
                        continue;
                    vis[ny][nx] = true;
                    if (rec(nx,ny,nd,(dd==1),c+1))
                        return true;
                    vis[ny][nx] = false;
                }
                return false;
            };
            REP(d, 4)
            {
                vis[sy][sx] = true;
                if (rec(sx,sy,d,0,1))
                {
                    cout<<"YES"<<endl;
                    return;
                }
                vis[sy][sx] = false;
            }
            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