📜  计算矩阵中两条边平行于矩阵边的直角三角形

📅  最后修改于: 2021-09-06 06:25:25             🧑  作者: Mango

给定一个维度为N * M的二进制矩阵arr[][] ,任务是计算通过连接包含值1的单元格可以形成的直角三角形的数量,这样三角形必须有任意两条边平行于长方形的边。

例子:

方法:这个想法是遍历给定的网格并将每行和每列中存在的1的计数分别存储在辅助数组row[]col[] 中。然后对于网格中的每个单元格arr[][] ,如果arr[i][j]1 ,那么形成的直角三角形的总数可以通过(row[i] – 1)*(col[ j] – 1)对于每个单元格。请按照以下步骤解决问题:

  • 0初始化数组col[]row[]
  • 遍历给定的网格并访问每个单元格arr[i][j]
  • 如果arr[i][j]1 ,则将row[i]col[j]增加1
  • 遍历网格后,用0初始化一个变量ans
  • 再次遍历整个网格,现在,如果arr[i][j]1,则通过以下方式更新直角三角形的计数:
  • 遍历后,打印ans的值作为直角三角形的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the right-angled
// traingle in the given grid a[][]
int numberOfTriangle(
    vector >& a)
{
    int N = a.size();
    int M = a[0].size();
 
    // Stores the count of 1s for
    // each row[] and column[]
    int rows[N] = { 0 };
    int columns[M] = { 0 };
 
    // Find the number of 1s in
    // each of the rows[0, N - 1]
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
 
            // Increment row[i]
            if (a[i][j] == 1) {
                rows[i]++;
            }
        }
    }
 
    // Find the number of 1s in
    // each of the columns[0, N - 1]
    for (int i = 0; i < M; ++i) {
 
        for (int j = 0; j < N; ++j) {
 
            // Increment columns[i]
            if (a[j][i] == 1) {
                columns[i]++;
            }
        }
    }
 
    // Stores the count of triangles
    int answer = 0;
 
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
 
            // If current cell has value 1
            if (a[i][j] == 1) {
 
                // Update the answer
                answer += (rows[i] - 1)
                          * (columns[j] - 1);
            }
        }
 
        
    }
   // Return the count
        return answer;
}
 
 
// Driver Code
int main()
{
    // Given grid arr[][]
    vector > arr;
 
    arr = { { 1, 0, 1, 0 },
            { 0, 1, 1, 1 },
            { 1, 0, 1, 0 },
            { 0, 1, 0, 1 } };
 
    // Function Call
    cout << numberOfTriangle(arr);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to count the right-angled
// traingle in the given grid a[][]
static int numberOfTriangle(int[][] a)
{
  int N = a.length;
  int M = a[0].length;
 
  // Stores the count of 1s for
  // each row[] and column[]
  int []rows = new int[N];
  int []columns = new int[M];
 
  // Find the number of 1s in
  // each of the rows[0, N - 1]
  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < M; ++j)
    {
      // Increment row[i]
      if (a[i][j] == 1)
      {
        rows[i]++;
      }
    }
  }
 
  // Find the number of 1s in
  // each of the columns[0, N - 1]
  for (int i = 0; i < M; ++i)
  {
    for (int j = 0; j < N; ++j)
    {
      // Increment columns[i]
      if (a[j][i] == 1)
      {
        columns[i]++;
      }
    }
  }
 
  // Stores the count
  // of triangles
  int answer = 0;
 
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < M; ++j)
    {
      // If current cell
      // has value 1
      if (a[i][j] == 1)
      {
        // Update the answer
        answer += (rows[i] - 1) *
                  (columns[j] - 1);
      }
    }
  }
 
  // Return the count
  return answer;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given grid arr[][]
  int [][]arr = {{1, 0, 1, 0},
                 {0, 1, 1, 1},
                 {1, 0, 1, 0},
                 {0, 1, 0, 1}};
 
  // Function Call
  System.out.print(numberOfTriangle(arr));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to count the right-angled
# traingle in the given grid a[][]
def numberOfTriangle(a):
     
    N = len(a)
    M = len(a[0])
 
    # Stores the count of 1s for
    # each row[] and column[]
    rows = [0] * N
    columns = [0] * M
 
    # Find the number of 1s in
    # each of the rows[0, N - 1]
    for i in range(N):
        for j in range(M):
 
            # Increment row[i]
            if (a[i][j] == 1):
                rows[i] += 1
 
    # Find the number of 1s in
    # each of the columns[0, N - 1]
    for i in range(M):
        for j in range(N):
 
            # Increment columns[i]
            if (a[j][i] == 1):
                columns[i] += 1
 
    # Stores the count of triangles
    answer = 0
 
    for i in range(N):
        for j in range(M):
 
            # If current cell has value 1
            if (a[i][j] == 1):
 
                # Update the answer
                answer += ((rows[i] - 1) *
                      (columns[j] - 1))
 
    # Return the count
    return answer
 
# Driver Code
if __name__ == '__main__':
     
    # Given grid arr
    arr = [ [ 1, 0, 1, 0 ],
            [ 0, 1, 1, 1 ],
            [ 1, 0, 1, 0 ],
            [ 0, 1, 0, 1 ] ]
 
    # Function call
    print(numberOfTriangle(arr))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
 
class GFG{
 
// Function to count the right-angled
// traingle in the given grid[,]a
static int numberOfTriangle(int[,] a)
{
  int N = a.GetLength(0);
  int M = a.GetLength(1);
 
  // Stores the count of 1s for
  // each []row and column[]
  int []rows = new int[N];
  int []columns = new int[M];
 
  // Find the number of 1s in
  // each of the rows[0, N - 1]
  for(int i = 0; i < N; ++i)
  {
    for(int j = 0; j < M; ++j)
    {
       
      // Increment row[i]
      if (a[i, j] == 1)
      {
        rows[i]++;
      }
    }
  }
 
  // Find the number of 1s in
  // each of the columns[0, N - 1]
  for(int i = 0; i < M; ++i)
  {
    for(int j = 0; j < N; ++j)
    {
       
      // Increment columns[i]
      if (a[j, i] == 1)
      {
        columns[i]++;
      }
    }
  }
 
  // Stores the count
  // of triangles
  int answer = 0;
 
  for(int i = 0; i < N; i++)
  {
    for(int j = 0; j < M; ++j)
    {
       
      // If current cell
      // has value 1
      if (a[i, j] == 1)
      {
         
        // Update the answer
        answer += (rows[i] - 1) *
               (columns[j] - 1);
      }
    }
  }
 
  // Return the count
  return answer;
}
 
// Driver Code
public static void Main(String[] args)
{
   
  // Given grid [,]arr
  int [,]arr = { { 1, 0, 1, 0 },
                 { 0, 1, 1, 1 },
                 { 1, 0, 1, 0 },
                 { 0, 1, 0, 1 } };
 
  // Function Call
  Console.Write(numberOfTriangle(arr));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出
16

时间复杂度: O(N*M),其中 NxM 是给定网格的维度。
空间复杂度: O(N*M)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live