結果

問題 No.86 TVザッピング(2)
ユーザー imgry22imgry22
提出日時 2014-12-27 18:09:53
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 2,254 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 159,464 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 23:49:22
合計ジャッジ時間 2,527 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;

#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

#define MAX_M 100
#define MAX_N 100

inline bool check(int x, int y);
bool dfs(int px, int py, int hx, int hy, int cnt, int cntRight, int cntLeft);

int n, m;
char a[MAX_N][MAX_M];
int cnt;
bool used[MAX_N][MAX_M];
bool flag;
int sx, sy;

int main()
{
  cin >> n >> m;
  rep(i, n) rep(j, m) cin >> a[i][j];
  rep(i, n) rep(j, m) if(a[i][j] == '.'){
    mp(sx, sy) = mp(i, j);
    cnt += 1;
  }

  used[sx][sy] = true;
  if(!flag && check(sx+1, sy  ))  flag |= dfs(sx, sy, sx+1, sy,   cnt, 0, 0);
  if(!flag && check(sx,   sy+1))  flag |= dfs(sx, sy, sx,   sy+1, cnt, 0, 0);
  if(!flag && check(sx-1, sy  ))  flag |= dfs(sx, sy, sx-1, sy,   cnt, 0, 0);

  puts(flag ? "YES" : "NO");

  return 0;
}

inline bool check(int x, int y)
{
  return 0<=x&&x<n && 0<=y&&y<m;
}

bool dfs(int px, int py, int hx, int hy, int cnt, int cntRight, int cntLeft)
{
  int nx, ny;

  used[hx][hy] = true;
  rep(i, 3){
    if(i == 0){
      nx = hx + hx - px;
      ny = hy + hy - py;
    }
    if(i == 1){
      if(cntLeft+1 > 6) return false;
      nx = hx - hy + py;
      ny = hy + hx - px;
    }
    if(i == 2 && cntRight == 0){
      nx = hx + hy - py;
      ny = hy - hx + px;
      cntRight = 1;
    }

    if(!check(nx, ny) || a[nx][ny] == '#') continue;
    if((mp(nx, ny) == mp(sx, sy)) && (cnt == 2)) return true;
    if(used[nx][ny]) continue;

    if(dfs(hx, hy, nx, ny, cnt-1, cntRight, cntLeft + i%2)) return true;
    if(i == 2) cntRight = 0;
  }

  used[hx][hy] = false;
  return false;
}
0