Chaos Metro (Python)

Python - breadth-first search traversal - chaotic subway problem

chaotic subway problem

problem introduction

【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 respectively. Every station on the subway line will stop at the subway, and there is a number m on each subway station, which represents the number of stations that passengers must take from this station. Every subway station has subways going in both directions. Therefore, you can advance m stations in the direction with a large number, or you can advance m stations in a direction with a small number. But if it goes beyond the scope of the subway station after advancing, the subway cannot be taken. For example, the number on the subway numbered 1 is 3, so if you get on the train at this subway station, you can sit in the positive direction to the No. 4 subway station. But you can't take the car in the opposite direction to the -2 subway station, because the -2 subway station does not exist. Now the passenger departs from No. A subway station and wants to reach No. B subway station. If he can get there, how many times should he take the subway at least?

【Input form】

Enter the number of subway stations in the first line

In the second line, enter the numbers of each subway station in turn, separated by spaces

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

【Output form】

The minimum number of subway rides required from subway station A to B

【Sample input】

5

2 4 1 2 3

1 2 

【Sample output】

2

programming 

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): #breadth-first search traversal
    count=0

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/128706562