The Artima Developer Community
Sponsored Link

Python Buzz Forum
Generating solutions to the 8 Queens Puzzle

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Thomas Guest

Posts: 236
Nickname: tag
Registered: Nov, 2004

Thomas Guest likes dynamic languages.
Generating solutions to the 8 Queens Puzzle Posted: Aug 4, 2006 8:16 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Thomas Guest.
Original Post: Generating solutions to the 8 Queens Puzzle
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
Latest Python Buzz Posts
Latest Python Buzz Posts by Thomas Guest
Latest Posts From Word Aligned: Category Python

Advertisement

Here’s a Python solution to the 8 Queens puzzle. I think that generator functions are perfectly suited to this kind of thing: they are easy to code and easy to use, they yield results as soon as they can, and clients have full control over when to stop them yielding results.

The Solution

queens.py
#!/bin/env python
""" General solution to the 8 queens puzzle.
"""

class queen:
    """ A queen on a chess board.
    """
    def __init__(self, row, col):
        self.row, self.col = row, col
    def __str__(self):
        return "(%d, %d)" % (self.row, self.col)
    def safe_from(self, queens):
        """ Is this queen safe from the list of queens?
        """
        for queen in queens:
            if self.attacked_by(queen):
                return False
        return True
    def attacked_by(self, queen):
        """ Is this queen attacked by the input queen?
        """
        r, c = self.row, self.col
        qr, qc = queen.row, queen.col
        def same_row():
            return r == qr
        def same_column():
            return c == qc
        def same_diagonal():
            return r + c == qr + qc or r - c == qr - qc
        return same_row() or same_column() or same_diagonal()

def queens_puzzle(board_size):
    """ Generate solutions to the Queens puzzle for the input board size.
    """
    def queen_columns(cols):
        if cols == 0:
            yield list()
        else:
            cols -= 1
            for queens in queen_columns(cols):
                for row in range(board_size):
                    new_queen = queen(row, cols)
                    if new_queen.safe_from(queens):
                        yield queens + [new_queen]
    for queens in queen_columns(board_size):
        yield queens

try:
    while True:
        board_size = int(raw_input("board size? "))
        for queens in queens_puzzle(board_size):
            print ", ".join(["%s" % qq for qq in queens])
except ValueError, v:
    print "Bye for now!"

Read: Generating solutions to the 8 Queens Puzzle

Topic: PeopleAggregator v0.01/r9 released Previous Topic   Next Topic Topic:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use