0
파이썬에서 8 가지 퍼즐을 풀 때 BFS에 문제가 있습니다. 원본을 수정하는 대신 새로운 퍼즐을 반환하는 이동 방법이 필요합니다. 아래 출력은 생성되는 새로운 퍼즐 대신 원래 퍼즐이 수정되었음을 보여줍니다. 나는 사본을 시도했다 그러나 그것은 작동하지 않으며 명부에는 깊은 사본이 없다. 원본에서 새 객체를 만드는 간단한 방법이 있습니까?새 변수를 만드는 대신 원래 변수를 수정하는 파이썬
def move(puzzle, direction):
# find where the zero (gap) is and swap in the given direction
row_count = 0
for row in puzzle:
if '0' in row:
gapx = row.index('0')
gapy = row_count
row_count = row_count + 1
# move it accordingly, as long as the move is legal
if direction == 'up' and (gapy==1 or gapy==2):
temp = puzzle[gapy-1][gapx]
puzzle[gapy-1][gapx] = '0'
puzzle[gapy][gapx] = temp
return puzzle
pretty_print(puzzle_state)
new_puzzle = move(puzzle_state, 'up')
pretty_print(puzzle_state)
는 출력 :
123
804
765
103
824
765
왜 'deepcopy'목록을 만들 수 없다고 생각하십니까? –
내가 잘못 사용하고있는 것처럼 보입니다. 감사! – mobone