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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started