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...
please i need the code for an 8 bit by 8 bit multiplier in verilog ....
please i need the code for an 8 bit by 8 bit multiplier in verilog . ( pls the code should be clearly written and BOLD)
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...
I need the full code. No hardcoding. Code a function to convert any 8-bit number from...
I need the full code. No hardcoding. Code a function to convert any 8-bit number from binary to hexadecimal. You are not allowed to use libraries that automate this process. a) Test your function with the following binary numbers. 11110000 and 00100010 The binary number will be provided by the user and you must read it using fgets. b) Draw a flowchart of your code. You can draw it by hand but it must be readable, and respect the flowchart...
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
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT