查找矩阵中的重复元素

时间:2019-05-04 13:49:29

标签: java

简介

我正在做一些作业,我们的任务是制作寻找对子的游戏。 我做了一个矩阵,并用这样的字母填充它:

Display
----------------
 C   H   F   E   
 G   F   D   D   
 E   C   H   B   
 A   B   G   A  

现在,我目前正在测试使用空矩阵并使用给定输入(row_1, col_1, row_2, col_2, gameMatrix)

填充的显示方法

问题

在创建“作弊/测试功能”以测试我的显示方法时。我在查找两个A(或其他任何字母)的位置时遇到了一些麻烦。 这是我尝试的这种方法:

代码

public static void PlayMeBoi(String[][] gameMatrix)
    {
        int row_1 = 0;
        int col_1 = 0;
        int row_2 = 0;
        int col_2 = 0;       


        for (int i = 0; i < gameMatrix.length; i++)
        {
            for (int j = 0; j < gameMatrix.length; j++) 
            {
                if ("A".equals(gameMatrix[i][j]))
                {
                    row_1 = i;
                    col_1 = j;
                    break;
                }
            }
        }
        for (int i = (row_1+1); i < gameMatrix.length; i++)
        {

            for (int j = (col_1+1); j < gameMatrix.length; j++) 
            {
                if ("A".equals(gameMatrix[i][j]))
                {

                    row_2 = i;
                    col_2 = j;
                    break;
                }
            }
        }

        System.out.println("First " + gameMatrix[row_1][col_1] + " at " + " [ " + row_1 + " ] " + "," + " [ " + col_1 + " ] ");
        System.out.println("Second " + gameMatrix[row_1][col_1] + " at " + " [ " + row_2 + " ] " + "," + " [ " + col_2 + " ] ");

        Turn(row_1, col_1, row_2, col_2, gameMatrix);

    }

有关代码的注释

  • 我正在使用String not char
  • Turn是用于评估字母是否等于字母(if "A".equals("A"))的函数
  • (row_1+1)(col_1+1)是我的想法,“如果以前没有找到我的字母,那么第二个“ for”将处理矩阵的其余部分)
  • gameMatrix是加载所有字母的矩阵

问题

我希望能够找到矩阵中“ A”或任何其他字母的位置。截至目前,我的当前想法还没有达到我想要的结果

请尽可能多地评论该代码。以后我可能会把它发布在GitHub上,供有兴趣或发现有用的人使用。

感谢您对此问题的关注。

2 个答案:

答案 0 :(得分:2)

第二个for是错误的。让我们看一下示例矩阵:

 C   H   F   E   
 G   F   D   D   
 E   C   H   B   
 A   B   G   A  

如果要查找值D,将首先在row = 1col = 2处找到它。然后在第二个步骤中,您仅从row = 2col = 3开始运行,这意味着在实践中,您只会从找到的位置开始在右边的向下单元格中进行迭代,在此示例中,仅会生成2个单元格而不是9(用*标记):

 C   H   F   E   
 G   F   D   D   
 E   C   H   *B*   
 A   B   G   *A*  

因此,在第二个for中,您应该从同一行和下一列继续搜索:

for (int i = row_1; i < gameMatrix.length; i++)
{
    // In the first row starting from the next cell, in the next rows start
    // from column 0
    int j = i == row_1 ? col_1 + 1 : 0;
    for (; j < gameMatrix.length; j++) 
    {
        if ("A".equals(gameMatrix[i][j]))
        {
            row_2 = i;
            col_2 = j;
            break;
         }
     }
}

答案 1 :(得分:1)

  

如果我以前没有找到我的信,那么第二个“ for”将   处理矩阵的其余部分

正确,但是矩阵的其余部分到底是什么?
如果在Arow = 1中找到第一个col = 1,则矩阵的其余部分是索引为> 1的每个项目。这将删除索引为(1,2),(2,1)和(1,3)等的项目。
还有其他问题。
当您将break放在嵌套循环中时,它只会从嵌套循环中中断,而不是从外部循环中中断。
这是我想出的一个解决方案,也许它不是最佳解决方案,但我认为它可行:

public static void PlayMeBoi(String[][] gameMatrix) {
    int row_1 = -1;
    int col_1 = -1;
    int row_2 = -1;
    int col_2 = -1;

    boolean found = false;
    for (int i = 0; i < gameMatrix.length; i++) {
        if (found) break;
        for (int j = 0; j < gameMatrix[0].length; j++) {
            if ("A".equals(gameMatrix[i][j])) {
                row_1 = i;
                col_1 = j;
                found = true;
                break;
            }
        }
    }

    if (!found) {
        System.out.println("Not Found");
        return;
    }

    found = false;
    for (int i = 1; i < gameMatrix.length; i++) {
        if (found) break;
        for (int j = 1; j < gameMatrix[0].length; j++) {
            if (i * gameMatrix[0].length + j > row_1 * gameMatrix[0].length + col_1) {
                if ("A".equals(gameMatrix[i][j])) {
                    row_2 = i;
                    col_2 = j;
                    found = true;
                    break;
                }
            }
        }
    }

    System.out.println("First " + gameMatrix[row_1][col_1] + " at " + " [ " + row_1 + " ] " + "," + " [ " + col_1 + " ] ");
    if (!found) {
        System.out.println("Second Not Found");
        return;
    }
    System.out.println("Second " + gameMatrix[row_1][col_1] + " at " + " [ " + row_2 + " ] " + "," + " [ " + col_2 + " ] ");
}