Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using code given write hill climbing solution based on prompt given. In c++ The compiler im using is net beans #include #include #include #include using

Using code given write hill climbing solution based on prompt given. In c++ The compiler im using is net beans

image text in transcribedimage text in transcribedimage text in transcribed

#include

#include

#include

#include

using namespace std;

bool check(char **board,int i,int j,int st,int N){

if(st == 1){

if(board[i][j] == 'K'){

return false;

}

for(int k = 0;k

if(board[i][k] == 'Q'){

return false;

}

}

for(int k = 0;k

if(board[k][j] == 'Q'){

return false;

}

}

for(int k = i-1,s = j-1;k >= 0 && s >= 0;k--,s--){

if(board[k][s] == 'Q'){

return false;

}

}

for(int k=i+1,s=j+1;k

if(board[k][s] == 'Q'){

return false;

}

}

for(int k=i-1,s=j+1;k>=0 && s

if(board[k][s] == 'Q'){

return false;

}

}

for(int k = i+1,s = j-1;k= 0;k++,s--){<>

if(board[k][s] == 'Q'){

return false;

}

}

int k = i;

int s = j;

if( k-2 >= 0){

if(s-1 >= 0){

if(board[k-2][s-1] == 'K'){

return false;

}

}

if(s+1

if(board[k-2][s+1] == 'K'){

return false;

}

}

}

if( k+2

if(s-1 >= 0){

if(board[k+2][s-1] == 'K'){

return false;

}

}

if(s+1

if(board[k+2][s+1] == 'K'){

return false;

}

}

}

if( s-2 >= 0){

if(k-1 >= 0){

if(board[k-1][s-2] == 'K'){

return false;

}

}

if(k+1

if(board[k+1][s-2] == 'K'){

return false;

}

}

}

if( s+2

if(k-1 >= 0){

if(board[k-1][s+2] == 'K'){

return false;

}

}

if(k+1

if(board[k+1][s+2] == 'K'){

return false;

}

}

}

}

if(st == 2){

if(board[i][j] == 'Q'){

return false;

}

int k = i;

int s = j;

if( k-2 >= 0){

if(s-1 >= 0){

if(board[k-2][s-1] == 'K'){

return false;

}

}

if(s+1

if(board[k-2][s+1] == 'K'){

return false;

}

}

}

if( k+2

if(s-1 >= 0){

if(board[k+2][s-1] == 'K'){

return false;

}

}

if(s+1

if(board[k+2][s+1] == 'K'){

return false;

}

}

}

if( s-2 >= 0){

if(k-1 >= 0){

if(board[k-1][s-2] == 'K'){

return false;

}

}

if(k+1

if(board[k+1][s-2] == 'K'){

return false;

}

}

}

if( s+2

if(k-1 >= 0){

if(board[k-1][s+2] == 'K'){

return false;

}

}

if(k+1

if(board[k+1][s+2] == 'K'){

return false;

}

}

}

}

}

void initializeBoard(char **board,int Q,int K,int N){

for(int i = 0;i

for(int j = 0;j

board[i][j] = ' ';

}

}

for(int i = 0;i

for(int j = 0;j

if(Q > 0){

if(check(board,i,j,1,N)){

board[i][j] = 'Q';

Q--;

}

}

if(K > 0){

if(check(board,i,j,2,N)){

board[i][j] = 'K';

K--;

}

}

if(Q == 0 && K == 0){

break;

}

}

}

}

void printBoard(char **Board,int N){

cout

for(int i =0;i

for(int j = 0;j

cout

}

cout

}

}

int main(){

int N;

int Q;

int K;

int tmax;

std::cout > N; cout

if (cin.fail())

{

std::cout

exit(0);

}

else if (N

{

std::cout 4.";

exit(0);

}

std::cout > Q; cout

if (cin.fail())

if (cin.fail())

{

std::cout

exit(0);

}

else if (Q

{

std::cout 1.";

exit(0);

}

std::cout > K; cout

if (cin.fail())

if (cin.fail())

{

std::cout

exit(0);

}

else if (K

{

std::cout 1.";

exit(0);

}

char **board = new char*[N];

for(int i = 0;i

board[i] = new char[N];

}

initializeBoard(board,Q,K,N);

printBoard(board,N);

return 0;

}

1 Solving Queens-and-Knights Problem with Hill Climbing Given an N N board, Q queens, and K knights, the objective is to place the queens and the knights on the board so that no queen attacks another queen no knight attacks another knight no knight attacks a queen Note that it is ok for a queen to attack a knight<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

Write formal proposal requests.

Answered: 1 week ago

Question

Write an effective news release.

Answered: 1 week ago

Question

Identify the different types of proposals.

Answered: 1 week ago