Question

In: Computer Science

Need this 8 queen code with same exact solution without goto. Need to keep the code...

Need this 8 queen code with same exact solution without goto.

Need to keep the code as similar as possible.

#include <iostream>
#include <cmath>
using namespace std;
int main(){
int q[8], c = 0, i, j,count=0;
q[0] = 0; //first queen top corner
nc: c++;
if(c == 8 ) goto print;
q[c] = -1;
nr: q[c]++;
if(q[c] == 8) goto backtrack;
for(i= 0; i < c; i++) {
if((q[i] == q[c])||(c - i == abs(q[c] - q[i]))) goto nr;
}
goto nc;
backtrack: c--;

if(c == -1) return 0;
goto nr;
print: cout << ++count <<endl;
for(i = 0; i < 8; i++){
for(j= 0; j < 8; j++){
if(q[i] == j){
cout << 1;
}
else {
cout << 0;
}
}
cout << endl;

}

goto backtrack;
}

Solutions

Expert Solution

#include<iostream>

#include<cmath>

using namespace std;

int q[8],c=0,i,j,count=0;

q[0]=0;

void nc()

{

c++;

}

void nr()

{

q[c]++;

}

void backtrack()

{

c--;

}

void print()

{

cout<<++count<<endl;

}

int main()

{

if(c==8) print();

q[c]=-1;

if(q[c]==8) backtrack();

for(i=0;i<c;i++)

{

if((q[i]==q[c])||(c-i==abs(q[c]-q[i])))

nr();

}

nc();

if(c==-1)

return 0;

nr();

for(i=0;i<8;i++)

{

for(j=0;j<8;j++)

{

if(q[i]==j)

{

cout<<1;

}

else

{

cout<<0;

}

}

cout<<endl;

}

backtrack();

}

Reference :

Explanation :

I have used functions to avoid goto statement. Just used function calling in the place of goto statement .

I didn't change the code , just declared the functions and variables out of the main function .


Related Solutions

Determine whether the equation is exact. If it is exact, find the solution. If it is...
Determine whether the equation is exact. If it is exact, find the solution. If it is not, enter NS. (8x^2−2xy+5)dx+(8y^2−x^2+8)dy=0
Determine whether the equation is exact. If it is exact, FIND THE SOLUTION. If not write...
Determine whether the equation is exact. If it is exact, FIND THE SOLUTION. If not write NOT EXACT. A) (y/x + 12x) + (lnx - 3)y' = 0 x>0 Solve the given initial value problem. B)  (12x2 + y − 1) − (14y − x)y' = 0,    y(1) = 0 y(x) = Determine at least approximately where the solution is valid. (Enter your answer as an inequality for which the solution is valid when true.) The solution is valid as long as:...
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
Find the exact solution for e2x − ex − 110 = 0. If there is no solution, write no solution.
Find the exact solution for e2x − ex − 110 = 0. If there is no solution, write no solution.
NOTE THERE IS A CHANGE IN THE REQUIREMENTS FOR THIS QUESTION!! IT'S NOT THE EXACT SAME...
NOTE THERE IS A CHANGE IN THE REQUIREMENTS FOR THIS QUESTION!! IT'S NOT THE EXACT SAME AS OTHER!!! CHANGE IN BOOK VALUE, AND NO LIQUIDATION EXPENSE! The Nice, Rice, and Dice Partnership has not been successful. The partners have determined they must liquidate their partnership. The partners have agreed to liquidate the partnership. Prior to the liquidation, the partnership balance sheet reflects the following book values: Cash $18,000 Noncash assets 51,000 Note receivable-Nice 3,000 Other liabilities 20,000 Capital, Nice 6,000...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
Analyst B , following the exact same procedure, carries out an analysis of the same acid...
Analyst B , following the exact same procedure, carries out an analysis of the same acid and reports a concentration of 6.772 ± 0.004 M, after performing 6 successful titrations (no data was rejected). Questions:Is this result obtained by Analyst B in agreement with that of Analyst A, at a 90% confidence level? The certified analysis record for the concentrated sulfuric acid is found and states that the acid has a concentration of 6.759 M. Do either of the Analysts...
Seeing multiple answers to the same question... Please give detailed solution with right answer without excel....
Seeing multiple answers to the same question... Please give detailed solution with right answer without excel. An explanation would be great. Daryl wishes to save money to provide for his retirement. He is now 30 years old and will be retiring at age 64. Beginning one month from now, he will begin depositing a fixed amount into a retirement savings account that will earn 12% compounded monthly. Then one year after making his final deposit, he will withdraw $100,000 annually...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT