CodeForces-585B(BFS)
链接:
https://vjudge.net/problem/CodeForces-585B
题意:
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.
思路:
最开始还想搞一下每个时刻的地图,看了题解瞬间懂了,把火车往左开转化成人往右走,判断一下就行.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
struct Node
{
int x, y;
int step;
};
char Map[5][MAXN];
int Vis[5][MAXN];
int n, k, row;
bool Check(Node x)
{
if (x.step <= 0)
return true;
int pos = x.y;
pos += (x.step-1)*2;
if (pos > n)
return true;
for (int i = pos+1;i <= min(pos+2, n);i++)
if (Map[x.x][i] != '.')
return false;
return true;
}
bool Check2(Node x)
{
int pos = x.y;
pos += (x.step-1)*2;
if (pos > n)
return true;
if (Map[x.x][pos] != '.')
return false;
return true;
}
bool Bfs()
{
int x = row, y = 1;
queue<Node> que;
que.push(Node{x, y, 0});
while (!que.empty())
{
Node now = que.front();
que.pop();
if (!Check(now))
{
// cout << "n" << endl;
continue;
}
// cout << now.x << ' ' << now.y << ' ' << now.step << endl;
if (now.y == n)
return true;
if (!Check2(Node{now.x, now.y+1, now.step+1}))
continue;
if (Vis[now.x][now.y+1] == 0 && Check2(Node{now.x, now.y+1, now.step+1}))
que.push(Node{now.x, now.y+1, now.step+1}), Vis[now.x][now.y+1] = 1;
if (now.x-1 >= 1 && Vis[now.x-1][now.y+1] == 0 && Check2(Node{now.x-1, now.y+1, now.step+1}))
que.push(Node{now.x - 1, now.y + 1, now.step + 1}), Vis[now.x - 1][now.y + 1] ++;
if (now.x+1 <= 3 && Vis[now.x+1][now.y+1] == 0 && Check2(Node{now.x+1, now.y+1, now.step+1}))
que.push(Node{now.x + 1, now.y + 1, now.step + 1}), Vis[now.x + 1][now.y + 1] ++;
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
memset(Vis, 0, sizeof(Vis));
cin >> n >> k;
for (int i = 1;i <= 3;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> Map[i][j];
if (Map[i][j] == 's')
row = i;
}
}
if (Bfs())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
CodeForces-585B(BFS)的更多相关文章
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
- Three Pieces CodeForces - 1065D (BFS)
链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...
- Fair CodeForces - 987D (bfs)
链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- codeforces 60B bfs
题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满. 思 ...
- Board Game CodeForces - 605D (BFS)
大意: 给定$n$张卡$(a_i,b_i,c_i,d_i)$, 初始坐标$(0,0)$. 假设当前在$(x,y)$, 若$x\ge a_i,y\ge b_i$, 则可以使用第$i$张卡, 使用后达到坐 ...
- Codeforces 1105D (BFS)
题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
随机推荐
- MySQL 树形结构 根据指定节点 获取其所有叶子节点
背景说明 需求:MySQL树形结构, 根据指定的节点,获取其下属的所有叶子节点. 叶子节点:如果一个节点下不再有子节点,则为叶子节点. 问题分析 1.可以使用类似Java这种面向对象的语言,对节点集合 ...
- C#客户端填充外部IE浏览器中网页文本(input)且不提交
//引用COM组件//Microsoft HTML Object Library//Microsoft Internet Controls 记得改成x86 SHDocVw.ShellWindows ...
- Spring-QUARTZ定时任务demo
Quartz定时任务demo下载: https://github.com/AliceSunCong/quartz 大致流程: **1.pom文件引入QUARTZ依赖** <dependency& ...
- 打开VMware提示该虚拟机似乎正在使用中该怎么办?
一,当出现虚拟机无法使用时 解决办法: 1,找到虚拟机安装路径. 2,然后,将后缀为.lck的文件夹删除 二,VMware虚拟机配置文件(.vmx)损坏修复 1,找到后缀vmx的文件,记事本打开: 2 ...
- Java的HashMap键值对存储结构解析
容器总体结构 Map存储键值对的数据结构是“数组+链表”的结构,结合了数组查询数据快和链表增删数据快的优点:用Entry[]存储键值对,Entry为类类型,类里面有四个属性:hash.K.V.next ...
- java中抽象类、接口及区别
转自:http://www.cnblogs.com/dolphin0520/p/3811437.html 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法:它只有声明,而没 ...
- Android 子线程无法刷新UI界面
问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...
- POJ - 1251 Jungle Roads (最小生成树&并查集
#include<iostream> #include<algorithm> using namespace std; ,tot=; const int N = 1e5; ]; ...
- POJ 3585 Accumulation Degree 题解
题面 一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量 这道题是换根法+二次扫描的模板: 首先若确定1为原点,那么可以写出dp方程:当v的度是1时, g[u]+= ...
- Kali安装在U盘+使用aircrack-ng套件
因为: Kali Linux 自带aircrack-ng 虚拟机VMware不能用笔记本内置网卡,需要另外买一个无线网卡,然而并不想买 不想给笔记本重装Kali Linux系统 有闲置的32GU盘 所 ...