Codeforces Round #325 (Div. 2) D bfs
1 second
256 megabytes
standard input
standard output
The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.
All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.
Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column.
Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≤ t ≤ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) — the number of sets.
Then follows the description of t sets of the input data.
The first line of the description of each set contains two integers n, k (2 ≤ n ≤ 100, 1 ≤ k ≤ 26) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains.
For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.
2
16 4
...AAAAA........
s.BBB......CCCCC
........DDDDD...
16 4
...AAAAA........
s.BBB....CCCCC..
.......DDDDD....
YES
NO
2
10 4
s.ZZ......
.....AAABB
.YYYYYY...
10 4
s.ZZ......
....AAAABB
.YYYYYY...
YES
NO
In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.
Note that in this problem the challenges are restricted to tests that contain only one testset.
题意 :给你一个3*n的矩阵 不同的字母代表一列火车 如图很明显 '.'代表可以通过的路 ‘s’为人的初始点 人向右走每秒1格(改变方向不记入格数) 之后火车向左开两格 判断人是否能安全的通过边界
题解:bfs 注意边界处理
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t;
char mp[][];
int dis[][]={{-,},{,},{,}};
int n,k;
struct node
{
int xx,yy;
};
int biao[][];
queue<node> q;
void bfs(int s,int e)
{
node exm,now;
while(!q.empty())
q.pop();
exm.xx=s;
exm.yy=e;
biao[s][e]=;
q.push(exm);
int flag=;
while(!q.empty())
{
exm=q.front();
q.pop();
if(exm.yy>n-)
{
flag=;
break;
}
for(int i=;i<;i++)
{
int xxx=exm.xx+dis[i][];
int yyy=exm.yy+dis[i][];
if(xxx>=&&xxx<&&yyy>=&&yyy<n+&&mp[xxx][yyy]=='.')
{
if(mp[exm.xx][exm.yy+]=='.'&&mp[xxx][exm.yy+]=='.'&&mp[xxx][exm.yy+]=='.')//判断通过的路径上都为‘.’
{
if(biao[xxx][yyy]==){
now.xx=xxx;
now.yy=yyy;
q.push(now);
biao[xxx][yyy]=;
}
}
}
}
}
if(flag==)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
scanf("%d",&t);
for(int i=;i<=t;i++)
{
memset(mp,,sizeof(mp));
memset(biao,,sizeof(biao));
scanf("%d %d",&n,&k);
for(int j=;j<;j++)
scanf("%s",mp[j]);
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
mp[][n]='.';mp[][n+]='.';mp[][n+]='.';
int sx,sy;
for(int j=;j<;j++)
{
if(mp[j][]=='s'){
sx=j;
sy=;
}
}
bfs(sx,sy);
}
return ;
}
Codeforces Round #325 (Div. 2) D bfs的更多相关文章
- Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS
D. Phillip and Trains Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/ ...
- Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #325 (Div. 2) C. Gennady the Dentist 暴力
C. Gennady the Dentist Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586 ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 水题
A. Alena's Schedule Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/pr ...
- Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀和
B. Laurenty and Shop Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/p ...
- Codeforces Round #325 (Div. 2) Phillip and Trains dp
原题连接:http://codeforces.com/contest/586/problem/D 题意: 就大家都玩过地铁奔跑这个游戏(我没玩过),然后给你个当前的地铁的状况,让你判断人是否能够出去. ...
- Codeforces Round #325 (Div. 2) Laurenty and Shop 模拟
原题链接:http://codeforces.com/contest/586/problem/B 题意: 大概就是给你一个两行的路,让你寻找一个来回的最短路,并且不能走重复的路. 题解: 就枚举上下选 ...
- Codeforces Round #325 (Div. 2) Alena's Schedule 模拟
原题链接:http://codeforces.com/contest/586/problem/A 题意: 大概就是给你个序列..瞎比让你统计统计什么长度 题解: 就瞎比搞搞就好 代码: #includ ...
- Codeforces Round #361 (Div. 2) B bfs处理
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- Adapter 启动时报错
如果把Adapter安装到C盘,有时在启动Adapter的时候会抛出java.io.IOException: Cannot run program "C:\tibco\adapter\adr ...
- Windows下QT Creator工程中添加文件夹
在QT项目,常常会有很多头文件和源文件,但是QT Creator中却没有添加文件夹的功能,造成项目代码混乱. 下面是建立文件的步骤: 1.打开工程目录,在目录下建立文件夹,如建立文件SerialP ...
- AngularJS的学习笔记(一)
声明:单纯作为我自己的学习笔记,纯是为了自己学习,上面的话都是从各处粘贴,如有冒犯,请原谅我这个小菜鸟~ AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷. 使用双大括 ...
- python导入cx_Oracle报错的问题!
import cx_Oracle 总是报错:ImportError: DLL load failed: 找不到指定的模块. 或者:ImportError: DLL load failed: %1 不是 ...
- nginx(一)
crul新浪微博的时候发现对面用的是nginx服务器,在虎扑足球(挺好的足球论坛)讨论世界杯也发现他们也用这nginx,联想到阿里的tengine也是基于nginx的,觉得有了解一下nginx的必要了 ...
- Android ShapeDrawable
今天做项目碰到一个这样的情况,就是颜色指示框,用的是正方形边框是黑色的,里面填充颜色,颜色值是动态的,为了解决这个问题,查了好多资料,终于找到解决的方法,利用ShapeDrawable,我们自定义一个 ...
- Bootstrap 模态框在用户点击背景空白处时会自动关闭
问题: Bootstrap 模态框在用户点击背景空白处时,会自动关闭. 解决方法: 在HTML页面中编写模态框时,在div初始化时添加属性 aria-hidden=”true” data-backdr ...
- java基础之 工具类
一.StringUtils StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true String ...
- linux 两个文件合并
可以使用cat命令,有两种实现的方式,一种将两个文件合并的到一个新的文件,另一种将一个文件追加到另一个文件的末尾. 方法一:使用cat命令从文件中读入两个文件,然后将重定向到一个新的文件.这种方法可以 ...
- 安装数据库Mocrosoft.NET Application Security警告
在安装sqlserver 2012的时候,出现了Mocrosoft.NET Application Security警告,这个时候可以检查是否联网,如果没有联网请连接上,然后重新检查就不再警告了.如果 ...