結果

問題 No.86 TVザッピング(2)
ユーザー mayoko_mayoko_
提出日時 2015-07-23 17:53:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,421 ms / 5,000 ms
コード長 2,658 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 78,804 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 15:01:29
合計ジャッジ時間 3,349 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef complex<double> C;

const int MAXN = 104;
string field[MAXN];
bool used[MAXN][MAXN];
int N, M;
int lasty, lastx, lastdir;

bool outOfRange(int y, int x) {
    return (y < 0 || y >= N || x < 0 || x >= M);
}

void dfs(int y, int x, int dir, int flag) {
    lasty = y; lastx = x; lastdir = dir;
    used[y][x] = true;
    int ny = y+dy[dir];
    int nx = x+dx[dir];
    if (outOfRange(ny, nx) || field[ny][nx] == '#' || used[ny][nx]) {
        dir += (flag == 0) ? 1 : 3;
        dir %= 4;
        ny = y+dy[dir];
        nx = x+dx[dir];
        if (outOfRange(ny, nx) || field[ny][nx] == '#') return;
        if (used[ny][nx]) return;
        dfs(ny, nx, dir, flag);
        return;
    }
    dfs(ny, nx, dir, flag);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for (int i = 0; i < N; i++) cin >> field[i];
    for (int y = 0; y < N; y++) for (int x = 0; x < M; x++) {
        if (field[y][x] == '#') continue;
        for (int k = 0; k < 4; k++) {
            for (int flag = 0; flag < 2; flag++) {
                memset(used, false, sizeof(used));
                dfs(y, x, k, flag);
                bool ng = false;
                for (int i = 0; i < N; i++) {
                    if (ng) break;
                    for (int j = 0; j < M; j++) {
                        if (field[i][j] != '#' && !used[i][j]) {
                            ng = true;
                            break;
                        }
                    }
                }
                if (!ng) {
                    if (lasty+dy[lastdir] == y && lastx+dx[lastdir] == x) {
                        cout << "YES" << endl;
                        return 0;
                    }
                    int dirdir = lastdir;
                    dirdir += (flag == 0) ? 1 : 3;
                    dirdir %= 4;
                    if (lasty+dy[dirdir] == y && lastx+dx[dirdir] == x) {
                        cout << "YES" << endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout << "NO" << endl;
    return 0;
}
0