Question
GIVEN THIS CODE IMPLEMENT HILL CLIMBING SOLUTION FOR THE FOLLOWING PARAMETERS. THE CODE WILL ASK YOU TO SELECT A NUMBER OF QUEENS, KNIGHTS, AND BOARD
GIVEN THIS CODE IMPLEMENT HILL CLIMBING SOLUTION FOR THE FOLLOWING PARAMETERS. THE CODE WILL ASK YOU TO SELECT A NUMBER OF QUEENS, KNIGHTS, AND BOARD SIZE AND THEN PRINT A BOARD.
#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 = 0){ if(board[k+2][s-1] == 'K'){ return false; } } if(s+1 = 0){ if(k-1 >= 0){ if(board[k-1][s-2] == 'K'){ return false; } } if(k+1 = 0){ if(board[k-1][s+2] == 'K'){ return false; } } if(k+1 = 0){ if(s-1 >= 0){ if(board[k-2][s-1] == 'K'){ return false; } } if(s+1 = 0){ if(board[k+2][s-1] == 'K'){ return false; } } if(s+1 = 0){ if(k-1 >= 0){ if(board[k-1][s-2] == 'K'){ return false; } } if(k+1 = 0){ if(board[k-1][s+2] == 'K'){ return false; } } if(k+1 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> N; cout 4."; exit(0); } std::cout > Q; cout 1."; exit(0); } std::cout > K; 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 knightStep 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