📌  相关文章
📜  检查国王在经过修改的棋盘中有N个夜晚时是否可以进行有效的移动

📅  最后修改于: 2021-06-25 18:44:33             🧑  作者: Mango

给定无限棋盘,其规则与国际象棋相同。还给出了无限棋盘上的N个骑士坐标(-10 ^ 9 <= x,y <= 10 ^ 9 )和国王的坐标,任务是检查国王是否为将军。

例子:

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. 

方法:在棋子中骑士的举动是不寻常的。它移动到一个水平方向相距两个正方形,垂直方向相距一个正方形或垂直方向相交两个正方形,水平相距一个正方形的正方形。因此,完整的移动看起来像字母“ L”,且形状各异(8个可能的移动)。因此,使用成对的哈希图来标记骑士可以移动的所有可能的坐标。如果国王无法移动到附近的8个坐标中的任何一个,即,如果该坐标被骑士的移动散列,则其为“将死”。

下面是上述方法的实现。

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


输出:
Yes its checkmate!

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。