【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input
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.
Output
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.
Examples
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
Note
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.
【题目链接】:http://codeforces.com/contest/586/problem/D
【题解】
bfs题;
首先从每辆车开始处理出每个点在某时刻会不会被车占据;
即can[x][y][t];
其中x最大为3,y最大为100,t最大也为100;
空间没问题;
然后模拟一下每辆车从右到左行驶的情况就可以了(不要忘记一开始的0时刻这个位置也被占据了);
然后从起点开始bfs;
要往右走的时候先看看右边那个位置有没有被车占据;
如果被车占据了;则什么都不能做;表示这个状态是不可行的(当然只是说这个状态不可行,可能还有其他状态在扩展,所以不能直接输出无解);
如果没有被车占据;那么就尝试往右走;时间+1,然后看看在这个时间下会不会被车撞;如果不会被车撞则把这个状态加入到队尾,并标记这个状态被占据(不加这个剪枝会各种T和WA);
如果右边那个位置没有被占据;则可以先往右再往上;到了上面再把时间+1;看看在这个位置会不会被撞.不行的话…..
右下同理;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
struct abc
{
int x,y,t;
};
int t,n;
bool can[4][110][110];
queue <abc> dl;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
bool bfs(int x,int y)
{
can[x][y][0] = true;
while (!dl.empty()) dl.pop();
abc temp;
temp.x = x,temp.y = y,temp.t = 0;
dl.push(temp);
while (!dl.empty())
{
int q1 = dl.front().x,q2 = dl.front().y,t = dl.front().t+1;
dl.pop();
if (q2 == n)
return true;
q2++;
abc temp1;
if (!can[q1][q2][t-1] && !can[q1][q2][t])
{
temp1.x = q1,temp1.y = q2,temp1.t = t;
can[q1][q2][t] = true;
dl.push(temp1);
}
if (!can[q1][q2][t-1] && q1-1>=1 && !can[q1-1][q2][t-1] && !can[q1-1][q2][t])
{
temp1.x = q1-1,temp1.y = q2,temp1.t = t;
can[q1-1][q2][t] = true;
dl.push(temp1);
}
if (!can[q1][q2][t-1] && q1+1<=3 && !can[q1+1][q2][t-1] && !can[q1+1][q2][t])
{
temp1.x = q1+1,temp1.y = q2,temp1.t = t;
can[q1+1][q2][t] = true;
dl.push(temp1);
}
}
return false;
}
int main()
{
rei(t);
while (t--)
{
memset(can,false,sizeof(can));
int sx,sy,m;
rei(n);rei(m);
string s1;
for (int i = 1;i <= 3;i++)
{
cin>>s1;
for (int j = 1;j <= n;j++)
{
if (s1[j-1]=='s')
sx = i,sy = j;
else
if (isalpha(s1[j-1]))
{
int l = j-1,r = j-1;
while (r+1<=n-1 && isalpha(s1[r+1]))
r++;
l++;r++;
int tempr = r;
int t = 0;
for (int k = l;k <= r;k++)
can[i][k][0] = true;
while (r>1)
{
t++;
l--;if (l <1) l = 1;
r--;
l--;if (l <1) l = 1;
r--;if (r <1) break;
for (int k = l;k <= r;k++)
can[i][k][t] = true;
can[i][l][t] = true;
}
j = tempr;
}
}
}
if (bfs(sx,sy))
puts("YES");
else
puts("NO");
}
return 0;
}
【33.33%】【codeforces 586D】Phillip and Trains的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【58.33%】【codeforces 747B】Mammoth's Genome Decoding
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)
[UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...
- JAVA 基础编程练习题33 【程序 33 杨辉三角】
33 [程序 33 杨辉三角] 题目:打印出杨辉三角形(要求打印出 10 行如下图) 程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 package ...
随机推荐
- synchronized和AtomicInteger解决并发问题的性能比较
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而volatile ...
- 【Codeforces Round #434 (Div. 1) B】Polycarp's phone book
[链接]h在这里写链接 [题意] 给你n个电话号码. 让你给每一个电话号码选定一个字符串s; 使得这个串s是这个电话号码的子串. 且不是任何一个其他电话号码的子串. s要求最短. [题解] 字典树. ...
- 写PPT的先扬后抑的思路
近期给一个客户做IT战略规划. 基本结束了,客户要求写点有高度的东西.我想也是,尽管眼下的PPT也触及到战略和行业的问题,可是没有总结出来.于是就挖空心思,琢磨了三天.写了4页PPT.改动了几遍.还算 ...
- 获取iOS顶部状态栏和Navigation的高度
状态栏的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navig ...
- js进阶 13-2 jquery动画滑动效果哪些注意事项
js进阶 13-2 jquery动画滑动效果哪些注意事项 一.总结 一句话总结:滑动里面这里up是隐藏,down是显示. 1.jquery动画默认的两个切换效果是什么(swing默认和linear的区 ...
- mootools常用特性和示例(基础篇2)
接着上一篇:mootools常用特性和示例(基础篇1) 1.表单操作 html: <form id="myForm" action="submit.php" ...
- 重写ajax方法实现异步请求session过期时跳转登录页面(转)
一般我们会在过滤器里判断登录状态,如果没登录就跳转登录页面,过滤器java核心代码如下: UserItem loginUser = (UserItem)request.getSession().get ...
- 【MongoDB】在Mongodb使用shell实现与javascript的动态交互
关于利用mongodb的shell运行脚本,这点在曾经的文章中有点遗漏:如今在此篇博客中做个补充: 一.在命令行中传入脚本文件 定义一个javasciprt文件,名称为:script1.js,内容例如 ...
- SetForegroundWindow的正确用法
在SetForegroundWindow之前比较早的时候(比如main函数里)调用一下以下代码: DWORD dwTimeout = -1; SystemParametersInfo(SPI_GETF ...
- sqlplus中怎么将你全部的操作和结果记录保存到你指定的文件里
[在sqlplus的操作中,非常多时候我们都想把自己的写的sql语句和改动日志或者结果信息做记录] [首先]肯定要正常连接到oralce数据库. [然后] 你用你指定的用户登录到oralce数据库之后 ...