Question

I am trying to break out of several nested while loops and I am having trouble. I want this program to break out into the outer loop, which will run only a certain amount of times. I tried doing it with a boolean but my program terminates too early. It is an N-Queens problem where I am solving for 1x1, 2x2, 3x3,...nxn queens.

Here is my code:

bool ok(int *q, int col)
{
   for(int i=0; i<col; i++)
      if(q[col]==q[i] || (col-i)==abs(q[col]-q[i])) return false;

return true;
};

void print(int q[], int n, int cnt)
{
    //static int count =0;
    cout<<"There are "<<cnt<<" solutions for "<<n<<" queens." <<endl;   
};

int main()
{
    int n;
    int *q;
    cout<<"Please enter the size of the board:"<<endl;
    cin>>n;

    int static count = 0;

    int c = 1;
    int a = 1;
    bool from_backtrack=false;

    while(a!=n){

        q= new int[a];
        q[0]=0;
        bool foundSolution=true;
        while(foundSolution)
        {
            if (c==a){
                a++;
            }
            while(c<a)
            {
                if(!from_backtrack)
                    q[c] = -1; //Start at the top
                from_backtrack=false;
                while(q[c]<a)
                {
                    q[c]++;
                    if  (q[c]==a)
                    {
                        c--;
                        if(c==-1) {
                            print(q, n, count);
                            foundSolution=false;
                            //system("PAUSE"); exit(1);
                        }
                        continue;
                    }
                    if( ok(q,c) ) break; //get out of the closest while loop
                }
                c++;
            }
            count++;
            c--;
            if(c==-1) {
                print(q, n, count);
                foundSolution=false;
                //system("PAUSE"); exit(1);
            }
            from_backtrack=true;
        }
        delete[a] q;
        a++;
    }
    system("PAUSE");
}
Was it helpful?

Solution

The most elegant way would be to wrap some of your inner loops in a function. It will be easier to read and to control.

OTHER TIPS

At my work, we employ MISRA guidelines which state "... only 1 break per while loop". This has caused me to rewrite my if and while loops:

bool can_continue = true;

if (can_continue)
{
  status = Do_Something();
  if (status != SUCCESS)
  {
    can_continue = false;
  }
}

if (can_continue)
{
  status = Do_Another_Thing();
  can_continue = status == SUCCESS;
}

//.. and so on.

The idea is to set a flag to "false" if execution can't continue. Check it after any segment can cause execution to fail.

while( true ){

    if( condition == true ){
        goto bye;
    }
}

:bye

Just don't submit this on your homework...

Think it is as crazy as useless.

However, suppose you want 3 iterations, you'll define an array of bool of 3 elements (all set to true). On each iteration you set the current element to false, until you reach the end of your array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top