Input
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
Output
YES
NO
Input
2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
Output
YES
NO
题意:给出3个隧道,英文字母代表火车,s是人的起点,需要从起点走到最右侧出隧道(从哪一个隧道出来都可以),规则是,人向右走一步,然后可以选择向上走一步,不走或者向下走一步,同时火车向左走两步,问人可以走出隧道吗。
思路:火车向左走两步可以转化成人向右走两步。用BFS。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const int M=3e3+10;
#define inf 0x3f3f3f
int vis[110][110];
struct node
{
int x,y;
}start,now;
char mapp[110][110];
int n,m;
queue <node> q;
int bfs(int x,int y)
{
while(!q.empty())
q.pop();
int i;
memset(vis,0,sizeof(vis));
start.x=x;
start.y=y;
vis[x][y]=1;
q.push(start);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.y>=m)//出了隧道就停止搜索
return 1;
now.y+=1;//向右走一步
if(isalpha(mapp[now.x][now.y]))//该位置是英文字母即撞到火车该位置就不能走
continue;
for(i=-1;i<=1;i++)//下,不动,上
{
start.x=now.x+i;
start.y=now.y;
if(start.x>=1&&start.x<=3)//在隧道范围内
{
if(isalpha(mapp[start.x][start.y])||isalpha(mapp[start.x][start.y+1])||isalpha(mapp[start.x][start.y+2]))//撞到火车
continue;
if(vis[start.x][start.y+2]==1)//向右两步的位置已经走过就不能走了
continue;
start.y+=2;//向右走两步
vis[start.x][start.y]=1;
if(start.y>=m)//走出隧道
return 1;
q.push(start);
}
}
}
return 0;
}
int main()
{
int T,i,j,ans=0,k,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&k);
for(i=1;i<=3;i++)
scanf(" %s",mapp[i]+1);
for(i=1;i<=3;i++)
{
for(j=1;j<=m;j++)
{
if(mapp[i][j]=='s')
{
c=i;
d=j;
}
}
}
ans=bfs(c,d);
if(ans==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
1531


BFS&spm=1001.2101.3001.11974&articleId=97661190&d=1&t=3&u=55f3c124e9c745aeabc4f39c3f00dd80)

被折叠的 条评论
为什么被折叠?



