首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确定矩阵[][]的对称性

确定矩阵[][]的对称性
EN

Stack Overflow用户
提问于 2018-08-05 08:34:41
回答 2查看 767关注 0票数 0

我试图找出一个矩阵是否在不同的方向上是对称的(horitzonzal/垂直的或两者都是),并找到了本教程https://www.geeksforgeeks.org/check-horizontal-vertical-symmetry-binary-matrix/,并尝试了我的矩阵,但它似乎不能正确地工作。下面是我的当前代码和我得到的输出:

示例矩阵:

代码语言:javascript
复制
111
144
144

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

代码语言:javascript
复制
 // test for symmetrie 
    checkHV(matrix, rows, colums);

在这种情况下,行和列都是3。在我看来,它应该输出NO,但是当前的输出是垂直的(所有矩阵都是.)。为什么会这样呢?我如何修改代码,使其对我正确工作呢?谢谢!

下面是教程中的代码:

代码语言:javascript
复制
 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");
}

编辑:在更改horitz周= true = true for垂直=true时,代码仍然不能很好地处理矩形矩阵,所以f.e。4*2,给了我一个超出界限的数组。矩阵:

代码语言:javascript
复制
1112
2212

行=2,列=4。

EN

回答 2

Stack Overflow用户

发布于 2020-10-16 13:54:39

试试这个:

代码语言:javascript
复制
 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");
}
票数 0
EN

Stack Overflow用户

发布于 2020-10-16 14:15:31

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51692710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档