使用递归函数的算法

问题描述 投票:0回答:1

https://codeup.kr/problem.php?id=6098

嗨,亲爱的! 我征求您的宝贵意见.. 谢谢各位的建议

[规则:问题陈述]

  • 起点:(1,1)
  • 仅移动到0
  • 必须向右(0,1)或向下(1,0)移动 (* 向右移动总是优于向下移动)
  • 移动直到遇到2或无法移动到任何方向
  • 用9标记经过的路径

我期望输出 A,但我的代码打印了错误的输出,B。我不知道为什么.. 详细来说,我期望“中断”在 (6,6) 中起作用,但事实并非如此。经过(7,6)之后,全零最终变成了9。这是错误的结果...换句话说,第三个'elif'不起作用。
请让我知道如何解决..

[A]  
1 1 1 1 1 1 1 1 1 1  
1 9 9 1 0 0 0 0 0 1  
1 0 9 1 1 1 0 0 0 1  
1 0 9 9 9 9 9 1 0 1  
1 0 0 0 0 0 9 1 0 1  
1 0 0 0 0 1 9 1 0 1  
1 0 0 0 0 1 9 1 0 1  
1 0 0 0 0 1 0 0 0 1  
1 0 0 0 0 0 0 0 0 1  
1 1 1 1 1 1 1 1 1 1  

[B]  
1 1 1 1 1 1 1 1 1 1  
1 9 9 1 9 9 9 9 9 1  
1 0 9 1 1 1 9 9 9 1  
1 9 9 9 9 9 9 1 9 1  
1 9 9 9 9 9 9 1 9 1  
1 9 9 9 9 1 9 1 9 1  
1 9 9 9 9 1 9 1 9 1  
1 9 9 9 9 1 9 9 9 1  
1 9 9 9 9 9 9 9 9 1  
1 1 1 1 1 1 1 1 1 1

[Initial grid]  
1 1 1 1 1 1 1 1 1 1  
1 0 0 1 0 0 0 0 0 1  
1 0 0 1 1 1 0 0 0 1  
1 0 0 0 0 0 0 1 0 1  
1 0 0 0 0 0 0 1 0 1  
1 0 0 0 0 1 0 1 0 1  
1 0 0 0 0 1 2 1 0 1  
1 0 0 0 0 1 0 0 0 1  
1 0 0 0 0 0 0 0 0 1  
1 1 1 1 1 1 1 1 1 1 

