📌  相关文章
📜  计算将在 N x N 棋盘上互相攻击的主教对

📅  最后修改于: 2022-05-13 01:57:23.581000             🧑  作者: Mango

计算将在 N x N 棋盘上互相攻击的主教对

给定N x N棋盘和X象棋在其上的位置,任务是计算象棋对的数量,使它们相互攻击。请注意,在考虑一对主教时,所有其他主教都不被视为董事会成员。

例子:

方法:给定的问题可以使用基本的组合学来解决。由于位于同一对角线上的所有主教都会互相攻击,因此所有可能在同一对角线上的可组成的主教对都会互相攻击。因此,在左右方向上遍历所有对角线的矩阵,并在变量cnt中计算当前对角线上的主教数。现在对于每个对角线,攻击主教的数量将是cnt C 2

下面是上述方法的实现:

C++
#include 
using namespace std;
 
int board[20][20];
 
int countPairs(int N, vector > bishops)
{
    // Set all bits to 0
    memset(board, 0, sizeof(board));
 
    // Set all the bits
    // having bishop to 1
    for (int i = 0; i < bishops.size(); i++) {
        board[bishops[i].first][bishops[i].second] = 1;
    }
 
    // Stores the final answer
    int ans = 0;
 
    // Loop to traverse the matrix
    // diagonally in left direction
    for (int s = 2; s <= 2 * N; s++) {
 
        // Stores count of bishop
        // in the current diagonal
        int cnt = 0;
 
        for (int i = 1, j = s - i;
             max(i, j) <= min(N, s - 1); i++, j--) {
            if (board[j][i - j] == 1)
 
                // Update cnt
                cnt++;
        }
 
        // Update the answer
        if (cnt > 1)
            ans += ((cnt + 1) * cnt) / 2;
    }
 
    // Loop to traverse the matrix
    // diagonally in right direction
    for (int s = 2; s <= 2 * N; s++) {
 
        // Stores count of bishop
        // in the current diagonal
        int cnt = 0;
 
        for (int i = 1, j = s - i;
             max(i, j) <= min(N, s - 1); i++, j--) {
            if (board[i][N - j + 1] == 1)
 
                // Update cnt
                cnt++;
        }
 
        // Update the answer
        if (cnt > 1)
            ans += ((cnt + 1) * cnt) / 2;
    }
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int N = 10;
    vector > bishops = {
        { 1, 1 }, { 1, 5 }, { 3, 3 }, { 5, 1 }, { 5, 5 }
    };
 
    cout << countPairs(N, bishops);
 
    return 0;
}


Java
import java.util.*;
class GFG
{
  static int [][]board = new int[20][20];
 
