zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    源代码的下载和翻译
    Git使用入门
    搭建Andriod开发环境
    Andriod系统移植与驱动开发概述
    直观打印二叉树
    深度优先遍历图(DFS)
    《UNIX网络编程 卷1 套接字联网API》(第三版)阅读笔记----2018.5.22
    C/C++
    实现具有getMin功能的栈
    用两个栈来模拟一个队列
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11369122.html
Copyright © 2011-2022 走看看