Phillip and Trains
原题链接https://vjudge.net/contest/345248#problem/D
将人物的行进路线一步一步判断
PS:
向右一步
上一步或不动或下一步
向右两步
然后由规律用bfs写
注意每走一步都要判断是否到达右端
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
long long mv[3][2]= {1,0,0,0,-1,0};//三个方向 上 不动 下
long long map[105][105];
char vis[105][105];
long long n,m;
long long sum;
struct node
{
long long x;
long long y;
long long step;
} t1;
long long bfs()
{
queue<node>tp;
long long i;
node o,t;
long long j;
map[t1.x][t1.y]=1;
tp.push(t1);
while(!tp.empty())
{
t=tp.front();
tp.pop();
if(t.y==n-1)
{
return 1;
}
t.y++;//向右一步
if(t.y==n-1)
{
return 1;
}
if(vis[t.x][t.y]>='A'&&vis[t.x][t.y]<='Z')
{
continue;
}
for(i=0; i<3; i++)//三种选择
{
o.x=t.x+mv[i][0];
o.y=t.y+mv[i][1];
if(o.x<0||o.y<0||o.x>2||o.y>n-1||map[o.x][o.y]==1)
{
continue;
}
map[o.x][o.y]=1;
if(vis[o.x][o.y]>='A'&&vis[o.x][o.y]<='Z')
{
continue;
}
o.y++;//向右一步
if(o.y==n-1)
{
return 1;
}
if(vis[o.x][o.y]>='A'&&vis[o.x][o.y]<='Z')
{
continue;
}
o.y++;//向右一步
if(o.y==n-1)
{
return 1;
}
if(vis[o.x][o.y]>='A'&&vis[o.x][o.y]<='Z')
{
continue;
}
tp.push(o);//可以走压入栈
}
}
return -1;
}
int main()
{
long long t;
scanf("%lld",&t);
while(t--)
{
long long i,j,k;
char ss;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
scanf("%lld %lld",&n,&k);
for(i=0; i<3; i++)
{
getchar();
for(j=0; j<n; j++)
{
scanf("%c",&ss);
vis[i][j]=ss;
if(ss=='s')
{
t1.x=i;
t1.y=j;
}
}
}
long long sum=bfs();//用返回值判断是否成立
if(sum==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}