  static int countPairs(int N, int[][] bishops)
  {
 
    // Set all the bits
    // having bishop to 1
    for (int i = 0; i < bishops.length; i++) {
      board[bishops[i][0]][bishops[i][1]] = 1;
    }
 
    // Stores the final answer
    int ans = 0;
 
    // Loop to traverse the matrix
    // diagonally in left direction
    for (int s = 2; s <= 2 * N; s++) {
 
      // Stores count of bishop
      // in the current diagonal
      int cnt = 0;
 
      for (int i = 1, j = s - i;
           Math.max(i, j) <= Math.min(N, s - 1)&&i-j>0; i++, j--) {
        if (board[j][i - j] == 1)
 
          // Update cnt
          cnt++;
      }
 
      // Update the answer
      if (cnt > 1)
        ans += ((cnt + 1) * cnt) / 2;
    }
 
    // Loop to traverse the matrix
    // diagonally in right direction
    for (int s = 2; s <= 2 * N; s++) {
 
      // Stores count of bishop
      // in the current diagonal
      int cnt = 0;
 
      for (int i = 1, j = s - i;
           Math.max(i, j) <= Math.min(N, s - 1); i++, j--) {
        if (board[i][N - j + 1] == 1)
 
          // Update cnt
          cnt++;
      }
 
      // Update the answer
      if (cnt > 1)
        ans += ((cnt + 1) * cnt) / 2;
    }
 
    // Return answer
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 10;
    int[][] bishops = {
      { 1, 1 }, { 1, 5 }, { 3, 3 }, { 5, 1 }, { 5, 5 }
    };
 
    System.out.print(countPairs(N, bishops));
 
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for above approach
board = [[0 for i in range(20)] for j in range(20)]
 
def countPairs(N, bishops):
 
    # Set all the bits
    # having bishop to 1
    for i in range(len(bishops)):
        board[bishops[i][0]][bishops[i][1]] = 1
 
    # Stores the final answer
    ans = 0
 
    # Loop to traverse the matrix
    # diagonally in left direction
    for s in range(2, (2 * N) + 1):
 
        # Stores count of bishop
        # in the current diagonal
        cnt = 0
 
        i = 1
        j = s - i
        while(max(i, j) <= min(N, s - 1)):
            if (board[j][i - j] == 1):
                # Update cnt
                cnt += 1
            i += 1
            j -= 1
 
        # Update the answer
        if (cnt > 1):
            ans += ((cnt + 1) * cnt) // 2
 
    # Loop to traverse the matrix
    # diagonally in right direction
    for s in range(2, (2 * N) + 1):
 
        # Stores count of bishop
        # in the current diagonal
        cnt = 0
 
        i = 1
        j = s - i
        while(max(i, j) <= min(N, s - 1)):
            
            if (board[i][N - j + 1] == 1):
                # Update cnt
                cnt += 1
            i += 1
            j -= 1
 
        # Update the answer
        if (cnt > 1):
            ans += ((cnt + 1) * cnt) // 2
 
    # Return answer
    return ans
 
# Driver code
N = 10
bishops = [[1, 1], [1, 5], [3, 3], [5, 1], [5, 5]]
print(countPairs(N, bishops))
 
# This code is contributed by gfgking


C#
using System;
class GFG
{
    static int[,] board = new int[20,20];
    static int countPairs(int N, int[,] bishops)
    {
 
        // Set all the bits
        // having bishop to 1
        for (int i = 0; i < bishops.GetLength(0); i++)
        {
            board[bishops[i,0], bishops[i,1]] = 1;
        }
 
        // Stores the final answer
        int ans = 0;
 
        // Loop to traverse the matrix
        // diagonally in left direction
        for (int s = 2; s <= 2 * N; s++)
        {
 
            // Stores count of bishop
            // in the current diagonal
            int cnt = 0;
 
            for (int i = 1, j = s - i;
                 Math.Max(i, j) <= Math.Min(N, s - 1) && i - j > 0; i++, j--)
            {
                if (board[j,i - j] == 1)
 
                    // Update cnt
                    cnt++;
            }
 
            // Update the answer
            if (cnt > 1)
                ans += ((cnt + 1) * cnt) / 2;
        }
 
        // Loop to traverse the matrix
        // diagonally in right direction
        for (int s = 2; s <= 2 * N; s++)
        {
 
            // Stores count of bishop
            // in the current diagonal
            int cnt = 0;
 
            for (int i = 1, j = s - i;
                 Math.Max(i, j) <= Math.Min(N, s - 1); i++, j--)
            {
                if (board[i,N - j + 1] == 1)
 
                    // Update cnt
                    cnt++;
            }
 
            // Update the answer
            if (cnt > 1)
                ans += ((cnt + 1) * cnt) / 2;
        }
 
        // Return answer
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 10;
        int[,] bishops = new int[5, 2]{{ 1, 1 }, { 1, 5 }, { 3, 3 }, { 5, 1 }, { 5, 5 }};
 
        Console.Write(countPairs(N, bishops));
 
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript


输出
6

时间复杂度: O(N * N)
辅助空间: O(N * N)