[Python] BFS (breadth-first search) ABC007C

ABC1007C There is a very easy-to-understand explanation in the problem statement. reference BFS (Breadth-first Search) Super Introduction! ~ Use the queue vividly ~

Sample code


from collections import deque

# (sx, sy)From(gx, gy)Find the shortest distance to
#If you can't reach it, inf (not expected to be output)
def bfs():
    #Initialize all point distances with inf
    d = [[float("inf")] * m for i in range(n)]

    #Vector in 4 directions of movement
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    for i in range(n):
        for j in range(m):
            #Set start and goal in map information
            if maze[i][j] == "S":
                sx = i
                sy = j
            if maze[i][j] == "G":
                gx = i
                gy = j

    #Queue the starting point and set the distance to that point to 0
    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    #Loop until the BFS body queue is empty
    while que:
        #Extract the beginning of the queue
        p = que.popleft()
        #If the state you took out is the goal, stop searching
        if p[0] == gx and p[1] == gy:
            break
        #Loop in 4 directions
        for i in range(4):
            #The point after moving(nx, ny)To
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            #Judgment whether it is possible to move and whether it has already been visited
            # d[nx][ny] !=I have visited inf
            if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):
                #Queue if you can move, and set the distance of that point to the distance from p+Confirm with 1
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]


n, m = map(int, input().split())
#Read as a list of lists
maze = [list(input()) for i in range(n)]

print(bfs())

Recommended Posts

[Python] BFS (breadth-first search) ABC007C
[Python] BFS (breadth-first search) ABC168D
Algorithm in Python (breadth-first search, bfs)
Python Exercise 1-Breadth-first search
[Python] Search (itertools) ABC167C
[Python] Search (NumPy) ABC165C
Breadth-first search / bidirectional search (Python version)
[Python] Depth-first search and breadth-first search
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python
Breadth-first search (BPF) Maybe understood (python)
[Python] 01-BFS ARC005C
ABC146C (binary search)
Sequential search with Python
Solve ABC146-C in Python
Binary search in Python
Binary search (python2.7) memo
[Python] Binary search ABC155D
Solve ABC098-C in Python
python bit full search
Linear search in Python
Binary search with python
Binary search with Python3
Search Twitter using Python
Binary search in Python (binary search)
I implemented breadth-first search in python (queue, drawing self-made)
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Search for strings in Python
Search algorithm using word2vec [python]
[Python] DFS (Depth-first Search) ATC001A
Algorithm in Python (binary search)
Full bit search with Python
[Python] DFS (Depth-first Search) ABC157D
Find the diameter of the graph by breadth-first search (Python memory)
Search engine work with python
Search twitter tweets with python
Streamline web search with python
Write a binary search in Python
Learn search with Python # 2bit search, permutation search
Algorithm in Python (depth-first search, dfs)
Master linear search! ~ Python implementation version ~
[At Coder] ABC085C --Otoshidama's Python answer
# 2 Python beginners challenge AtCoder! ABC085C --Otoshidama
Reproduce One-Touch Search on Python 3.7.3. (Windows 10)
Depth-first search using stack in Python
Python 2-minute search and its derivation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search