Question

In: Computer Science

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 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 output of the python Script

Solutions

Expert Solution

#python Code Backtracking


import math
ways = 0


def solveBoard(board, row, rowmask,
                           ldmask, rdmask):

   n = len(board)
  

   all_rows_filled = (1 << n) - 1
  

   if (rowmask == all_rows_filled):
       global ways
       ways = ways + 1
       print("=====================")
       print("Queen placement - " + (str)(ways))
       print("=====================")
       printBoard(board)
      

   # placement of iteration for row index
   safe = all_rows_filled & (~(rowmask |
                               ldmask | rdmask))
  
   while (safe > 0):
  
  
       p = safe & (-safe)
       col = (int)(math.log(p)/math.log(2))
       board[row][col] = 'Q'
      
       solveBoard(board, row+1, rowmask|p,
                       (ldmask|p) << 1, (rdmask|p) >> 1)
      
  
       safe = safe & (safe-1)
      
       # Backtracking
       board[row][col] = ' '

# Program to print board
def printBoard(board):
   for row in board:
       print("|" + "|".join(row) + "|")

# Driver Code     
def main():

   n = 4; # board size
   board = []
  
   for i in range (n):
       row = []
       for j in range (n):
           row.append(' ')
       board.append(row)

   rowmask = 0
   ldmask = 0
   rdmask = 0
   row = 0
  
   # Function Call
   solveBoard(board, row, rowmask, ldmask, rdmask)
  
   # creates a new line
   print()
   print("Number of ways to place " + (str)(n) +
                       " queens : " + (str)(ways))
  
if __name__== "__main__":
   main()


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 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)
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
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...
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
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...
Write a Python program in a file called consonants.py, to solve the following problem using a...
Write a Python program in a file called consonants.py, to solve the following problem using a nested loop. For each input word, replace each consonant in the word with a question mark (?). Your program should print the original word and a count of the number of consonants replaced. Assume that the number of words to be processed is not known, hence a sentinel value (maybe "zzz") should be used. Sample input/output: Please enter a word or zzz to quit:...
Write a script or function in Python that approximates the solution to the system ??⃗=?⃗⃗ using...
Write a script or function in Python that approximates the solution to the system ??⃗=?⃗⃗ using the Jacobi Method. The inputs should be an nxn matrix A, an n-dimensional vector?⃗⃗, a starting vector ?⃗0,an error tolerance ?, and a maximum number of iterations N. The outputs should be either an approximate solution to the system??⃗=?⃗⃗ or an error message, along with the number of iterations completed. Additionally, it would be wise to build in functionality that allows you to optionally...
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A*...
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is a puzzle invented and popularized by Noyes Palmer Chapman in the 1870s. It is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order, using as few moves as possible. You are permitted to slide blocks...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT