pythonalgorithmrecursion

algorithm using recursive function


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

Hi, Dear! I ask your precious advice.. Thank you in advice

[Rule : Problem Statement]

I expected A output, but My code printed wrong output, B. I don't know why.. In detail, I expected the 'break' to work in (6,6), but it didn't. After going over (7,6), all zero is finally changed to 9. It is wrong result... In other words, the third 'elif' is not working.
Please let me know how to fix..

[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  

[My Code]

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)


Solution

  • Instead of iterating over the x and y values in a particular range, just try each move in order and make a recursive call to move for the next location, which keeps track of the current location correctly.

    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)
    

    It will be more efficient to rewrite this with iteration.