473,761 Members | 8,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Large algorithm issue -- 5x5 grid, need to fit 5 queens plus some squares

Background:
The problem I'm trying to solve is.
There is a 5x5 grid.
You need to fit 5 queens on the board such that when placed there are
three spots left that are not threatened by the queen.

My thinking:
I created a list, named brd, that represents the board.
I made it such that brd[1] would be the first square on the grid, and
brd[25] would be the bottom right end of the grid.
Like this:
| 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 |10|
|11|12 |13|14 |15|
|16|17 |18|19 |20|
|21|22 |23|24 |25|

Next,
I created 4 functions
The first named clearbrd() which takes no variables, and will reset the
board to the 'no-queen' position.
The second function I made was name permute(seq,n) and will create
every combination of the placement of 5 queens.
The third function is the printbrd() function, which takes no input,
and prints the board.

The final, and most important function is the affect(u) function, where
u is the position of the queen on the grid, and it makes that value 1,
then it finds all the places that the queen threatens, and makes those
values 3.
For example -- If I was to do,
affect(1)
printbrd()
it would output
|1|3|3|3|3|
|3|3|0|0|0|
|3|0|3|0|0|
|3|0|0|3|0|
|3|0|0|0|3|
The last function of my code is where I create a for loop that takes
all the combinations of the queens position, and puts them on the
board, counts the numbers of zero, and if it's >= 3, it outputs the
location of the queens, and the board.

Problem:
It doesn't output anything. Even when I change the mininum number of
0's to 1, it doesn't output anything.
I tried taking it and statically inputing the vars for it to have two
zeros, and made the mininum number 2. and it says that it is a correct
answer. Then I took permute() and pasted all the output to a file, and
it had the combination I tried. So I don't understand why it's not
working.

Thanks for all of your help guys,
Poz

The Code:
#!/usr/bin/env python
brd = [9,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0]
def clearbrd():
brd = [9,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0]
a = "|"
def permutate(seq,n ):
if n == 0:
yield[]
else:
for i in range(len(seq)) :
for subseq in permutate(seq[:i] + seq[i + 1:], n - 1):
yield [seq[i]] + subseq
def printbrd():
print a,brd[1],a,brd[2],a,brd[3],a,brd[4],a,brd[5],a,"\n"
print a,brd[6],a,brd[7],a,brd[8],a,brd[9],a,brd[10],a,"\n"
print a,brd[11],a,brd[12],a,brd[13],a,brd[14],a,brd[15],a,"\n"
print a,brd[16],a,brd[17],a,brd[18],a,brd[19],a,brd[20],a,"\n"
print a,brd[21],a,brd[22],a,brd[23],a,brd[24],a,brd[25],a,"\n"
def affect(u):
origu = u
brd[u] = 1
# Do Diagonal down to the right
while u!=5 and u!=10 and u!=15 and u<20:
u = u + 6
brd[u] = 3
u = origu
# Do Diagonal down to the left
while u!=1 and u!=6 and u!=11 and u!=16 and u!=21 and u<22:
u = u + 4
brd[u] = 3
u = origu
# Do horizontal to the left
while u!=1 and u!=6 and u!=11 and u!=16 and u!=21:
u = u - 1
brd[u] = 3
u = origu
# Do horizontal to the right
while u!=5 and u!=10 and u!=15 and u!=20 and u!=25:
u = u + 1
brd[u] = 3
u = origu
# Do down
while u < 21:
u = u + 5
brd[u] = 3
u = origu
# Do up
while u > 5:
u = u - 5
brd[u] = 3
u = origu
# do Diagonal left up
while u>6 and u!=11 and u!=16 and u!=21:
u = u - 6
brd[u] = 3
u = origu
# do Diagonal right up
while u>5 and u!=10 and u!=15 and u!=20 and u!=25:
u = u - 4
brd[u] = 3
answeru = origu
clearbrd()
for v,w,x,y,z in permutate(range (1,26),5):
affect(v)
affect(w)
affect(x)
affect(y)
affect(z)
if brd.count(0) >= 3:
print "-----------------"
print "Solved",v,w,x, y,z
printbrd()
clearbrd()

Thanks for all of your help guys!
Poz

Mar 16 '06 #1
9 2293
to**********@gm ail.com wrote:
The first named clearbrd() which takes no variables, and will reset the
board to the 'no-queen' position. (snip) The Code:
#!/usr/bin/env python
brd = [9,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0]
def clearbrd():
brd = [9,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0]


clearbrd() isn't doing what you want it to. It should be written as:

def clearbrd():
global brd
brd = [9,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0]

Explanation:
http://www.python.org/doc/faq/progra...-in-a-function

--Ben

Mar 16 '06 #2
It looks like a good start! Some tips-

