📌  相关文章
📜  求矩阵中正方形的最大边长

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

求矩阵中正方形的最大边长

给定一个奇数阶 N 的方阵。任务是找到矩阵中形成的最大正方形的边长。如果正方形的周长上的所有元素都相同,并且其中心是矩阵的中心,则称形成了矩阵中的正方形。矩阵的中心也是一个边长为 1 的正方形。
例子

Input : matrix[][] = {2, 3, 0,
                      0, 2, 0,
                      0, 1, 1}
Output : 1

Input : matrix[][] = {2, 2, 2,
                      2, 1, 2,
                      2, 2, 2}
Output : 3

任何奇数阶矩阵的中心是索引为i = j = n/2 的元素。现在,要在矩阵中找到最大的正方形,请检查距离中心相同距离的所有 sorrounding 元素。对于正方形,其为i-单元从中心远离检查所有这些都是N / 2-in / 2 + i个行和列也为N / 2差异和任一元素的索引的元素则-不超过我.请在下面的示例中找到具有不同边长的正方形的格式。

x x x x x
x y y y x
x y c y x
x y y y x
x x x x x

需要关心的点:

  • 由于中心元素也是正方形的形式,最小可能的边长为 1。
  • 最长的正方形可以是所有元素都出现在第一行和第一列,因此最大可能的边长是n

算法 :



  • 迭代 i=0 到 n/2
    • 对于第 i 行和第 ni-1 行,将第 i 列迭代到 ni-1
      反之亦然,并检查所有元素是否相同。
    • 如果是,则正方形的长度为 n-2*i。
    • 否则进行下一次迭代

下面是上述方法的实现:

C++
// C++ program to find max possible side-length
// of a square in a given matrix
 
#include 
#define n 5
using namespace std;
 
// Function to return side-length of square
int largestSideLen(int matrix[][n])
{
    int result = 1;
 
    // Traverse the matrix
    for (int i = 0; i < n / 2; i++) {
 
        int element = matrix[i][i];
        int isSquare = 1;
 
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i][j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1][j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j][i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j][n - i - 1] != element)
                isSquare = 0;
        }
 
        if (isSquare)
            return n - 2 * i;
    }
 
    // Return result
    return result;
}
 
// Driver program
int main()
{
    int matrix[n][n] = { 1, 1, 1, 1, 1,
                         1, 2, 2, 2, 1,
                         1, 2, 1, 2, 1,
                         1, 2, 2, 2, 1,
                         1, 1, 1, 1, 1 };
 
    cout << largestSideLen(matrix);
 
    return 0;
}


Java
// Java program to find max possible side-length
// of a square in a given matrix
 
import java.io.*;
 
class GFG {
static int n = 5;
    // Function to return side-length of square
static int largestSideLen(int matrix[][])
{
    int result = 1;
 
    // Traverse the matrix
    for (int i = 0; i < (n / 2); i++) {
 
        int element = matrix[i][i];
        int isSquare = 1;
 
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i][j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1][j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j][i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j][n - i - 1] != element)
                isSquare = 0;
        }
 
        if (isSquare > 0)
            return n - (2 * i);
    }
 
    // Return result
    return result;
}
 
// Driver program
     
    public static void main (String[] args) {
        int matrix[][] = {{ 1, 1, 1, 1, 1},
                        {1, 2, 2, 2, 1},
                        {1, 2, 1, 2, 1},
                        {1, 2, 2, 2, 1},
                        {1, 1, 1, 1, 1 }};
 
    System.out.println (largestSideLen(matrix));
         
         
    }
}


Python3
# Python 3 program to find max possible
# side-length of a square in a given matrix
 
n = 5
 
# Function to return side-length of square
def largestSideLen(matrix):
    result = 1
 
    # Traverse the matrix
    for i in range(int(n / 2)):
        element = matrix[i][i]
        isSquare = 1
 
        for j in range(i, n - i):
             
            # for row i
            if (matrix[i][j] != element):
                isSquare = 0
                 
            # for row n-i-1
            if (matrix[n - i - 1][j] != element):
                isSquare = 0
                 
            # for column i
            if (matrix[j][i] != element):
                isSquare = 0
                 
            # for column n-i-1
            if (matrix[j][n - i - 1] != element):
                isSquare = 0
 
        if (isSquare):
            return n - 2 * i
 
    # Return result
    return result
 
# Driver Code
if __name__ == '__main__':
    matrix = [[1, 1, 1, 1, 1],
              [1, 2, 2, 2, 1],
              [1, 2, 1, 2, 1],
              [1, 2, 2, 2, 1],
              [1, 1, 1, 1, 1]]
 
    print(largestSideLen(matrix))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C#  program to find max possible side-length
// of a square in a given matrix
using System;
 
public class GFG{
    static int n = 5;
    // Function to return side-length of square
static int largestSideLen(int [,]matrix)
{
    int result = 1;
 
    // Traverse the matrix
    for (int i = 0; i < (n / 2); i++) {
 
        int element = matrix[i,i];
        int isSquare = 1;
 
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i,j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1,j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j,i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j,n - i - 1] != element)
                isSquare = 0;
        }
 
        if (isSquare > 0)
            return n - (2 * i);
    }
 
    // Return result
    return result;
}
 
// Driver program
     
     
    static public void Main (){
        int [,]matrix = {{ 1, 1, 1, 1, 1},
                        {1, 2, 2, 2, 1},
                        {1, 2, 1, 2, 1},
                        {1, 2, 2, 2, 1},
                        {1, 1, 1, 1, 1 }};
 
           Console.WriteLine(largestSideLen(matrix));
         
    }
}


PHP


Javascript


输出:
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程