Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!