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
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
Thanks for code
ReplyDeletehow to run input.json file.
ReplyDeletePlease send the steps how to run this program.
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.
Delete1. Open terminal in a system with python installed.
2. Type the following command - python filename.py
3. It will execute the code.
can anyone explain this code....Please...!
DeleteFile "8queens.py", line 4
DeleteSyntaxError: Non-ASCII character '\xc2' in file 8queens.py on line 4, but no encoding declared;
Please can u explain the code in details?
ReplyDeleteWhat is the time complexity of this program
ReplyDeletei have not input.json file
ReplyDeletehow to get
please explain this program in brief??
ReplyDelete