Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

given code help write solution in hill climbing, c++. The prompt and output example of the code area shown below #include #include #include #include using

given code help write solution in hill climbing, c++. The prompt and output example of the code area shown below

#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;

}

}

}

}

return true;

}

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;

}image text in transcribedimage text in transcribed

The program should use hill climbing to solve the problem. Specifically, s <>

<>

<>

<>

<>

<>

<>

<>

<>

<>

<>

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

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions