识别矩阵的对称性[] []

时间:2018-08-05 08:34:42

标签: java matrix symmetry

我试图查找矩阵[] []是否在不同的方向(水平/垂直或垂直)对称,并找到了本教程 https://www.geeksforgeeks.org/check-horizontal-vertical-symmetry-binary-matrix/并在我的矩阵中进行了尝试,但似乎无法正常工作。 这是我当前的代码以及得到的输出:

示例矩阵:

111
144
144

我通过以下方式调用本教程中的方法:

 // test for symmetrie 
    checkHV(matrix, rows, colums);

在这种情况下,行和列均为3。 以我的观点,它应该输出NO,但是当前输出是VERTICAL(始终带有所有矩阵..)。这是为什么?以及如何修改代码以使其对我正常工作?谢谢!

这里是本教程中的代码:

 static void checkHV(int [][]arr, int N,
                int M)
{

// Initializing as both horizontal 
// and vertical symmetric.
boolean horizontal = true;
boolean vertical = true;

// Checking for Horizontal Symmetry. 
// We compare first row with last
// row, second row with second
// last row and so on.
for (int i = 0, k = N - 1; 
         i < N / 2; i++, k--)
{

    // Checking each cell of a column.
    for (int j = 0; j < M; j++)
    {

        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

// Checking for Vertical Symmetry. We compare
// first column with last column, second xolumn
// with second last column and so on.
for (int i = 0, k = M - 1;
         i < M / 2; i++, k--)
{

    // Checking each cell of a row.
    for (int j = 0; j < N; j++)
    {
        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

if (!horizontal && !vertical)
    System.out.println("NO");

else if (horizontal && !vertical)
    System.out.println("HORIZONTAL");

else if (vertical && !horizontal)
    System.out.println("VERTICAL");

else
    System.out.println("BOTH");
}

编辑: 在将horizo​​ntal = true更改为vertical = true后,代码仍然无法正确处理矩形矩阵,因此4 * 2,给我一个超出范围的数组。 矩阵:

1112
2212

行= 2,列=4。

2 个答案:

答案 0 :(得分:0)

尝试一下:

 static void checkHV(int [][]arr, int N,
            int M)
{

// Initializing as both horizontal 
// and vertical symmetric.
boolean horizontal = true;
boolean vertical = true;

// Checking for Horizontal Symmetry. 
// We compare first row with last
// row, second row with second
// last row and so on.
for (int i = 0, k = N - 1; 
         i < N / 2; i++, k--)
{

    // Checking each cell of a column.
    for (int j = 0; j < M; j++)
    {

        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

// Checking for Vertical Symmetry. We compare
// first column with last column, second xolumn
// with second last column and so on.
for (int i = 0, k = M - 1;
         i < M / 2; i++, k--)
{

    // Checking each cell of a row.
    for (int j = 0; j < N; j++)
    {
        // check if every cell is identical
        if (arr[j][i] != arr[j][k])
        {
            vertical = false;
            break;
        }
    }
}

if (!horizontal && !vertical)
    System.out.println("NO");

else if (horizontal && !vertical)
    System.out.println("HORIZONTAL");

else if (vertical && !horizontal)
    System.out.println("VERTICAL");

else
    System.out.println("BOTH");
}

答案 1 :(得分:0)

public static boolean isSymmetricHorizontally(int[][] matrix) {
    for(int rowL = 0, rowH = matrix.length - 1; rowL < rowH; rowL++, rowH--)
        for(int col = 0; col < matrix[0].length; col++)
            if(matrix[rowL][col] != matrix[rowH][col])
                return false;
    
    return true;
}

public static boolean isSymmetricVertically(int[][] matrix) {
    for(int colL = 0, colH = matrix[0].length - 1; colL < colH; colL++, colH--)
        for(int row = 0; row < matrix[0].length; row++)
            if(matrix[row][colL] != matrix[row][colL])
                return false;
    
    return true;
}