Answered step by step
Verified Expert Solution
Question
1 Approved Answer
*********This is the reference code for the problem********** /** This assignment is a really tricky one. This assignment is call the Eight Queens Problem, because
*********This is the reference code for the problem**********
/** This assignment is a really tricky one. This assignment is call the Eight Queens Problem, because we have to place eight queens in the chess board in a way such that no two of the queens cross each others path. */ /** So, this is an assigment where we need to use 2 dimensional arrays in order to find the required algorithmic solution to the problem. We need to use goto statements, for loops, while loops, if statements and backtracking. Backtracing play a huge roll in this problem. It easily goes back and starts from the very beginning and checks whether we can place a queen or not. This problem will give at most 92 solutions using the backtracking method. */ #include#include using namespace std; int main () { int b[8][8] = {0}; int r; int c = 0; int ans = 1; b[0][0] = 1; next_col: c++; if ( c==8) goto print; r = -1; next_row: r++; if (r==8) goto backtrack; for (int i =0; i = 0; t++){ if (b[r+t][c-t] == 1) { goto next_row; } } for(int g=1; (r-g) >=0 && (c-g) >= 0 ; g++){ if(b[r-g][c-g] == 1){ goto next_row; } } b[r][c] = 1; goto next_col; backtrack: --c; if (c==-1) return 0; r=0; while (b[r][c] !=1 ) r++; b[r][c] = 0; goto next_row; print: cout << "Result " << ans << ":" << endl; for(int i =0; i<8; i++){ for (int j=0; j <8; j++){ cout << b[i][j] << " "; } cout << endl; } cout<< endl; ans ++; goto backtrack; }
Problem 1. Write a C++ program the 8 queens 1 dimensional array program with backtracking...( Please try to explain the solution with comments)
Problem 2. Write a C++ program the 8 queens 1 dimensional array program with backtracking REMOVING ALL "GOTOs" - but implementing the same algorithm.( Please try to explain the solution with comments)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started