Skip to content

Instantly share code, notes, and snippets.

@yurahuna
Created September 22, 2016 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurahuna/edb7a8f8a3b6a5ab07e28bfce95c61d7 to your computer and use it in GitHub Desktop.
Save yurahuna/edb7a8f8a3b6a5ab07e28bfce95c61d7 to your computer and use it in GitHub Desktop.
int H, W;
vector<string> s;
map<vector<string>, int> dp; // 1:ok, -1:ng, 0:unknown
int gx, gy;
// N, E, S, W
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = { 0, 1, 0, -1};
bool inside(int x, int y) {
return 0 <= x && x < H && 0 <= y && y < W;
}
vector<string> play(vector<string> s, int i, int j, int k) {
for ( ; s[i][j] == 'o' && inside(i + dx[k], j + dy[k]); i += dx[k], j += dy[k]) {
swap(s[i][j], s[i + dx[k]][j + dy[k]]);
}
return s;
}
int dfs(vector<string> s) {
if (dp[s] != 0) return dp[s];
if (s[gx][gy] == 'o') {
return dp[s] = 1;
}
int ret = -1;
rep(i, H) {
rep(j, W) {
if (s[i][j] == 'o') {
rep(k, 4) {
auto t = play(s, i, j, k);
if (dp.count(t) == 0 && dfs(t) == 1) {
ret = 1;
}
}
}
}
}
return dp[s] = ret;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> H >> W;
s.resize(H);
rep(i, H) cin >> s[i];
int N = 0;
rep(i, H) {
rep(j, W) {
if (s[i][j] == 'o') {
N++;
}
if (s[i][j] == '@') {
gx = i, gy = j;
s[i][j] = '.';
}
}
}
if (H > 1 && W > 1 && N >= 3) {
cout << "yes" << endl;
return 0;
}
cout << (dfs(s) == 1 ? "yes" : "no") << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment