Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the time complexity for the following code? #include using namespace std; int sol[5]; // global solution stack. ignore cell 0 void printsolution(void) {

What is the time complexity for the following code?

#include

using namespace std;

int sol[5]; // global solution stack. ignore cell 0

void printsolution(void)

{

for (int i = 1; i < 5; i++)

printf("%d ,", sol[i]);

printf(" ");

}

bool cellok(int n)

{

int i;

// check for queens on other rows

for (i = 1; i < n; i++)

if (sol[i] == sol[n])

return false;

// check for queens on diagonals

for (i = 1; i < n; i++)

if ((sol[i] == (sol[n] - (n - i))) || (sol[i] == (sol[n] + (n - i))))

return false;

return true;

}

void build(int n)

{

int p = 1;

// loop while there are more possible moves

while (p <= 4) {

// Store this move

sol[n] = p;

// Check if cell is okay

if (cellok(n))

// is this the last column?

if (n == 4)

printsolution();

else

build(n + 1); // get the next move

p++;

}

}

void main(void)

{

build(1)

}

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

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago