Question: Please modify this function in Python. It is a Futoshiki solver that is similar to Latin Squares, but just needs a modification to the solver

Please modify this function in Python. It is a Futoshiki solver that is similar to Latin Squares, but just needs a modification to the solver method.
Here is the code:
def is_valid(board, row, col, num):
N = len(board)
# Check if the number is not in the same row and column
if num in board[row] or num in [board[i][col] for i in range(4)]:
return False
# Check inequality constraints
inequality_signs =['<','>','^','v']
for sign in inequality_signs:
if sign =='<':
if col >0 and board[row][col -1]== num:
return False
elif sign =='>':
if col < N -1 and board[row][col +1]== num:
return False
elif sign =='^':
if row >0 and board[row -1][col]== num:
return False
elif sign =='v':
if row < N -1 and board[row +1][col]== num:
return False
return True
def solve_futoshiki(board):
empty = find_empty_location(board)
if not empty:
return True # Puzzle solved
row, col = empty
for num in range(1,5): # Numbers 1 to 4 in a 4x4 grid
if is_valid(board, row, col, num):
board[row][col]= num
if solve_futoshiki(board):
return True # If solution is found, return True
board[row][col]=0 # Backtrack if the current configuration does not lead to a solution
return False
The output should be:
[2,1,4,3]
[3,1,4,2]
[4,3,1,2]
[2,1,3,1]
But right now my output is:
[1,2,3,4]
[2,1,4,3]
[3,4,1,2]
[4,3,2,1]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To modify the Futoshiki solver correctly you need to ensure both the constraints ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!