📜  给定矩阵中回文加路径的计数

📅  最后修改于: 2021-09-02 06:30:42             🧑  作者: Mango

给定一个N x M整数矩阵,任务是计算数组中回文正数的数量。

例子:

方法:
要解决此问题,请按照以下步骤操作:

  • 遍历所有可以成为回文加号中心的单元格,即除第一行和最后一行列之外的所有单元格。
  • 对于所有这些单元格(i, j) ,检查a[i][j – 1]是否等于a[i][j + 1]并且a[i – 1][j]等于a[i + 1][j] 。如果两个条件都满足,则增加回文正数的计数。
  • 打印回文加号的最终计数。

下面是上述方法的实现:

C++
// C++ Program to count the number
// of palindromic pluses in
// a given matrix
#include 
using namespace std;
  
// Function to count and return
// the number of palindromic pluses
int countPalindromicPlus(
    int n, int m,
    vector >& a)
{
    int i, j, k;
    int count = 0;
  
    // Traverse all the centers
    for (i = 1; i < n - 1; i++) {
        for (j = 1; j < m - 1; j++) {
  
            // Check for palindromic plus
            // Check whether row and
            // column are palindrome or not
            if (a[i + 1][j] == a[i - 1][j]
                && a[i][j - 1] == a[i][j + 1])
                ++count;
        }
    }
    // Return the answer
    return count;
}
  
// Driver code
int main()
{
    int n = 4, m = 4;
  
    vector > a
        = { { 1, 2, 1, 3 },
            { 2, 3, 2, 3 },
            { 3, 2, 1, 2 },
            { 2, 3, 2, 3 } };
    cout << countPalindromicPlus(
                n, m, a)
         << endl;
  
    return 0;
}


Java
// Java program to count the number
// of palindromic pluses in
// a given matrix
class GFG{
  
// Function to count and return
// the number of palindromic pluses
static int countPalindromicPlus(int n, int m,
                                int [][]a)
{
    int i, j;
    int count = 0;
  
    // Traverse all the centers
    for(i = 1; i < n - 1; i++)
    {
       for(j = 1; j < m - 1; j++)
       {
            
          // Check for palindromic plus
          // Check whether row and
          // column are palindrome or not
          if (a[i + 1][j] == a[i - 1][j] && 
              a[i][j - 1] == a[i][j + 1])
              ++count;
       }
    }
      
    // Return the answer
    return count;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 4, m = 4;
    int [][]a = { { 1, 2, 1, 3 },
                  { 2, 3, 2, 3 },
                  { 3, 2, 1, 2 },
                  { 2, 3, 2, 3 } };
                    
    System.out.print(
           countPalindromicPlus(n, m, a) + "\n");
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 Program to count the number
# of palindromic pluses in
# a given matrix
  
# Function to count and return
# the number of palindromic pluses
def countPalindromicPlus(n, m, a):
    i, j, k = 0, 0, 0
    count = 0
  
    # Traverse all the centers
    for i in range(1, n - 1):
        for j in range(1, m - 1):
  
            # Check for palindromic plus
            # Check whether row and
            # column are palindrome or not
            if (a[i + 1][j] == a[i - 1][j]
                and a[i][j - 1] == a[i][j + 1]):
                count += 1
                  
    # Return the answer
    return count
  
# Driver code
if __name__ == '__main__':
    n = 4
    m = 4
  
    a = [[1, 2, 1, 3 ],
         [2, 3, 2, 3 ],
         [3, 2, 1, 2 ],
         [2, 3, 2, 3 ]]
    print(countPalindromicPlus(n, m, a))
  
# This code is contributed by Mohit Kumar


C#
// C# program to count the number
// of palindromic pluses in
// a given matrix
using System;
class GFG{
  
// Function to count and return
// the number of palindromic pluses
static int countPalindromicPlus(int n, int m,
                                int [,]a)
{
    int i, j;
    int count = 0;
  
    // Traverse all the centers
    for(i = 1; i < n - 1; i++)
    {
        for(j = 1; j < m - 1; j++)
        {
              
            // Check for palindromic plus
            // Check whether row and
            // column are palindrome or not
            if (a[i + 1, j] == a[i - 1, j] && 
                a[i, j - 1] == a[i, j + 1])
                ++count;
        }
    }
      
    // Return the answer
    return count;
}
  
// Driver code
public static void Main()
{
    int n = 4, m = 4;
    int [,]a = {{ 1, 2, 1, 3 },
                { 2, 3, 2, 3 },
                { 3, 2, 1, 2 },
                { 2, 3, 2, 3 }};
                  
    Console.Write(
            countPalindromicPlus(n, m, a) + "\n");
}
}
  
// This code is contributed by Code_Mech


输出:
3

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

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