题目链接
这道题其实不难,但却给予了我需要总结的东西,因为当你做bfs的时候,我看这道题是往一个方向的所以就没考虑是否遍历过这个问题,但是,确实会重复经过同一个节点的。。。QAQ.....
题目是一个换参考系的BFS裸题,就是告诉你有个人,躲火车,每次往前走一步之后可以向上、向下、原地不动,这么三种状态,那么火车既然要相向开过来,不如就相当于人横向走的是三步即可,然后来过的点就不要再走了!!!中间还有件事,就是你得判断路上是否有火车,还有就是成功逃亡的时候,可能会超出边界,所以一开始多开几个点。
完整代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const int maxN=115;
int N, K, sx, sy; char mp[4][maxN]; bool vis[4][maxN];
struct node
{
int x, y;
node(int a=0, int b=0):x(a), y(b) {}
}u, v;
queue<node> Q;
bool bfs(int x, int y)
{
while(!Q.empty()) Q.pop();
Q.push(node(x, y));
vis[x][y]=true;
while(!Q.empty())
{
u=Q.front(); Q.pop();
if(u.y>=N) return true;
u.y++;
if(mp[u.x][u.y]=='.')
{
for(int i=-1; i<=1; i++)
{
v=node(u.x+i, u.y);
if(v.x<1 || v.x>3) continue;
if(mp[v.x][v.y]=='.' && mp[v.x][v.y+1]=='.' && mp[v.x][v.y+2]=='.' && !vis[v.x][v.y+2])
{
v.y+=2;
vis[v.x][v.y]=true;
Q.push(v);
}
}
}
}
return false;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &K); memset(vis, false, sizeof(vis));
for(int i=1; i<=3; i++)
{
scanf("%s", mp[i]+1);
if(mp[i][1]=='s') { sx=i; sy=1; mp[i][1]='.'; }
mp[i][N+1]=mp[i][N+2]=mp[i][N+3]='.';
}
if(bfs(sx, sy)) printf("YES\n");
else printf("NO\n");
}
return 0;
}