Question: How to get this desired output: Solution: [ 2 , 4 , 1 , 3 ] [ 3 , 1 , 4 , 2 ]

How to get this desired output:
Solution:
[2,4,1,3]
[3,1,4,2]
[4,3,2,1]
[1,2,3,4]
With this 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
# 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 for a 4x4 puzzle
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 # No solution found
def find_empty_location(board):
for i in range(4):
for j in range(4):
if board[i][j]==0:
return i, j
return None
def print_board(board):
for row in board:
print(row)
# Create an empty Futoshiki puzzle grid (4x4)
empty_futoshiki_puzzle =[
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
]
# Print the initial state board
print("Initial State:")
print_board(empty_futoshiki_puzzle)
# Solve the empty puzzle
if solve_futoshiki(empty_futoshiki_puzzle):
print("
Solution:")
print_board(empty_futoshiki_puzzle)
else:
print("
No solution found.")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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!