Table of Contents
Chaotic subway problem
[Problem description]
[Input form]
[Output format]
[Sample input]
【Sample output】
Programming
Summary
Home Backend Development Python Tutorial How to use breadth-first search in Python to traverse the chaotic subway

How to use breadth-first search in Python to traverse the chaotic subway

Apr 28, 2023 am 08:01 AM
python

    Chaotic subway problem

    [Problem description]

    The subway network in a certain city is extremely chaotic. There are n subway stations on a subway line, numbered 1 to n. There are subway stops at every station on the subway line. There is a number m on each subway station, which represents the number of stops that passengers must take from this station. Every metro station has metro lines going in both directions. Therefore, you can advance m stations in the direction with a larger number, or you can advance m stations in the direction with a smaller number. However, if you go beyond the scope of a subway station, you cannot ride the subway. For example, the number on the subway numbered 1 is 3. If you get on the train at that subway station, you can sit in the forward direction to the No. 4 subway station. But you cannot take the bus in the opposite direction to the -2 subway station, because the -2 subway station does not exist. Now a passenger starts from subway station A and wants to reach subway station B. Can he reach it? How many subway rides do he need at least?

    [Input form]

    • Enter the number of subway stations in the first line

    • The second line in sequence Enter the number of each subway station, separated by spaces

    • In the third line, enter subway stations A and B, separated by spaces

    [Output format]

    Minimum number of subway rides from subway station A to B

    [Sample input]

    5

    2 4 1 2 3

    1 2

    【Sample output】

    2

    Programming

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    n=int(input())

    lst1=[int(i) for i in range(n)]

    lst2=list(map(int,input().split()))

    start,end=map(int,input().split())

    def BFS(lst1,lst2,start,end):      #广度优先搜索遍历

        count=0          #计算达到终点所需乘坐地铁的次数

        visited=[0 for i in range(n)]    #设置标志列表

        Queue=[]         #设置队列,用于广度优先搜索遍历

        Queue.append(start-1)   #将起点放入队列

        flag=1           #用于改变方向

        while Queue:    #开始循环遍历

            t=Queue.pop(0)   #出队

            for i in range(2):    #分别向左右两个方向走

                flag=-1*flag    #改变方向      

                new=lst1[t]+flag*lst2[t]    #到达的新的地铁站的下标

                if new<0 or new>=n:      #检查是否合法

                    continue

                if new>=0 or new<n:

                    Queue.append(new)     #若合法,就放入队列中

                    visited[new]=1        #标记一下

                    count+=1              #乘坐的地铁次数

                    if visited[end-1]==1:   #如果终点被标记了,说明已经到终点了

                        return count

        return 0

    print(BFS(lst1,lst2,start,end))

    Copy after login

    Summary

    Breadth-first search traversal is mainly implemented through a queue. The specific format is:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    Queen.append()

     

    while Queen:

     

        t=Queen.pop()

     

        if ……

     

            Queen.append()

    Copy after login

    First put the first element into the queue, and then put the first element Take it out and find all the legal elements and put them into the queue, and then take them out from the queue one by one until the queue is empty, which means that all legal elements have been traversed.

    The above is the detailed content of How to use breadth-first search in Python to traverse the chaotic subway. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot Article Tags

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

    What are the advantages and disadvantages of templating?

    How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

    How to download deepseek Xiaomi

    Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

    Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

    For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

    For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

    Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

    Share several .NET open source AI and LLM related project frameworks

    A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

    A complete guide to golang function debugging and analysis

    How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

    How do you ask him deepseek

    How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

    How to save the evaluate function

    See all articles