I am reposting this thread because the last time I posted it no one understood it and some people with a desire to help me posted solutions that didnt solve my problem. This actually caused the post count fo the thread to reach 5 which caused other people not to reply thinking that the question might have been answered
Kindly dont reply if you dont understand my question
Let me state this problem
I dont want to know the bugs in my problem.
Please dont tell me about them.
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 which is "Successful at level 0 with position 0"
But the interesting thing is that when I add another line which does noting but only print a statement "Entered the while loop", what happens is that the "successful at level ......." line also appears infinite times.
The line is 15th in this code and I have commented that line out.
Can someone please guide why it is 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;
}
}