Check if a king can move a valid move or not when N nights are there in a modified chessboard
Given an infinite chessboard with the same rules as that of chess. Also given are N knights coordinates on the infinite chessboard(-10^9 <= x, y <= 10^9) and the king’s coordinate, the task is to check if the King is checkmate or not.
Examples:
Input: a[] = { {1, 0}, {0, 2}, {2, 5}, {4, 4}, {5, 0}, {6, 2} } king -> {3, 2}
Output: Yes
The king cannot make any move as it has been check mate.
Input: a[] = { {1, 1} } king -> {3, 4}
Output: No
The king can make valid moves.
Approach: The knight’s move is unusual among chess pieces. It moves to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. The complete move, therefore, looks like the letter “L” in every shape possible(8 possible moves). Hence, use a hash map of pairs to mark all possible coordinates where the knight can move. If the King cannot move to any of its nearby 8 coordinates i.e., if the coordinate is hashed by a knight’s move, then its a “checkmate”.
Below is the implementation of the above approach.
C++
// C++ program for checking 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 checkCheckMate(pair<int, int> a[], int n, int kx, int ky) { // Pair of hash to mark the coordinates map<pair<int, int>, int> mpp; // iterate for Given N knights for (int i = 0; i < n; i++) { int x = a[i].first; int y = a[i].second; // mark all the "L" shaped coordinates // that can be reached by a Knight // initial position mpp[{ x, y }] = 1; // 1-st move mpp[{ x - 2, y + 1 }] = 1; // 2-nd move mpp[{ x - 2, y - 1 }] = 1; // 3-rd move mpp[{ x + 1, y + 2 }] = 1; // 4-th move mpp[{ x + 1, y - 2 }] = 1; // 5-th move mpp[{ x - 1, y + 2 }] = 1; // 6-th move mpp[{ x + 2, y + 1 }] = 1; // 7-th move mpp[{ x + 2, y - 1 }] = 1; // 8-th move mpp[{ 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 = kx + i; int ny = ky + j; if (i != 0 && j != 0) { // check a move can be made or not if (!mpp[{ nx, ny }]) { return true; } } } } // any moves return false; } // Driver Code int main() { pair<int, int> a[] = { { 1, 0 }, { 0, 2 }, { 2, 5 }, { 4, 4 }, { 5, 0 }, { 6, 2 }}; int n = sizeof(a) / sizeof(a[0]); int x = 3, y = 2; if (checkCheckMate(a, n, x, y)) cout << "Not Checkmate!"; else cout << "Yes its checkmate!"; return 0; } |
Java
// Java program for checking if a king // can move a valid move or not when // N nights are there in a modified chessboard import java.util.*; class GFG { static class pair { int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } static boolean checkCheckMate(pair a[], int n, int kx, int ky) { // Pair of hash to mark the coordinates HashMap<pair, Integer> mpp = new HashMap<pair, Integer>(); // iterate for Given N knights for (int i = 0; i < n; i++) { int x = a[i].first; int y = a[i].second; // mark all the "L" shaped coordinates // that can be reached by a Knight // initial position mpp.put(new pair( x, y ), 1); // 1-st move mpp.put(new pair( x - 2, y + 1 ), 1); // 2-nd move mpp.put(new pair( x - 2, y - 1 ), 1); // 3-rd move mpp.put(new pair( x + 1, y + 2 ), 1); // 4-th move mpp.put(new pair( x + 1, y - 2 ), 1); // 5-th move mpp.put(new pair( x - 1, y + 2 ), 1); // 6-th move mpp.put(new pair( x + 2, y + 1 ), 1); // 7-th move mpp.put(new pair( x + 2, y - 1 ), 1); // 8-th move mpp.put(new pair( 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 = kx + i; int ny = ky + j; if (i != 0 && j != 0) { // check a move can be made or not pair p =new pair(nx, ny ); if (mpp.get(p) != null) { return true; } } } } // any moves return false; } // Driver Code public static void main(String[] args) { pair a[] = {new pair( 1, 0 ), new pair( 0, 2 ), new pair( 2, 5 ), new pair( 4, 4 ), new pair( 5, 0 ), new pair( 6, 2 )}; int n = a.length; int x = 3, y = 2; if (checkCheckMate(a, n, x, y)) System.out.println("Not Checkmate!"); else System.out.println("Yes its checkmate!"); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 program for checking if a king # can move a valid move or not when # N nights are there in a modified chessboard def checkCheckMate(a, n, kx, ky): # Pair of hash to mark the coordinates mpp = {} # iterate for Given N knights for i in range(0, n): x = a[i][0] y = a[i][1] # mark all the "L" shaped coordinates # that can be reached by a Knight # initial position mpp[(x, y)] = 1 # 1-st move mpp[(x - 2, y + 1)] = 1 # 2-nd move mpp[(x - 2, y - 1)] = 1 # 3-rd move mpp[(x + 1, y + 2)] = 1 # 4-th move mpp[(x + 1, y - 2)] = 1 # 5-th move mpp[(x - 1, y + 2)] = 1 # 6-th move mpp[(x + 2, y + 1)] = 1 # 7-th move mpp[(x + 2, y - 1)] = 1 # 8-th move mpp[(x - 1, y - 2)] = 1 # iterate for all possible 8 coordinates for i in range(-1, 2): for j in range(-1, 2): nx = kx + i ny = ky + j if i != 0 and j != 0: # check a move can be made or not if not mpp[(nx, ny)]: return True # any moves return False # Driver Code if __name__ == "__main__": a = [[1, 0], [0, 2], [2, 5], [4, 4], [5, 0], [6, 2]] n = len(a) x, y = 3, 2 if checkCheckMate(a, n, x, y): print("Not Checkmate!") else: print("Yes its checkmate!") # This code is contributed by Rituraj Jain |
C#
// C# program for checking if a king // can move a valid move or not when // N nights are there in a modified chessboard using System; using System.Collections.Generic; class GFG { class pair { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } static bool checkCheckMate(pair []a, int n, int kx, int ky) { // Pair of hash to mark the coordinates Dictionary<pair, int> mpp = new Dictionary<pair, int>(); // iterate for Given N knights for (int i = 0; i < n; i++) { int x = a[i].first; int y = a[i].second; // mark all the "L" shaped coordinates // that can be reached by a Knight // initial position mpp.Add(new pair( x, y ), 1); // 1-st move mpp.Add(new pair( x - 2, y + 1 ), 1); // 2-nd move mpp.Add(new pair( x - 2, y - 1 ), 1); // 3-rd move mpp.Add(new pair( x + 1, y + 2 ), 1); // 4-th move mpp.Add(new pair( x + 1, y - 2 ), 1); // 5-th move mpp.Add(new pair( x - 1, y + 2 ), 1); // 6-th move mpp.Add(new pair( x + 2, y + 1 ), 1); // 7-th move mpp.Add(new pair( x + 2, y - 1 ), 1); // 8-th move mpp.Add(new pair( 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 = kx + i; int ny = ky + j; if (i != 0 && j != 0) { // check a move can be made or not pair p = new pair(nx, ny); if (mpp.ContainsKey(p)) { return true; } } } } // any moves return false; } // Driver Code public static void Main(String[] args) { pair []a = {new pair( 1, 0 ), new pair( 0, 2 ), new pair( 2, 5 ), new pair( 4, 4 ), new pair( 5, 0 ), new pair( 6, 2 )}; int n = a.Length; int x = 3, y = 2; if (checkCheckMate(a, n, x, y)) Console.WriteLine("Not Checkmate!"); else Console.WriteLine("Yes its checkmate!"); } } // This code is contributed by PrinciRaj1992 |
Yes its checkmate!
Recommended Posts:
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Check if the given chessboard is valid or not
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Total position where king can reach on a chessboard in exactly M moves
- Check if possible to move from given coordinate to desired coordinate
- Number of cells a queen can move with obstacles on the chessborad
- Count the total number of squares that can be visited by Bishop in one move
- Minimum adjacent swaps to move maximum and minimum to corners
- Move matrix elements in given direction and add elements with same value
- Check if a Queen can attack a given cell on chessboard
- Check if the given permutation is a valid DFS of graph
- Check if given Sudoku board configuration is valid or not
- Minimum number of moves required to reach the destination by the king in a chess board
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

