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)最大
随机推荐
- Python实现打印螺旋矩阵功能的方法
Python实现打印螺旋矩阵功能的方法 本文实例讲述了Python实现打印螺旋矩阵功能的方法.分享给大家供大家参考,具体如下: 一.问题描述 输入N, 打印 N*N 螺旋矩阵 比如 N = 3,打印: ...
- pycryptodom的源码安装
1.去网站https://pypi.python.org/pypi/pycryptodome/#downloads下载 2.python setup.py build -> python set ...
- CSS元素隐藏
{ display: none; /* 不占据空间,无法点击 */ } /*************************************************************** ...
- Java应用监控利器JMX
啥是 JMX? The Java Management Extensions (JMX) API is a standard API for management and monitoring of ...
- 【6.12校内test】T1单词序列
[问题描述] 给出两个单词(开始单词和结束单词)以及一个词典.找出从开始单词转换到结束单词, 所需要的最短转换序列.转换的规则如下: 1.每次只能改变一个字母 2.转换过程中出现的单词(除开始单词和结 ...
- ZOJ 2836 Number Puzzle 题解
题面 lcm(x,y)=xy/gcd(x,y) lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn) #include <bits/stdc++.h> ...
- 匿名函数lambda和map函数
一.map函数,实现迭代操作 map(f1,x) f1为函数的名称(不加括号),x为map的参数,示例如下: def add(a): return a+10 print map(add,[1,2,3] ...
- C# System.Reflection (反射)
在使用.NET创建的程序或组件时,元数据(metadata)和代码(code)都存储于“自成一体”的单元中,这个单元称为装配件.我们可以在程序运行期间访问这些信息. 在System.Reflectio ...
- 简单了解 node net 模块
简单了解 node net 模块 文章记录了对net 模块的简单理解分析. net模块 简单使用 net.Server 类 net.Socket 类 总结 1.1 net模块 Node.js 的 Ne ...
- vue-注册全局过滤器
import Vue from 'vue'; import dayjs from 'dayjs'; const filters = { formatDate(date, format = 'YYYY- ...