CodeForces-585B(BFS)

本文介绍了一个名为“Subway Roller”的游戏,并详细解析了游戏中的移动策略。通过将火车的移动转化为人物的移动,利用BFS算法判断人物是否能够成功到达目标位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:

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

### 关于Codeforces Round 971 (Div. 4)比赛题目及解析 #### A-G1题解概述 对于Codeforces Round 971 (Div. 4),该轮次的比赛涵盖了多个难度级别的编程挑战,旨在测试参赛者的算法思维能力和编码技巧。其中,A至G1的题目设计覆盖了基础数据结构操作、贪心策略应用以及动态规划等多个方面。 #### G2. Yunli’s Subarray Queries (Hard Version) 具体到G2这道难题目,其背景设定在一个由n个节点构成的无向图环境中,此图恰好拥有\( n-1 \)条带权重的边,每一边连接着两个连续编号的顶点,并且初始状态下每个顶点都放置了一个其自身编号相匹配的小球[^1]。问题的核心在于计算一种最优方案下的总成本——即通过调整各小球的位置让它们不再位于原本对应的顶点之上;每一次位置交换都需要支付相应路径上的代价(即边的权重),而目标就是找到能够满足上述条件的同时使总的迁移费用达到最低的方法。 针对此类涉及最短路径求解的问题,通常采用广度优先搜索(BFS)或迪杰斯特拉(Dijkstra's Algorithm)等经典图论算法来进行处理。然而,在本题特殊条件下,则更倾向于利用差分数组配合线段树/树状数组来高效解决区间更新查询的需求,从而快速响应多次修改后的即时状态变化并给出正确解答[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N], b[N]; long long sum[N << 2]; void push_up(int rt){ sum[rt] = min(sum[rt<<1],sum[rt<<1|1]); } // build, update and query functions omitted for brevity... int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,q,x,y,z; cin >> n >> q; // initialization code here... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值