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;
}