Hi, I've done this depth-firs, but now doing it breadth-first is giving me some trouble. I'm pretty sure I'm queueing up right, etc... But, I think the checkvalid function is off a little. Can somebody give some suggestions? Thanks. Code:
#include <queue>
#include <iostream>
#include <math.h>
using namespace std;
#define N 8
int x = 0;
bool ask = true;
bool checkvalid(int);
typedef struct
{
int a[N + 1];
} array;
array a = {0};
array b = {0};
queue<array> q;
void main()
{
int sizeQ;
int i, num;
for(i = 1, num = 1; num <= N; ++num)
{
a.a = num;
q.push(a);
}
sizeQ = q.size();
cout << "Size of queue is: " << sizeQ << endl;
sizeQ = q.size();
/*
for(int j = 1; j <= N; ++j)
{
for(i = 1; i <= N; ++i)
{
cout << q.front().a;
}
cout << endl;
q.pop();
}
*/
// iterate through queue, pushing
// valid combinations, and poping
// all previous partial solutions
while(q.front().a[8] != N)
{
// loop to check every possible combination
// between 1 and N for the next board
for(i = 1; i <= N; ++i)
{
// if move is valid push the new
// legal combination onto the queue
if(checkvalid(i++))
{
q.push(b);
}
}
q.pop();
}
for(i = 1; i <= sizeQ; ++i)
{
cout << i << ". " << "Q[1..8] = ";
for(int j = 1; j <= N; ++j)
{
cout << q.front().a << " ";
}
cout << endl;
q.pop();
}
}
bool checkvalid(int x)
{
int i;
bool flag = true;
// initalize the new array 'b'
// to be used to push onto the
// queue, when a good placement
// has been found
for(i = 1; i <= N; ++i)
{
b.a = q.front().a;
}
// check for a valid move
for(i = 1; i <= N; ++i)
{
if(b.a[x] == b.a)
{
flag = false;
}
if((x - i) == (abs(b.a[x] - b.a)))
{
flag = false;
}
if(flag)
{
b.a[x] = x;
}
}
return flag;
}
#include <queue>
#include <iostream>
#include <math.h>
using namespace std;
#define N 8
int x = 0;
bool ask = true;
bool checkvalid(int);
typedef struct
{
int a[N + 1];
} array;
array a = {0};
array b = {0};
queue<array> q;
void main()
{
int sizeQ;
int i, num;
for(i = 1, num = 1; num <= N; ++num)
{
a.a = num;
q.push(a);
}
sizeQ = q.size();
cout << "Size of queue is: " << sizeQ << endl;
sizeQ = q.size();
/*
for(int j = 1; j <= N; ++j)
{
for(i = 1; i <= N; ++i)
{
cout << q.front().a;
}
cout << endl;
q.pop();
}
*/
// iterate through queue, pushing
// valid combinations, and poping
// all previous partial solutions
while(q.front().a[8] != N)
{
// loop to check every possible combination
// between 1 and N for the next board
for(i = 1; i <= N; ++i)
{
// if move is valid push the new
// legal combination onto the queue
if(checkvalid(i++))
{
q.push(b);
}
}
q.pop();
}
for(i = 1; i <= sizeQ; ++i)
{
cout << i << ". " << "Q[1..8] = ";
for(int j = 1; j <= N; ++j)
{
cout << q.front().a << " ";
}
cout << endl;
q.pop();
}
}
bool checkvalid(int x)
{
int i;
bool flag = true;
// initalize the new array 'b'
// to be used to push onto the
// queue, when a good placement
// has been found
for(i = 1; i <= N; ++i)
{
b.a = q.front().a;
}
// check for a valid move
for(i = 1; i <= N; ++i)
{
if(b.a[x] == b.a)
{
flag = false;
}
if((x - i) == (abs(b.a[x] - b.a)))
{
flag = false;
}
if(flag)
{
b.a[x] = x;
}
}
return flag;
}