Monday, October 26, 2015

8-Queens Matrix is Stored using JSON/XML having first Queen placed, use back-tracking to place remaining Queens to generate final 8-queen’s Matrix using Python.

PROGRAM

filename: b1.py

import json

def isattack(board,r,c):
    for i in range(r):
        if(board[i][c]==1):
            return True
    
    i=r-1
    j=c-1
    while((i>=0) and (j>=0)):
        if(board[i][j]==1):
            return True
        i=i-1
        j=j-1
    
    i=r-1
    j=c+1
    while((i>=0) and (j<8)):
        if(board[i][j]==1):
            return True
        i=i-1
        j=j+1
    return False
    
def solve(board,row):
    i=0
    while(i<8):
        if(not isattack(board, row, i)):
            board[row][i]=1
            if(row==7):
                return True
            else:
                if(solve(board, row+1)):
                    return True
                else:
                    board[row][i]=0
        i=i+1
    
    if(i==8):
        return False
    
def printboard(board):
    for i in range(8):
        for j in range(8):
            print str(board[i][j])+"  ",
        print "\n"
        
board = [[0 for x in range(8)] for x in range(8)]

if __name__ == '__main__':
    data=[]
    with open('input.json') as f:
        data=json.load(f)
    
    if(data["start"]<0 or data["start"]>7):
        print "Invalid JSON input"
        exit()
    
    board[0][data["start"]]=1
    if(solve(board, 1)):
        print "Queens problem solved!!!"
        print "Board Configuration:"
        printboard(board)
    else:
        print "Queens problem not solved!!!"
    
filename: input.json
{"start":4}


OUTPUT

Amols-Air:b1 Darwin$ python solve.py
Queens problem solved!!!
Board Configuration:
0   0   0   0   1   0   0   0   

1   0   0   0   0   0   0   0   

0   0   0   1   0   0   0   0   

0   0   0   0   0   1   0   0   

0   0   0   0   0   0   0   1   

0   1   0   0   0   0   0   0   

0   0   0   0   0   0   1   0   

0   0   1   0   0   0   0   0  

9 comments:

  1. how to run input.json file.
    Please send the steps how to run this program.

    ReplyDelete
    Replies
    1. To run this code just put both the files in the same folder and run the .py file how you execute a normal python code.
      1. Open terminal in a system with python installed.
      2. Type the following command - python filename.py
      3. It will execute the code.

      Delete
    2. can anyone explain this code....Please...!

      Delete
    3. File "8queens.py", line 4
      SyntaxError: Non-ASCII character '\xc2' in file 8queens.py on line 4, but no encoding declared;

      Delete
  2. Please can u explain the code in details?

    ReplyDelete
  3. What is the time complexity of this program

    ReplyDelete
  4. i have not input.json file
    how to get

    ReplyDelete
  5. please explain this program in brief??

    ReplyDelete

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...