結果

問題 No.86 TVザッピング(2)
ユーザー nanasilinanasili
提出日時 2014-12-05 01:00:20
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 4,627 bytes
コンパイル時間 969 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 08:48:06
合計ジャッジ時間 2,292 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <vector>
#include <cctype>
#include <deque>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <cstdio>
#include <string>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <functional>

using namespace std;

typedef long long ll;


#define LLMAX LLONG_MAX
#define IMAX INT_MAX


int main() {
  int n, m;
  cin >> n >> m;
  int safeN = 0;
  char field[m][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> field[j][i];
      safeN += (field[j][i] == '.');
    }
  }

  if (safeN == 2 && n*m == 2) {
    cout << "YES" << endl;
    return 0;
  }
  if (n == 1 || m == 1) {
    cout << "NO" << endl;
    return 0;
  }

  bool vis[m][n];
  memset(vis, false, sizeof(vis));
  bool ok = false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (field[j][i] == '.') {
        int dx[] = {1,0,-1,0};
        int dy[] = {0,-1,0,1};
        for (int p = 0; p < 4; p++) {
          int nx = j+dx[p];
          int ny = i+dy[p];
          int cnt = 1;
          if (!(nx >= 0 && ny >= 0 && nx < m && ny < n && field[nx][ny] == '.')) continue;
          memset(vis, false, sizeof(vis));
          nx = j; ny = i;
          vis[j][i] = true;
          for (int k = 0; k < 4; k++) {
            nx += dx[(k+p)%4];
            ny += dy[(k+p)%4];
            while (nx >= 0 && ny >= 0 && nx < m && ny < n && field[nx][ny] == '.' && !vis[nx][ny]) {
              cnt++;
              vis[nx][ny] = true;
              // std::cout << nx << " " << ny << std::endl;
              nx += dx[(k+p)%4];
              ny += dy[(k+p)%4];
              if (!(nx >= 0 && ny >= 0 && nx < m && ny < n && field[nx][ny] == '.' && !vis[nx][ny])) {
                nx -= dx[(k+p)%4];
                ny -= dy[(k+p)%4];
                if (cnt == safeN) {
                  for (int e = 0; e < 4; e++) {
                    nx += dx[e];
                    ny += dy[e];
                    if (nx == i && ny == j) {
                      cout << "YES" << endl;
                      return 0;
                    }
                    nx -= dx[e];
                    ny -= dy[e];
                  }
                }
                break;
              }
            }
          }
          // cout << endl;
        }
        ok = true;
      }
      if (ok) break;
    }
    if (ok) break;
  }

  ok = false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (field[j][i] == '.') {
        int dx[] = {1,0,-1,0};
        int dy[] = {0,-1,0,1};
        memset(vis, false, sizeof(vis));
        for (int p = 0; p < 4; p++) {
          int nx1 = j+dx[p];
          int ny1 = i+dy[p];
          int nx2 = j+dx[(p+1)%4];
          int ny2 = i+dy[(p+1)%4];
          int nx3 = j+dx[p]+dx[(p+1)%4];
          int ny3 = i+dy[p]+dy[(p+1)%4];
          if (!(nx1 >= 0 && ny1 >= 0 && nx1 < m && ny1 < n && field[nx1][ny1] == '.')) continue;
          if (!(nx2 >= 0 && ny2 >= 0 && nx2 < m && ny2 < n && field[nx2][ny2] == '.')) continue;
          if (!(nx3 >= 0 && ny3 >= 0 && nx3 < m && ny3 < n && field[nx3][ny3] == '#')) continue;
          int cnt = 1;
          vis[j][i] = true;
          int nx = i, ny = j; p++;
          for (int k = 0; k < 5; k++) {
            nx += dx[(k+p)%4];
            ny += dy[(k+p)%4];
            while (nx >= 0 && ny >= 0 && nx < m && ny < n && field[nx][ny] == '.' && !vis[nx][ny]) {
              cnt++;
              vis[nx][ny] = true;
              // std::cout << nx << " " << ny << std::endl;
              nx += dx[(k+p)%4];
              ny += dy[(k+p)%4];
              if (!(nx >= 0 && ny >= 0 && nx < m && ny < n && field[nx][ny] == '.' && !vis[nx][ny])) {
                nx -= dx[(k+p)%4];
                ny -= dy[(k+p)%4];
                if (cnt == safeN) {
                  for (int e = 0; e < 4; e++) {
                    nx += dx[e];
                    ny += dy[e];
                    if (nx == i && ny == j) {
                      cout << "YES" << endl;
                      return 0;
                    }
                    nx -= dx[e];
                    ny -= dy[e];
                  }
                }
              }
            }
            // cout << endl;
          }
          ok = true;
          if (ok) break;
        }
      }
      if (ok) break;
    }
    if (ok) break;
  }
  cout << "NO" << endl;
}
0