[output; In fact, It should stop in (6,6)]
1 1 1 1 1 1 1 1 1 1  
1 0 0 1 0 0 0 0 0 1  
1 0 0 1 1 1 0 0 0 1  
1 0 0 0 0 0 0 1 0 1  
1 0 0 0 0 0 0 1 0 1  
1 0 0 0 0 1 0 1 0 1  
1 0 0 0 0 1 2 1 0 1  
1 0 0 0 0 1 0 0 0 1  
1 0 0 0 0 0 0 0 0 1  
1 1 1 1 1 1 1 1 1 1  
1 1  
1 2  
2 2  
3 2  
3 3  
3 4  
3 5  
3 6  
4 6  
5 6  
6 6  
7 6  
7 7  
7 8  
8 8  
8 8  
7 8  
8 7  
7 7  
8 7  
7 8  
8 6  
6 7  
6 8  
7 8  
8 8  
6 8  
7 6 
8 6  
7 7  
8 6  
4 7  
4 8  
5 8  
6 8  
7 8  
8 8  
5 8  
6 8  
7 8  
8 8  
4 8  
5 6  
6 6  
7 6  
8 6  
3 7  
3 8  
4 8  
5 8  
6 8  
7 8  
8 8  
3 8  
4 6  
5 6  
6 6  
7 6  
8 6  
3 6  
4 5  
5 5  
6 5  
7 5  
8 5  
7 6  
8 5  
3 5  
4 5  
5 5  
6 5  
7 5  
8 5  
3 6  
4 4  
5 4  
6 4  
7 4  
8 4  
7 5  
8 4  
6 5  
7 4  
8 4  
5 5  
6 4  
7 4  
8 4  
4 5  
5 4  
6 4  
7 4  
8 4  
3 4  
4 4  
5 4  
6 4  
7 4  
8 4  
3 5  
4 3  
5 3  
6 3  
7 3  
8 3  
7 4  
8 3  
6 4  
7 3  
8 3  
5 4  
6 3  
7 3  
8 3  
4 4  
5 3  
6 3  
7 3  
8 3  
3 3  
4 3  
5 3  
6 3  
7 3  
8 3  
3 4  
4 2  
5 2  
6 2  
7 2  
8 2  
7 3  
8 2  
6 3  
7 2  
8 2  
5 3  
6 2  
7 2  
8 2  
4 3  
5 2  
6 2  
7 2  
8 2  
2 3  
3 2  
4 2  
5 2  
6 2  
7 2  
8 2  
3 3  
4 2  
5 2  
6 2  
7 2  
8 2  
1 3  
1 4  
1 5  
1 6  
1 7  
1 8  
2 8  
3 8  
4 8  
5 8  
6 8  
7 8  
8 8  
2 8  
3 8  
4 8  
5 8  
6 8  
7 8  
8 8  
1 8  
2 7  
3 7    
4 7  
5 7  
6 7  
7 7  
8 7  
1 7  
2 7  
3 7  
4 7  
5 7  
6 7  
7 7  
8 7  
1 8  
2 6  
3 6  
4 6  
5 6  
6 6  
7 6  
8 6  
1 6  
2 6  
3 6  
4 6  
5 6  
6 6  
7 6  
8 6  
1 7  
2 5  
3 5  
4 5  
5 5  
6 5  
7 5  
8 5  
1 5  
2 4  
3 4  
4 4  
5 4  
6 4  
7 4  
8 4  
1 4  
2 2  
3 2  
4 2  
5 2  
6 2  
7 2  
8 2  
1 2  
2 1  
3 1  
4 1  
5 1  
6 1  
7 1  
8 1  
7 2  
8 1  
6 2  
7 1  
8 1  
5 2  
6 1  
7 1  
8 1  
4 2  
5 1  
6 1  
7 1  
8 1  
3 2  
4 1  
5 1  
6 1  
7 1  
8 1  
2 2  
3 1  
4 1  
5 1  
6 1  
7 1  
8 1  

[我的代码]

grid = []
for i in range(10):
  grid.append([])
  for j in range(10):
    grid[i].append(0)

for i in range(10):
  grid[i] = list(map(int, input().split()))

p_x, p_y = (1,1)
grid[p_x][p_y] = 9

def move(p_x, p_y):
  for x in range(p_x,9):
    for y in range(p_y,9):
      print(x,y)

      if grid[x][y + 1] == 0:
        grid[x][y + 1] = 9
        move(x, y + 1)
      elif grid[x][y + 1] == 2:
        grid[x][y + 1] = 9
        break
      elif grid[x + 1][y] == 0:
        grid[x + 1][y] = 9
        move(x + 1, y)
      elif grid[x + 1][y] == 2:
        grid[x + 1][y] = 9
        break
      else:
        break
  
move(p_x, p_y)

python algorithm recursion
1个回答
0
投票

无需迭代特定范围内的 x 和 y 值,只需按顺序尝试每个移动,并递归调用

move
以获得下一个位置,从而正确跟踪当前位置。

def move(p_x, p_y):
    for dx in range(2):
        if grid[p_x + dx][p_y + 1 - dx] == 2:
            grid[p_x + dx][p_y + 1 - dx] = 9
            break
        elif grid[p_x + dx][p_y + 1 - dx] == 0:
            grid[p_x + dx][p_y + 1 - dx] = 9
            return move(p_x + dx, p_y + 1 - dx)

用迭代重写这个会更高效。

© www.soinside.com 2019 - 2024. All rights reserved.