N queen problem implementation in python

class NQueen:
    def __init__(self, n):
        self.n = n
        self.board = self.create_borad(self.n)
        self.queens = 0
        self.row_exist = [0 for i in range(n)] # row check queen exist
        self.col_exist = [0 for i in range(n)] # column check queen exist
        self.main_exist = [0 for i in range(2 * n - 1)] # main diagnosis check queen exist
        self.vice_exist = [0 for i in range(2 * n - 1)] # vice diagnosis check queen exist
        
        self.count = 0 # number of solution
        
    # create the n queen board
    def create_borad(self, n):
        board = [[0 for row in range(n)] for col in range(n)]
        return board
    
    # return the size of the board
    def size(self):
        return self.n
    
    # return the number of queens on the board
    def num_queens(self):
        return self.queens
    
    # check if the cell can put a queen
    def unguarded(self, row, col):
        if self.row_exist[row] == 1:
            return False
        if self.col_exist[col] == 1:
            return False
        if self.main_exist[self.n + row - col - 1] == 1:
            return False
        if self.vice_exist[row + col] == 1:
            return False
        
        return True
    
    # place a queen on the board
    def place_queen(self, row, col):
        self.board[row][col] = 1
        self.queens += 1
        self.row_exist[row] = 1
        self.col_exist[col] = 1
        self.main_exist[self.n + row - col - 1] = 1
        self.vice_exist[row + col] = 1
    
    # remove a queen from the board
    def remove_queen(self, row, col):
        self.board[row][col] = 0
        self.queens -= 1
        self.row_exist[row] = 0
        self.col_exist[col] = 0
        self.main_exist[self.n + row - col - 1] = 0
        self.vice_exist[row + col] = 0
    
    # draw the queens board
    def draw(self):
        for i in range(self.size()):
            print self.board[i]
#            for j in range(self.size()):
#                print self.board[i][j]
#            print
    
    # backtracking solve the problem
    def solve(self, row, col):
        if row > self.n:
            return 
        if self.num_queens() == self.n:
            self.count += 1
            print 'the count is: ', self.count
            self.draw()
        else:
            for row in range(self.size()):
                if self.unguarded(row, col):
                    self.place_queen(row, col)
                    self.solve(row + 1, col + 1)
                    self.remove_queen(row, col)

if __name__ == '__main__':
    q = NQueen(8)
    q.solve(0, 0)
    
    
                
                
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值