N Queens Problem Solution for Square Grid in Python

I recently wrote some code on the flight from Boston to CA to get at least 1 solution to the N Queens problem.

This is a recursive backtracking based algorithm – which I have solved using iterative backtracking.

The code is attached and written in python.
This code may not be super optimized but is small enough and correct.

We can add a wrapper on top of this code easily to output all the possible solutions of the N Queens problem

This program takes 1 parameter – the Size of the Grid. It is assumed that it is a Square Grid

It outputs the possible coordinates to place the N Queens
The grid Index starts from 1

Simple way to run the program using Python interpreter

Command Line

python 8QueensProblem.py <N>

Where <N> is the Grid Size

Code

https://github.com/palsumitpal/sumitpython/blob/master/8QueensProblem.py

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s