http://codeforces.com/problemset/problem/586/D
题意:有一个3*n(n<100)的隧道。一个人在最左边,要走到最右边,每次他先向右移动一格,再上下移动一格。隧道里有车,每次人移动以后,车会向右移动两格,人与车轮流运动。
题解,将每次车的移动变成人往右移动两格,然后就能搜索了。dfs,用vis数组判掉已经移动到的格子(因为已经移动到的格子要么是不能走到底的,要么就可以走完)。注意他只能移动到三的倍数的格子,所以读入map后要再后面加两列'.'
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<algorithm> #include<iostream> #include<string> #include <vector> #include<string.h> using namespace std; typedef long long ll; const int maxn = 100 + 5; //int d[maxn], k[maxn]; string map[4]; int vis[4][maxn]; //1e4 int n, x; int ok; void dfs(int dx, int dy) { if (vis[dx][dy]) return; vis[dx][dy] = 1; for (int i = -1; i <= 1; i++) { int ddx = dx + i; if (dy >= n - 1) { ok = 1; return; } if (map[dx][dy + 1] != '.')return; if (map[dx][dy + 2] == '.'&&map[dx][dy + 3] == '.')dfs(dx, dy + 3); if (ok)return; if (dx - 1 > 0 && map[dx - 1][dy + 1] == '.'&&map[dx - 1][dy + 2] == '.'&&map[dx - 1][dy + 3] == '.') dfs(dx - 1, dy + 3); if (ok)return; if (dx + 1 <4 && map[dx+ 1][dy + 1] == '.'&&map[dx+ 1][dy + 2] == '.'&&map[dx+ 1][dy + 3] == '.') dfs(dx+ 1, dy + 3); if (ok)return; } } int main() { int t; cin >> t; while (t--) { memset(vis, 0, sizeof(vis)); cin >> n >> x; int s = 0; for (int i = 1; i <= 3; i++) { cin >> map[i]; if (map[i][0] == 's')s = i; } ok = 0; if (n % 3 != 1)for (int i = 1; i <= 3; i++) { if ((n % 3) == 0)map[i] = map[i] + '.'; if ((n % 3) == 2)map[i] = map[i] + ".."; } dfs(s, 0); if (ok == 1)cout << "YES" << endl; else cout << "NO" << endl; } //cin >> t; }