結果

問題 No.86 TVザッピング(2)
ユーザー krotonkroton
提出日時 2014-12-06 20:32:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,382 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 55,064 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 14:58:56
合計ジャッジ時間 1,669 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstring>
using namespace std;

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

int H, W;
string mat[111];

char at(int y, int x){
    if(y < 0 || y >= H || x < 0 || x >= W)return '#';
    return mat[y][x];
}

bool used[111][111];
bool check(int sy, int sx, int rest, int y, int x, int d){
    if(used[y][x]){
        if(y == sy && x == sx && rest == 0){
            return true;
        } else {
            return false;
        }
    }
    
    used[y][x] = true;
    for(int i=0;i<2;i++){
        int ny = y + dy[d];
        int nx = x + dx[d];
    
        if(at(ny, nx) == '.'){
            return check(sy, sx, rest - 1, ny, nx, d);
        }
        
        d = (d + 1) % 4;
    }
    
    return false;
}

string solve(){
    int dot = 0;
    for(int y=0;y<H;y++)for(int x=0;x<W;x++)if(mat[y][x] == '.')++dot;
    
    int max_dot = H * 2 + W * 2 - 4;
    if(dot > max_dot)return "NO";
    
    for(int sy=0;sy<H;sy++)for(int sx=0;sx<W;sx++)if(mat[sy][sx] == '.'){
        for(int d=0;d<4;d++){
            memset(used, false, sizeof(used));
            
            if(check(sy, sx, dot, sy, sx, d))return "YES";
        }
    }
    
    return "NO";
}

int main(){
    cin >> H >> W;
    for(int i=0;i<H;i++)cin >> mat[i];
    
    cout << solve() << endl;
    return 0;
}
0