element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog Brute force Python solution for Queens Chess Problem
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: urkraft
  • Date Created: 13 May 2018 8:37 PM Date Created
  • Views 2307 views
  • Likes 1 like
  • Comments 2 comments
  • Code Exchange
Related
Recommended

Brute force Python solution for Queens Chess Problem

urkraft
urkraft
13 May 2018

Don't know if anyone is interested in this, but since i just finished my quick and dirty brute force solution of the Queens Chess Problem using Python i thought that maybe i should publish it to see what kind of feedback i will get:

 

#!/usr/bin/python3'''Program to solve the "n x n chess board and n queens chess problem" fromMagPi 68 (magpi.cc/JgrHLH)Date: 13MAY2018Author: raymond gordon'''import sys, copy, pprint


def is_solution(coordinates, dimensions):
   # check if multiple queens on same row   for x in range(dimensions):
  pos = coordinates[x]
   for next_col in range(x + 1, dimensions):
  next_pos = coordinates[next_col]
   # check if multiple queens on same row   if pos == next_pos:
   return None   # check if multiple queens on same diagonal   if (pos + next_col - x) == next_pos:
   return None  if (pos - next_col + x) == next_pos:
   return None  return coordinates


def main():
  solutions = []
   dim = 0   while True:
  dim = input('Enter the size of the chess board: ')
   if dim.isdecimal():
   break   print('An intiger value is required. ', end='')
  dim = int(dim)
   if dim < 4:
   print('No solution exists for a board of dimension', dim, 'x', dim)
   print('Exiting program ...')
   return   # populate this_permutation   this_permutation = []
   for n in range(dim):
  this_permutation.append(0)

   # calculate the number of permutations   possible_permutations = dim
   for n in range(dim):
  possible_permutations *= dim

   # initialize the counter  #placement_cnt = 0  # go through the permutations searching for all possible solutions   for n in range(possible_permutations):
  rem = n
   for m in range(dim):
  this_permutation[m] = rem % dim
  rem //= dim
   # is this_permutation a solution?   result = is_solution(copy.copy(this_permutation), dim)
   if result:
   # was this solution encountered previously?   if result not in solutions:
  solutions.append(result)

   print('\nInvestigated', possible_permutations, 'possibilities.')
  sol_cnt = len(solutions)
   print(sol_cnt, 'solutions found.')

   print('\n==========\nSolutions:\n==========')
   print('\n("x" position is position of value in array.\n"y" position is value in array)\n')
  cnt = 0   for solution in solutions:
  cnt += 1   print()
   print('Solution', cnt, ':')
  pprint.pprint(solution)


if __name__ == '__main__':
  main()

Unfortunately, the code is not being formatted properly here, so it is probably completely hopeless to understand.

 

- raymond

  • Sign in to reply
  • urkraft
    urkraft over 7 years ago

    Here is a link to a version of the program that also renders a visualization of each solution: https://pastebin.com/CePZuRkJ

    I should also mention a couple of relevant bits of information about the program:

    • The program prompts for the size of the chess board and expects a single integer value (for example 8 for all solutions of 8 queens on an 8 x 8 board.
    • The program uses a brute force algorythm, and the time required to find solutions will be exponential. Responses to 4, 5, and 6 should be fast, but greater values will take dramatically greater amounts of time to solve. 8 took my pc 22 minutes to solve!

     

    Enjoy!

     

    - raymond

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • urkraft
    urkraft over 7 years ago

    The code should be easier to follow here: https://pastebin.com/Lk2BvbUY

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube