Question

In: Computer Science

Second Time Posting - Do not Rush to answer question - Must be done in Python...

Second Time Posting - Do not Rush to answer question - Must be done in Python - Please HELP

Write and Compile a python script to solve the 4-queens problem using the Iterative Improvement 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

#Question solution-Python program to solve 4*4 Queen problem= First We draw the 4* 4 queen picture as following.

Q=4
Q=4
Q=4
Q=4

N = 4

def printQueen(board):

    for i in range(N):

        for j in range(N):

            print board[i][j],

        print

def isSafeQueen(board, row, col):

    # now Check this row on left side

    for i in range(col):

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

            return False

    # now 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

    # we can 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

    # let us this column and try placing

    # In this queen in all rows one by one

    for i in range(N):

        if isSafeQueen(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

    # 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

    printQueen(board)

    return True

output=================

0 0 1 0
1 0 0 0
0 0 0 0
0 1 0 0

Related Solutions

This is my second time posting this question. Can the answer be typed please? Its hard...
This is my second time posting this question. Can the answer be typed please? Its hard to read someones handwritten cursive response. In recent years, many elderly people have purchased Medigap insurance policies to cover a growing Medicare copayment. These policies cover some or all of the medical costs not covered by Medicare. Use economic theory to explain how the growth of these policies is likely to influence the demand for health care by elderly people.
Second time posting: Actual question is How would you structure the datasets to analyze this relationship?...
Second time posting: Actual question is How would you structure the datasets to analyze this relationship? For some reason everyone discusses the trade and economic growth as if this is a writing class but Im just trying to figure out how to answer the question about analyzing the relationship with datasets. THANKS Lets say you are interested in how levels of trade for countries influence economic growth. How would you structure the dataset to analyze this relationship
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer...
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer Protocol (FTP) application that transmits files between a client and a server using Python. Your programs should implement four FTP commands: (1) change directory (cd), (2) list directory content (ls), (3) copy a file from client to a server (put) and (4) copy a file from a server to a client (get). Implement two versions of the program: one that uses stock TCP for...
posting same question for second times don't want any wrong answer or half complete Statement of...
posting same question for second times don't want any wrong answer or half complete Statement of Cash Flows—Indirect Method The comparative balance sheet of Yellow Dog Enterprises Inc. at December 31, 20Y8 and 20Y7, is as follows: Dec. 31, 20Y8 Dec. 31, 20Y7 Assets Cash $73,750 $90,850 Accounts receivable (net) 113,320 122,480 Merchandise inventory 161,890 151,810 Prepaid expenses 6,600 4,600 Equipment 329,780 271,980 Accumulated depreciation-equipment (85,740) (66,700) Total assets $599,600 $575,020 Liabilities and Stockholders' Equity Accounts payable (merchandise creditors) $125,920...
THIS IS THE SECOND TIME I AM UPLOADING THIS QUESTION - PLEASE READ THE REQUIREMENT! ANSWER...
THIS IS THE SECOND TIME I AM UPLOADING THIS QUESTION - PLEASE READ THE REQUIREMENT! ANSWER IN 150 WORDS OR LESS! Section 1 (3 marks: You can start this section after Week 4 lecture) Read the following article: Harford, T. (2014), ‘Big data: A big mistake?’, Significance 11(5), 14–19. Question: Critically evaluate the main points of the article using three bullet points, in less than 150 words in total. Critical evaluation means To give your opinion on something To support...
THIS IS THE THIRD TIME POSTING IT!!! CAN SOMEONE ANSWER THIS AND TYPE THE RESPONSE. THANK...
THIS IS THE THIRD TIME POSTING IT!!! CAN SOMEONE ANSWER THIS AND TYPE THE RESPONSE. THANK YOU! What are one example of a monopolistically competitive firm, oligopoly, and monopoly in your local area? In your example, please relate to the market characteristics of each of the market structures? Work cite and type is a most!!!!!
This is my 3rd time posting this question, first time was wrong: The Zagat Restaurant Survey...
This is my 3rd time posting this question, first time was wrong: The Zagat Restaurant Survey provides food, decor, and service ratings for some of the top restaurants across the United States. For 18 restaurants located in a certain city, the average price of a dinner, including one drink and tip, was $48.60. You are leaving on a business trip to this city and will eat dinner at three of these restaurants. Your company will reimburse you for a maximum...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
Note: this is my second time submitting this question. Previous answer for cost of equity percentage...
Note: this is my second time submitting this question. Previous answer for cost of equity percentage (10.19%) and WACC (7.54%) were wrong. please try to do again.   cost of debt of 5.52% is correct. Thank you, ----------------------------------------------------------------------------- If Wild Widgets, Inc., were an all-equity company, it would have a beta of .90. The company has a target debt-equity ratio of .45. The expected return on the market portfolio is 11 percent and Treasury bills currently yield 2.9 percent. The company...
Question 3 30 marks Use the following information to answer questions 3.1 and 3.2 Rush &...
Question 3 30 marks Use the following information to answer questions 3.1 and 3.2 Rush & Reddy Pty Ltd The balance sheet taken from the company’s year-end financials is provided below: Assets Dec-31 2015 R Dec-31 2014 R Dec-31 2013 R Current Assets Cash 122,200 108,000 Accounts Receivable 45,000 35,000 37,000 Inventory 17,000 14,000 13,000 Other Current Assets 13,000 11,000 Total Current Assets 197,200 168,000 50,000 Long-term Assets Property, Plant & Equipment, net 1,489,800 50,000 Intangible Assets 422,500 450,000 Total...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT