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

Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or...
Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or any other unconditional branching statement: k = (j+13)/27; //assume i,j,k are integers properly declared. loop: if k > 10 then goto out k=k+1.2; i=3*k-1; goto loop; out: …
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; }...
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!!
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...
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test...
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test score         String input;     //To hold the user's input                 // Get the numeric test score.         input = JOptionPane.showInputDialog("Enter your numeric test score and I will tell you the grade: ");         testScore = Integer.parseInt(input);                 //Display the grade.         if (testScore < 60)            JOptionPane.showMessageDialog(null, "Your score of "+testScore+" is an F.");         else if (testScore < 70)            JOptionPane.showMessageDialog(null,...
Describe about the Spread Footings foundation? Need explanation and step by step solution and without plagrism
Describe about the Spread Footings foundation? Need explanation and step by step solution and without plagrism
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s)...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list. 5. Clear...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT