Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**You can use the following link to fix/write the correct code for this problem*

ID: 3595756 • Letter: #

Question

**You can use the following link to fix/write the correct code for this problem**

Link: cpp.sh/8zt6

The output should look something like this but with different number:

Eight Numbers in a Cross In the solution to this problem, use the backtracking scheme that we covered in class Write a program which allocates the integers 1-8 to the squares in the figure above, subject to the restrictions that no two adjacent squares contain consecutive integers By adjacent we mean vertically, horizontally, or diagonally

Explanation / Answer

Here you go, the below program is verified on the cpp.sh/8zt6 you posted -

---------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <cmath>
using namespace std;

void backtrack(int &c){
c--;
if (c==-1) {
exit(1);
}
}//end of backtrack

void print(int a[], int &count) {
cout<<count<<endl;
for(int k=0;k<8;k++){
cout<<a[k]<<" ";
}
cout<<endl<<endl;
  
}//end of print

bool ok(int a[8], int b, int c) {
for(int i=0; i<b; i++) {
if (a[i]==a[b]||(abs(a[b]-a[c]) == 1)) return false;
}
return true;
}


int main() {
int ans[8]={0}, c=0, i=0, count=0;
int a[8][5] = {
{-1,-1,-1,-1,-1},
{ 0,-1,-1,-1,-1},
{ 0, 1,-1,-1,-1},
{ 0, 2,-1,-1,-1},
{ 1, 2,-1,-1,-1},
{ 1, 2, 3, 4,-1},
{ 2, 3, 5,-1,-1},
{ 4, 5, 6,-1,-1} };
  
while(true){
while(c<8) {
ans[c]++;
if (ans[c]>8) {
ans[c]=0;
backtrack(c);
continue;
}
for(i=0; i<5; i++) {
if(a[c][i]==-1){
c++;
if(c==8) {
print(ans,++count);
backtrack(c);
}
break;
}
if(ok(ans,c,a[c][i])==false) break;
}
  
}
}
  
  
}