- Index your arrays starting from 0 instead of 1. It will make life
easier (and it's the convention in most modern languages)

- Try a two dimensional array for the board representation? A list of
lists will do:

brd = [ [0] * 5 for i in xrange(5) ]

Now "brd[row][col]" will give the value of the square at that row and
column.

The printbrd function becomes:

def printbrd():
for row in xrange(5):
for col in xrange(5):
print brd[row][col],
print "" # print automatically adds a newline unless you
follow with a comma

- Or better yet, you don't even need a board representation. You can
only have five queens, as opposed to 25 squares, so instead of storing
a whole board, just store a list of the positions of queens.

- affect is going to wipe out previous queens "1" with a "3", and you
could still get a count of three or more zeros.

- Try to generalize the problem from 5 queesn on 5x5 to N queens on
NxN. Is 7 possible? How about 8?

- the permute function is a nice use of generators

Mar 16 '06 #3
to**********@gm ail.com wrote:
The problem I'm trying to solve is.
There is a 5x5 grid.
You need to fit 5 queens on the board such that when placed there are
three spots left that are not threatened by the queen.


when you're done with your homework (?), you can compare it with
Guido's solution:

http://svn.python.org/view/python/tr...ipts/queens.py

</F>

Mar 16 '06 #4
Fredrik Lundh wrote:
to**********@gm ail.com wrote:
The problem I'm trying to solve is.
There is a 5x5 grid.
You need to fit 5 queens on the board such that when placed there are
three spots left that are not threatened by the queen.


when you're done with your homework (?), you can compare it with
Guido's solution:

http://svn.python.org/view/python/tr...ipts/queens.py

</F>

Or, Tim Peters' generator-based one:

http://svn.python.org/view/python/tr..._generators.py

Michael

Mar 16 '06 #5
Thank you very much guys!
Just for clarification it wasn't homework, just extra credit :)

I can't beleive I didn't realize that I didn't clear the GLOBAL
variable :D

Mar 16 '06 #6
Em Qui, 2006-03-16 Ã*s 09:20 +0100, Fredrik Lundh escreveu:
when you're done with your homework (?), you can compare it with
Guido's solution:

http://svn.python.org/view/python/tr...ipts/queens.py


Just a curiosity. Running the script as the site lists on my computer:

$ time python2.4 /tmp/queens.py -n 12
Found 14200 solutions.

real 0m14.177s
user 0m13.700s
sys 0m0.042s

Adding a "import psyco; psyco.full()" clause to the beginning of the
file:

$ time python2.4 /tmp/queens.py -n 12
Found 14200 solutions.

real 0m3.250s
user 0m3.003s
sys 0m0.012s

At least interesting...

Felipe.

Mar 16 '06 #7
Sorry to bring this up again, but I decided to try to re-create the
program, using the 2d array.
However, I ran into a slight problem.
How will the permutation function have to be modified?
I'm having issues trying to figure out how it works, and how it would
need to be modified to use it correctly (I used it from a cookbook, and
didn't bother figuring it out)

Mar 17 '06 #8
"Fredrik Lundh" <fr*****@python ware.com> wrote:
to**********@g mail.com wrote:
The problem I'm trying to solve is.
There is a 5x5 grid.
You need to fit 5 queens on the board such that when placed there are
three spots left that are not threatened by the queen.


when you're done with your homework (?), you can compare it with
Guido's solution:

http://svn.python.org/view/python/tr...ipts/queens.py


That solves a different problem. The traditional N queens problem would be
"place 5 queens so that none of them threatens another". That's very
different from his problem specification.

It turns out there is only 1 unique (non-rotated, non-reflected) solution
to the problem as he posted it.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 17 '06 #9
"to**********@g mail.com" <to**********@g mail.com> wrote:
Background:
The problem I'm trying to solve is.
There is a 5x5 grid.
You need to fit 5 queens on the board such that when placed there are
three spots left that are not threatened by the queen.


I know this wasn't a contest, but here's my solution. This finds 8
solutions, which are all reflections and rotations of each other:

rows = (
( 1, 2, 3, 4, 5 ),
( 6, 7, 8, 9, 10 ),
( 11, 12, 13, 14, 15 ),
( 16, 17, 18, 19, 20 ),
( 21, 22, 23, 24, 25 ),
( 1, 6, 11, 16, 21 ),
( 2, 7, 12, 17, 22 ),
( 3, 8, 13, 18, 23 ),
( 4, 9, 14, 19, 24 ),
( 5, 10, 15, 20, 25 ),
( 16, 22 ),
( 11, 17, 23 ),
( 6, 12, 18, 24 ),
( 1, 7, 13, 19, 25 ),
( 2, 8, 14, 20 ),
( 3, 9, 15 ),
( 4, 10 ),
( 2, 6 ),
( 3, 7, 11 ),
( 4, 8, 12, 16 ),
( 5, 9, 13, 17, 21 ),
( 10, 14, 18, 22 ),
( 15, 19, 23 ),
( 20, 24 )
)

zeros = [ 0 ] * 25

def printme( cells ):
for i,j in enumerate(cells ):
print "%2d" % j,
if i % 5 == 4:
print

def check( queens ):
cells = zeros[:]
for q in queens:
for row in rows:
if q in row:
for x in row:
cells[x-1] = 1
nils = len( [1 for k in cells if not k] )
if nils >= 3:
for i in queens:
cells[i-1] = 9
print queens
printme( cells )

for q1 in range(25):
for q2 in range(q1+1,25):
for q3 in range(q2+1,25):
for q4 in range(q3+1,25):
for q5 in range(q4+1,25):
check( [q1+1,q2+1,q3+1, q4+1,q5+1] )
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6746
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals. How does downfree mark the diagonal?
0
8566
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row and add child grid into that row because the columns in child grid are same as parent grid and we want to align them with the cols of parent grid. 2. If there is data for child grid we show a plus image in the parent row (in the row above the...
8
8879
by: lupo666 | last post by:
Hi again, every I go into Design mode in a report and move something around, it gets "disaligned" with respect to the underlying grid, either at 8X8 or 9X9 (don't know why at 10X10 you can't see tha grid, either...) and I have to manually select every control, textbox, whatever and move each of the sides up and down to "re-align" it correctly to the grid... Is it just me missing something or is it a normal behaviour?
18
4085
by: cf29 | last post by:
Greetings, I designed in JavaScript a small program on my website called 5 queens. (http://www.cf29.com/design/dame5_eng.php) The goal is to control all the chess board with five queens that do not attack each other. I found "manually" many solutions to this problem (184 until now) and wanted to create a Python script to find them all. As I am new to Python, I struggle a lot.
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.