# 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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` `bool` `checkCheckMate(pair<``int``, ``int``> a[], ``int` `n, ``int` `kx, ``int` `ky) ` `{ ` ` `  `    ``// Pair of hash to mark the coordinates ` `    ``map, ``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); ` ` `  `    ``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 mpp = ``new` `HashMap(); ` ` `  `    ``// 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 mpp = ``new` `Dictionary(); ` ` `  `    ``// 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 `

Output:

```Yes its checkmate!
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.