[PYTHON] [At Coder] Solve a typical problem of depth-first search (DFS)

I want to be a competitive professional, so [This site (AtCoder version! Ant book (beginner))](https://qiita.com/drken/items/e77685614f3c6bf86f44#%E4%BE%8B%E9%A1%8C-2 -5-5warshall-floyd-% E3% 82% 92% E4% BD% BF% E3% 81% 86% E5% 95% 8F% E9% A1% 8C) to solve the AtCoder problem and solve the algorithm The foundation is solidified. This time, we will solve AtCoder's A-depth-first search. This is a typical problem of depth-first search (DFS), which is a type of full search.

problem

In a rectangular town in the north, south, east and west, the question is whether you can reach the fishmonger from your house through the road without passing through a fence on the way.

solution

This time, we will solve it by stacking without using recursion. [Wikipedia](https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4 % A2) Depth-first search page gives an overview of the algorithm.

As a point to solve,

■ DFS General Part

--Create a stack and put in unvisited nodes --Mark the visited node as visited

■ Part specific to this problem

――I don't know the start position, so check it first (= put it on the stack) --There are four types of nodes connected to the parent node (x, y): (x-1, y), (x + 1, y), (x, y-1), (x, y + 1).

What a place, such as. Below is the python code.

from collections import deque
 
H,W = map(int, input().split())
C = [input() for _ in range(H)]
 
#Check the position of s in advance
for n1,c_w in enumerate(C):
    for n2,c in enumerate(c_w):
        if c == "s":
            s_h = n1
            s_w = n2
            
stack = deque([[s_h, s_w]])  #Create a stack containing only the start position
 
visited_list = [[0]*W for _ in range(H)]
visited_list[s_h][s_w] = 1
 
def dfs(town, visited_list, stack):
    while len(stack) > 0:
        h, w = stack.pop()
        if town[h][w] == "g":
            return "Yes"
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h, new_w = h+j, w+k
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            if town[new_h][new_w] != "#" and visited_list[new_h][new_w] == 0:
                visited_list[new_h][new_w] = 1
                stack.append([new_h, new_w])
    
    return "No"
 
print(dfs(C, visited_list, stack))

Recommended Posts

[At Coder] Solve a typical problem of depth-first search (DFS)
[At Coder] Solve the problem of binary search
[At Coder] Solving a typical BFS problem [A --Darker and Darker]
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Combinatorial optimization --Typical problem-- Partition of a set problem
Solve A ~ D of yuki coder 247 with python
[Python] DFS (Depth-first Search) ATC001A
[Python] DFS (Depth-first Search) ABC157D
Solve the subset sum problem with a full search in Python
Algorithm in Python (depth-first search, dfs)
Write a depth-first search in Python
I tried to solve AtCoder's depth-first search (DFS) in Python (result: TLE ...)
Try to solve a set problem of high school math with Python
Want to solve a simple classification problem?
[AtCoder] Solve ABC1 ~ 100 A problem with Python