I am having a strange problem. Please help me.
I have a code which tries to solve the queen's problem. It is wrong so please dont bother yourself with the code unless you are interested.
The problem is that when I execute this wrong code, it goes into infinite loop.
But prints only one line.
But when I add another line in the code at one place, it prints the first line and the newer line both.
So I dont understand why is it so.
Can someone please guide:
The line is 15th in this code and I have commented that line out. So please run the program twice once when it is commented and second time when it is uncommented. I am using gcc (GCC) 4.1.1 20061011 (Red Hat 4.1.1-30)
#include <iostream>
using namespace std;
#define order 8
void printall(int arr[]);
bool validity_check(int,int,int []);
int main()
{
int nextjump[order];//Keeps track for every order what should be the pos. of next jump.
for(int i=0;i<order;i++) nextjump[i]=0;
int level=0;
int position[order];
mainloop:
while(true)
{
//cout<<"Entered the while loop"<<endl; //This is the line which can change the whole output when uncommented
for(int i=nextjump[level];i<order;i++)
{
if(validity_check(level,i,position))
{
cout<<"Successful at level "<<level<<" using position "<<i<<endl;
position[level]=i;
nextjump[level++]=i+1;
if(level==order)
{
printall(position);
goto mainloop;
}
nextjump[level]=0;
goto mainloop;
}
}
level-- ;
goto mainloop;
}
return 0;
}
bool validity_check(int level,int n, int current[])
{
for(int i=0;i<level;i++)
{
if(current[i]==n) return false;
}
//Level 0 has always full validity.
if((level>0)&&(level<order-1))
{
if((current[level-1]==n)|(current[level+1]==n)) return false;
}
if(level==order-1)
{
if(current[level-1]==n) return false;
}
}
void printall(int current[])
{
for(int i=0;i<order;i++)
{
cout<<current[i]<<endl;
}
}