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;
}

转载于:https://www.cnblogs.com/YDDDD/p/11369122.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值