473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Eight queens using stacks instead of recursion


Hey all, I been reading through these forums for a long time but have
never posted. Well, I got a dillema. I have a program of the Eight
Queen's program and I have to make it work without recursion. I have to
use a stack instead. It's been buggin me for a week and I have no clue
how to start it. Do you guy think you can help me out?

Here is the recursive part of the program:

size_t continue_placement(Board &board, size_t num_queens, long
int &steps)

// Assumes that bd contains num_queens queens, and is shadowed

// accordingly.

// Returns the number of queens it could place.

// A -1 in a board position q will show that there is a queen on q.

{

// Entry 1: start

size_t n = board.size;

if (n == num_queens) return num_queens;

// Display the board after every 10000 recursive calls.

steps++;

if (0 == steps % 10000) {

cerr << steps << endl;

board.display(cerr);

}

size_t max_num_queens = num_queens;

Board::Position q(n); // = (0, 0)

// Entry 2: start the loop with a loop test.

while(q.is_legal()){

if (0 == board.get(q)) {

// add new queen

board.set(q, -1);

board.queen_shadow(q, 1);

size_t new_max =
continue_placement(board,num_queens+1, steps);

// Entry 3: return from recursive call

if (new_max > max_num_queens)

max_num_queens = new_max;

if (n == max_num_queens)

return max_num_queens;

else{

// remove the new queen

board.queen_shadow(q, -1);

board.set(q, 0);

}

}

// Entry 4: we jumped here if 0 != board.get(q).

q = q.next();

}

// Entry 5: get here if the loop test fails.

return max_num_queens;

}
--
Posted via http://dbforums.com
Jul 19 '05 #1
3 6839
"cfanatic" <me*********@dbforums.com> wrote...

Hey all, I been reading through these forums for a long time but have
never posted. Well, I got a dillema. I have a program of the Eight
Queen's program and I have to make it work without recursion. I have to
use a stack instead. It's been buggin me for a week and I have no clue
how to start it. Do you guy think you can help me out?


Instead of calling your function recursively, push the state of the
data onto the stack and do another iteration. On every iteration
use the top of the stack as your current state. When the iteration
is complete, pop the stack. Do so until done.

BTW, this is not a C++ language issue.

Victor

Jul 19 '05 #2

"cfanatic" <me*********@dbforums.com> wrote in message news:34****************@dbforums.com...

Hey all, I been reading through these forums for a long time but have
never posted. Well, I got a dillema. I have a program of the Eight
Queen's program and I have to make it work without recursion. I have to
use a stack instead. It's been buggin me for a week and I have no clue
how to start it. Do you guy think you can help me out?


Take every variable that you would get a new copy of if you recursed and
put that in a class or struct. Every place you would have called the recursive
function, push that struct on your stack. Pop where you would have returned.
For example:

void recurse(int i, int j) {
--i; ++j;
if(i)
recurse(i, j);
}

recurse(100, 0);

becomes

struct RV {
int i;
int j;
};

vector <RV> rstack;

RV v;
v.i = 100;
v.j = 0;

rstack.push_back(v);
while(!rstack.empty()) {
---v.i;
++v.j;
if(v.i) {
rstack.push_back(v);
continue;
}
v = rstack.back();
rstack.pop_back()
}

Jul 19 '05 #3

Thanks guys for the help. I didn't expect that quick of a response. I
going to give it a shot right now. Also sorry victor, I didn't know but
thanks for helping me anyway.
--
Posted via http://dbforums.com
Jul 19 '05 #4

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

Similar topics

2
by: Andreas | last post by:
Hi! does anybody know an algorithm to solve the 8-queens-problem in PHP?
4
by: Alexis | last post by:
Hi, I need to transform one xml document into a second xml document. They both have many nodes so xslt works fine, but there is one node I have not figure out how to transform. Here it is:...
4
by: chandra.somesh | last post by:
I recently was having trouble converting implementaion of recursions using stack. While single level of recursions are quite intuitive , it is when we have more than one recursive calls in the...
5
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....
9
by: totalgeekdom | last post by:
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. ...
2
by: angel120 | last post by:
Since this is my first post, allow me to introduce myself. My name is Angel, and I'm a 3rd year student at Queens College. I currently am taking CS211, which is the second level of C++, CS212 which...
18
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...
1
by: AZRebelCowgirl73 | last post by:
I am trying to develop an 8 queens program, and currently it is working however it is printing 87222211 which is obviously wrong, I am trying to print the row of the queen in order from...
0
Brad Orders
by: Brad Orders | last post by:
Hi all Here is my situation: When table A is updated, I need to record some data in table B, then apply the update to table A Normally I would use a FOR UPDATE trigger, but the table has a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.