tags:

views:

183

answers:

2

I have been working on multiple 8 queens problems in my C++ class. I got the bazic 8 queens problem. Now I am having an issue printing out the solutions using the goto and backtrack statements.

// Queens2.cpp.cpp : Defines the entry point for the console application.
// 8 Queens problem using goto and backtrack.

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
    int b[8][8] = {0};
    int r, c, i;
    b[0][0] = 1;
    c =0;

NC: c++;
    if (c == 8) goto print;

    r = -1;
NR: r++;
    if (r == 8) goto backtrack;

    // Row Test
    for(i = 0; i < c; i++)
        if(b[r][i] == 1) goto NR;
    // Up Diagonal Test
    for(i = 1; (r-i) >= 0 && (c-i) >= 0; i++)
        if(b[r-i][c-i] == 1) goto NR;
    // Down Diagonal Test
    for(i = 1; (r+i) <= 7 && (c-i) >= 0; i++)
        if(b[r+i][c-i] == 1) goto NR;

    b[r][c] = 1;
    goto NC;

backtrack:
    c--;
    if(c == -1) return 0;

    r = 0;
    while(b[r][c] != 1){
        r++;
    }

    b[r][c] = 0;
    goto NR;

print:
    for(i = 0; i <= 7; i++){
        for(int j = 0; j <= 7; j++){
            cout << b[i][j];
        }
        cout << endl;
    }

    return 0;
}
+1  A: 

The goto statement performs a one-way jump to (it "goes to") another line of code; here, the destination is specified by a label. The labels in the code you posted are groups of letters followed by a colon (:), so in this particular case, the labels are:

  • NC
  • NR
  • backtrack
  • print

Note that backtrack isn't a statement or a keyword, it's just a label.

Under normal circumstances, code will execute line-by-line, starting from the top of the program, moving downward. However, when a goto statement is executed, the next line executed is not the line below the goto statement, but the line below the label that the goto specifies.

For example, the code

NC: c++;
    if (c == 8) goto print;

    r = -1;

does the following when c is not equal to 8:

  1. Increment c
  2. Set r equal to -1

However, when c is equal to 8, this happens:

  1. Increment c
  2. Jump to the print label, causing these lines (immediately below the label) to be executed:

for(i = 0; i <= 7; i++){
    for(int j = 0; j <= 7; j++){
        cout << b[i][j];
    }
    cout << endl;
}

return 0;

Hopefully, that clarifies the goto statement for you.

Matt Ball
Now the professor wants us to rewrite the code removing the goto statements
Mister Bunker
Then perhaps he's not as bad of a professor as we made him out to be. Maybe he's trying to teach you a valuable lesson in maintaining rotten code!!
glowcoder
+5  A: 

Your code as given correctly finds one solution and prints it out. It sounds like your assignment is to make the code print all 92 solutions.

When a solution is found and printed (at the print label), what happens next in the given code? Answer: The return at the end of main() is hit, and the whole program exits.

I was able to modify the given code to print all 92 solutions by adding just one line of new code. Hint: Think about what you might want to do next after printing one solution.

Greg Hewgill
@Mister Bunker: I know you accepted Matt's answer, and I won't deny it is very informative about how goto works, and how it pertains to your code, but Greg's response illustrates how to solve your problem. And while I haven't tested it myself, I am 99% sure I know what the line Greg added, and his hint is not only good for this assignment, but the entirety of writing software as a whole!
glowcoder
@glowcoder: the only reason I posted that answer about gotos was the OP's original comment on the question: "Having major issues understanding the concept of the goto and backtracking statement. Need clarification." I really don't care which answer gets accepted. Greg's is definitely a good one, though useful in a totally different way from mine. (+1 to your comment)
Matt Ball