简体   繁体   中英

Find duplicated elements in a matrix

Introduction

I'm doing some homework where we are tasked for making a game of finding pairs. I made a matrix and filled it with letters as such:

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

Right now I'm currently testing the display method which uses an empty matrix and fills it with the given input (row_1, col_1, row_2, col_2, gameMatrix)

Problem

While creating a "cheat/test function" to test my display method. I encounter some trouble with finding the position of both A's (or any other letter). This is my try at such method:

Code

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);

    }

Notes about the code

  • I'm working with String not char
  • Turn is the function which evaluates if a letter equals a letter ( if "A".equals("A") )
  • The (row_1+1) and (col_1+1) it's my thought at "if I haven't found my letter previously, then the second 'for' will handle the rest of the matrix)
  • gameMatrix is the matrix where all the letters are loaded

Question

I want to be able to find the position of both "A" or any other letter inside the matrix. As of now, I'm not getting my desired result with my current idea

Feel free to comment about the code as much as you can. I might post it on GitHub later on for those who are interested or find anything useful in it.

Thanks for the interest in this question.

The second for is wrong. let's look at your example matrix:

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

If you're looking for the value D you'll find it first at row = 1 and col = 2 . then in the second for you only runs from row = 2 and col = 3 which means in practice you'll iterate only over the right down cells from the position you found, which in this example will result in only 2 cells instead of 9 (marked in *):

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

So in the second for what you should do is continue the search from the same row and the next column:

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;
         }
     }
}

if I haven't found my letter previously, then the second 'for' will handle the rest of the matrix

Correct, but what exactly is the rest of the matrix?
If the 1st A is found in row = 1 and col = 1 , is the rest of the matrix every item with indices > 1 . This would leave out items with indices (1,2) and (2,1) and (1,3) etc.
There are other issues also.
When you put a break inside a nested loop it only breaks form the nested loop and not the outer.
Here's a solution I came up with, maybe it's not optimal but I think it works:

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 + " ] ");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM