# Yukicoder No.86 TVザッピング(2)

,

http://yukicoder.me/problems/no/86

## solution

1110
2  0
2  055554
2       4
2       4
233333333


ただし上が縮退した結果の下を含む。

111111110
2       0
2       0
233333330


## implementation

#include <iostream>
#include <vector>
#include <set>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using namespace std;
bool solve(int h, int w, vector<string> const & f) {
auto is_on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };
auto pred = [&](int y, int x) { return is_on_field(y, x) and f[y][x] == '.'; };
const int dy[] = { 0, -1, 0, 1 }; // counter-clockwise
const int dx[] = { 1, 0, -1, 0 };
int total = 0; repeat (y,h) repeat (x,w) total += pred(y, x);
repeat (y0,h) repeat (x0,w) if (pred(y0, x0)) {
repeat (d0,4) {
int y = y0;
int x = x0;
set<pair<int, int> > used;
for (int i = 0; i < 6; ) {
if (not used.empty() and y == y0 and x == x0) {
if (used.size() == total) return true;
break;
}
int di = 0;
for (; di < 2; ++ di) {
int ny = y + dy[(d0 + i + di) % 4];
int nx = x + dx[(d0 + i + di) % 4];
if (pred(ny, nx) and not used.count(make_pair(ny, nx))) {
y = ny;
x = nx;
i += di;
used.insert(make_pair(y, x));
break;
}
}
if (di == 2) break;
}
}
}
return false;
}
int main() {
int h, w; cin >> h >> w;
vector<string> f(h); repeat (y,h) cin >> f[y];
cout << (solve(h, w, f) ? "YES" : "NO") << endl;
return 0;
}