Question

In: Computer Science

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

Solutions

Expert Solution

# Python3 program to solve N Queen 
# Problem using backtracking 
global N 
N = 4

def printSolution(board): 
        for i in range(N): 
                for j in range(N): 
                        print (board[i][j], end = " ") 
                print() 

# A utility function to check if a queen can 
# be placed on board[row][col]. Note that this 
# function is called when "col" queens are 
# already placed in columns from 0 to col -1. 
# So we need to check only left side for 
# attacking queens 
def isSafe(board, row, col): 

        # Check this row on left side 
        for i in range(col): 
                if board[row][i] == 1: 
                        return False

        # Check upper diagonal on left side 
        for i, j in zip(range(row, -1, -1), 
                                        range(col, -1, -1)): 
                if board[i][j] == 1: 
                        return False

        # Check lower diagonal on left side 
        for i, j in zip(range(row, N, 1), 
                                        range(col, -1, -1)): 
                if board[i][j] == 1: 
                        return False

        return True

def solveNQUtil(board, col): 
        
        # base case: If all queens are placed 
        # then return true 
        if col >= N: 
                return True

        # Consider this column and try placing 
        # this queen in all rows one by one 
        for i in range(N): 

                if isSafe(board, i, col): 
                        
                        # Place this queen in board[i][col] 
                        board[i][col] = 1

                        # recur to place rest of the queens 
                        if solveNQUtil(board, col + 1) == True: 
                                return True

                        # If placing queen in board[i][col 
                        # doesn't lead to a solution, then 
                        # queen from board[i][col] 
                        board[i][col] = 0
                
                print("Iteration", i, "- ")
                printSolution(board)

        # if the queen can not be placed in any row in 
        # this colum col then return false 
        return False

# This function solves the N Queen problem using 
# Backtracking. It mainly uses solveNQUtil() to 
# solve the problem. It returns false if queens 
# cannot be placed, otherwise return true and 
# placement of queens in the form of 1s. 
# note that there may be more than one 
# solutions, this function prints one of the 
# feasible solutions. 
def solveNQ(): 
        board = [ [0, 0, 0, 0], 
                        [0, 0, 0, 0], 
                        [0, 0, 0, 0], 
                        [0, 0, 0, 0] ] 

        if solveNQUtil(board, 0) == False: 
                print ("Solution does not exist") 
                return False

        print("Final Solution")
        printSolution(board) 
        return True

# Driver Code 
solveNQ() 


Related Solutions

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)
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...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
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...
Python Code for 8-queens using random restart algorithms
Python Code for 8-queens using random restart algorithms
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT