結果

問題 No.86 TVザッピング(2)
ユーザー tnakao0123tnakao0123
提出日時 2016-03-10 23:25:11
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 839 ms / 5,000 ms
コード長 1,676 bytes
コンパイル時間 821 ms
コンパイル使用メモリ 84,528 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-25 12:50:58
合計ジャッジ時間 2,953 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 86.cc: No.86 TVザッピング(2) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_M = 100;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

/* global variables */

int n, m, cn;
string as[MAX_N];
bool used[MAX_N][MAX_M];

/* subroutines */

bool check(int sx, int sy, int di) {
  memset(used, false, sizeof(used));

  int x = sx, y = sy, c = 0;

  for (;;) {
    used[y][x] = true;
    c++;

    int d;
    for (d = 0; d < 2; d++) {
      int di0 = (di + d) & 3;
      int x0 = x + dxs[di0], y0 = y + dys[di0];
      if (x0 >= 0 && x0 < m && y0 >= 0 && y0 < n && as[y0][x0] == '.') {
	if (x0 == sx && y0 == sy) {
	  //printf("  c=%d, cn=%d\n", c, cn);
	  return (c == cn);
	}
	if (! used[y0][x0]) {
	  x = x0, y = y0, di = di0;
	  break;
	}
      }
    }
    if (d >= 2) return false;
  }
  return false;
}

/* main */

int main() {
  cin >> n >> m;
  for (int y = 0; y < n; y++) {
    cin >> as[y];
    for (int x = 0; x < m; x++)
      if (as[y][x] == '.') cn++;
  }

  for (int sy = 0; sy < n; sy++)
    for (int sx = 0; sx < m; sx++)
      for (int di = 0; di < 4; di++)
	if (as[sy][sx] == '.') {
	  bool ans = check(sx, sy, di);
	  //printf("%d,%d,%d = %d\n", sx, sy, di, ans);
	  if (ans) {
	    puts("YES");
	    return 0;
	  }
	}

  puts("NO");
  return 0;
}
0