Question

In: Computer Science

Write a python script to solve the 4-queens problem using. The code should allow for random...

Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting.

"The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal."

Display the iterations until the final solution

Hill Climbing (your choice of variant)

Solutions

Expert Solution

def isSafe (board, row, col):

# check left row

for y in range(col):

if board[row][y] == 1:

return False

# check diagonal left top

for x, y in zip(range(row, -1, -1), range(col, -1, -1)):

if board[x][y] == 1:

return False

# check diagonal left bottom

for x, y in zip(range(row, N, 1), range(col, -1, -1)):

if board[x][y] == 1:

return False

return True

def generateSolution(board, col):

# terminating condition

# all columns covered

global N

if col >= N:

return True

# loop over all the rows

for i in range(N):

if isSafe(board, i, col) == True:

board[i][col] = 1

# recursively place other queens

if generateSolution(board, col + 1) == True:

return True

# unmark queen spot

board[i][col] = 0

# backtrack

return False

N = int(input())

startCol = 0

board = [[0 for i in range(N)] for j in range(N)]

# print(board)

if generateSolution(board, startCol) == False:

print("No Solution Exists")

else:

print("Solution exists")

print(board)


Related Solutions

Write and Compile a python script to solve the 4-queens problem using. The code should allow...
Write and Compile a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting using numpy "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Arc consistency
Write and Compile a python script to solve the 4-queens problem using Forward Checking algorithm ....
Write and Compile a python script to solve the 4-queens problem using Forward Checking algorithm . The code should allow for random starting, and for placed starting. Random Starting means randomly place a queen position on the chessboard. Placed Starting means asked for the user input to place a queen position on the chessboard. Display the iterations until the final solution "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens...
Python Code for 8-queens using random restart algorithms
Python Code for 8-queens using random restart algorithms
For this problem you should describe the algorithm using a flowchart and then write Python code...
For this problem you should describe the algorithm using a flowchart and then write Python code to implement the algorithm. You are to create a Python program file for your code. Include in your HW document a photo of your flowchart and a screenshot of your program file and the output in IDLE that results when you run the program. The algorithm: Ask the user to input their annual salary and then calculate and print the weekly salary (annual/52). I...
please use python to solve this problem. Math and String operations Write a script to perform...
please use python to solve this problem. Math and String operations Write a script to perform various basic math and string operations. Use some functions from Python's math module. Generate formatted output. Basic math and string operations Calculate and print the final value of each variable. a equals 3 to the power of 2.5 b equals 2 b equals b + 3 (use +=) c equals 12 c = c divided by 4 (use /=) d equals the remainder of...
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow...
USING RSTUDIO: Write a script that allows two people to play tic-tac-toe. The script should allow each player to take sequential turns and to interactively enter their choice of position at the command line on each turn. For each turn, the script should output the set-up of the board. Note, you don’t have to use “x” and “o” as the symbol of each player. Note, the main body of this code (not including functions) should be able to be written...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
Write a Python script that will be used as a calculator to allow simple arithmetic computation...
Write a Python script that will be used as a calculator to allow simple arithmetic computation of two input integer numbers. Requirements: a. The script should first allow the user to select if they want to add, subtract, multiply or divide two integer digits. b. The script should pass the user inputs to a function using arguments for computation and the results return to the main part of the script. c. The return value will be tested to determine if...
#python. Explain code script of each part using comments for the reader. The code script must...
#python. Explain code script of each part using comments for the reader. The code script must work without any errors or bugs. Ball moves in a 2D coordinate system beginning from the original point (0,0). The ball can move upwards, downwards, left and right using x and y coordinates. Code must ask the user for the destination (x2, y2) coordinate point by using the input x2 and y2 and the known current location, code can use the euclidean distance formula...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT