def h2(n, goalState): state = n.state count = 0 while state != goalState: emptyPos = getSpacePosition(state) emptyGoalPos = getSpacePosition(goalState) if emptyPos != emptyGoalPos: # Move right value in the position of the space value = goalState[emptyPos[0]][emptyPos[1]] rightValuePos = getValuePosition(state, value) state = swapValuesInPuzzle(emptyPos, rightValuePos, state) else: # Get wrong value position wrongValuePos = [] for i in range(len(state)): for j in range(len(state[i])): if state[i][j] != 0 and state[i][j] != goalState[i][j]: wrongValuePos = [i, j] break # Swap with empty position state = swapValuesInPuzzle(emptyPos, wrongValuePos, state) count += 1 return count
def h4(n, goalState): state = n.state count = 0 for i in range(len(state)): for j in range(len(state[i])): if state[i][j] != goalState[i][j]: rightValuePos = getValuePosition(goalState, state[i][j]) if rightValuePos[0] != i: count += 1 if rightValuePos[1] != j: count += 1 return count