I can't solve the labyrinthine problem in Python.

Asked 2 years ago, Updated 2 years ago, 44 views

Read the body of the problem here: AtCoder Typical Contest 001A - Depth Priority Search

My code is as follows.

import sys
sys.setrecursionlimit(10000)


H,W = map(int, input().split())
maze = [list(map(str, input().split())))) for_in range(H)]

def search(x,y,maze):
    if x<0 or y<0 or maze[x][0][y]=="#":
        return
    elifmaze[x][0][y]=="g":
        print ("Good")
    else:
        search(x+1,y,maze)
        search(x-1,y,maze)
        search(x,y+1,maze)
        search(x,y-1,maze)

# identify the coordinates of "S"
for i in range (H):
    for jin range (W):
        ifmaze[i][0][j]=="s":
            sx = i
            sy=j

search(sx,sy,maze)

When you run it, you get the following error:(Example Input 1)

RecursionError: maximum recovery depth exceeded in comparison

python python3 algorithm

2022-09-30 20:21

1 Answers

To understand the meaning of the algorithm, maze[x][0][y] is a mistake in maze[y][x] or something like print(maze) or print(maze[2][3]).

However, you should still see errors in other examples after making this correction.Try to check if the conditions for function search stop recursive calls are set correctly.Specifically, search recursive calls stop roughly when one of the following conditions is met, but consider if there are any omissions.

  • x is less than 0 (with the left protruding)
  • y is <0 (top protruding)
  • When the mass you are looking at is "#" (when you hit the wall)
  • When the mass you are looking at is "g" (when you reach the goal)

If you can't see it after organizing the conditions, write down how the program will run by hand.An example is a test of understanding.


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.