Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Eight cross C++ program: Fix program please, I believe the issue is in the ok function. I left some lines of sudo code that I

Eight cross C++ program: Fix program please, I believe the issue is in the ok function. I left some lines of sudo code that I couldn't figure out.

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.

#include #include #include using namespace std; bool ok(int q[], int c); void backtrack(int &c); void print(int q[]); bool ok(int q[], int c) { static int adj_table[8][5]= { //column size varies {-1}, // box 0 {0,-1}, //box 1 {0,1,-1}, //box 2 {0,2,-1}, //box 3 {1,2,-1}, // box 4 {1,2,3,4,-1}, //box 5 {2,3,5,-1}, //box 6 {4,5,6,-1} //box 7 }; for(int i=0; i

int main(){ int q[8]; q[0]=0; /*board setup section*/ int c=0; bool from_backtrack=false; while(true){ while(c<8){ if(!from_backtrack) { /*column section*/ c++; /*increase column number */ if(c==8) break; /*check column number, break if c == 8*/ q[c]=1; /*initialize row number */ } from_backtrack=false; while(q[c]<8){ /*row section*/ q[c]++; /*increase row number */ if(q[c]==8) { backtrack(c); /*check row number, call backtrack*/ from_backtrack = true; } if(ok(q,c)) break; /*call ok function, break if true */ } } print(q) ; /*print section*/ backtrack(c); /*backtrack section*/ from_backtrack=true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

=+ 5. Do Europeans work more or fewer hours than Americans?

Answered: 1 week ago

Question

1. What is the meaning and definition of banks ?

Answered: 1 week ago

Question

2. What is the meaning and definition of Banking?

Answered: 1 week ago

Question

3.What are the Importance / Role of Bank in Business?

Answered: 1 week ago