- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a king can move a valid move or not when N nights are there in a modified chessboard in C++
Concept
With respect of given infinite chessboard with the same rules as that of chess and given N knights coordinates on the infinite chessboard (-10^9 <= x, y <= 10^9) and the king’s coordinate, the task is to verify if the King is checkmate or not.
Input
a1[] = { { 2, 1 }, { 1, 3 }, { 3, 6 },{ 5, 5 }, { 6, 1 }, { 7, 3 }} king -> {4, 3}
Output
Yes
The king is unable to make any move as it has been check mate.
Input
a1 [] = {{1, 1}} king -> {3, 4}
Output
No
The king can be able to make valid moves.
Method
Here, the knight’s move is unusual among chess pieces. Its movement is towards a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. So, the complete move looks like the letter “L” in every shape possible (8 possible moves). As a result of this, apply a hash map of pairs to mark all possible coordinates where the knight can move. If it has been seen that the King cannot move to any of its nearby 8 coordinates i.e., if the coordinate is hashed by a knight’s move, then it is declared as a “checkmate”.
Example
// C++ program for verifying if a king // can move a valid move or not when // N nights are there in a modified chessboard #include <bits/stdc++.h> using namespace std; bool checkCheckMate1(pair<int, int>a1[], int n1, int kx1, int ky1){ // Pair of hash to indicate or mark the coordinates map<pair<int, int>, int> mpp1; // iterate for Given N knights for (int i = 0; i < n1; i++) { int x = a1[i].first; int y = a1[i].second; // indicate or mark all the "L" shaped coordinates // that can be reached by a Knight // starting or initial position mpp1[{ x, y }] = 1; // 1-st move mpp1[{ x - 2, y + 1 }] = 1; // 2-nd move mpp1[{ x - 2, y - 1 }] = 1; // 3-rd move mpp1[{ x + 1, y + 2 }] = 1; // 4-th move mpp1[{ x + 1, y - 2 }] = 1; // 5-th move mpp1[{ x - 1, y + 2 }] = 1; // 6-th move mpp1[{ x + 2, y + 1 }] = 1; // 7-th move mpp1[{ x + 2, y - 1 }] = 1; // 8-th move mpp1[{ x - 1, y - 2 }] = 1; } // iterate for all possible 8 coordinates for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { int nx = kx1 + i; int ny = ky1 + j; if (i != 0 && j != 0) { // verify or check a move can be made or not if (!mpp1[{ nx, ny }]) { return true; } } } } // any moves return false; } // Driver Code int main(){ pair<int, int&lgt; a1[] = { { 2, 1 }, { 1, 3 }, { 3, 6 }, { 5, 5 }, { 6, 1 }, { 7, 3 }}; int n1 = sizeof(a1) / sizeof(a1[0]); int x = 4, y = 3; if (checkCheckMate1(a1, n1, x, y)) cout << "Not Checkmate!"; else cout << "Yes its checkmate!"; return 0; }
Output
Yes its checkmate!