检测2d数组中值的边界周围的相同值

时间:2013-11-23 06:37:39

标签: java arrays algorithm logic multidimensional-array

让我们从一个简单的结构开始:

m m N N N
m m N N N
m m N N N
m m m m m
m m m m m

如何检测中间的N是否包围了其他N.

到目前为止,我已经这样做了:

public class Main {
    public static void main(String[] args) {
        char[][] grid = new char[][]{
                {'m','m','N','N','N'},
                {'m','m','N','N','N'},
                {'m','m','N','N','N'},
                {'m','m','m','m','m'},
                {'m','m','m','m','m'},
        };

        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
               if(grid[i][j] == 'N') {
                    //NOW WHAT??
               }
            }
        }
    }
}

我已经迭代了2d数组的每个值但现在我没有得到如何做到这一点? :/

更新:关于你的答案,我喜欢他们,但我想检测'N'是否会产生正方形

2 个答案:

答案 0 :(得分:1)

   if(grid[i][j] == 'N') {
   if(grid[i-1][j]=='N'&&grid[i+1][j]=='N'&&grid[i][j-1]=='N'&&grid[i][j+1]=='N'&&grid[i-1][j-1]=='N'$$grid[i-1][j+1]=='N'&&grid[i+1][j-1]=='N'&&grid[i+1][j+1]=='N'){
    System.out.println("Key is "+i+","+j)
   }
 }

还有一件事your i and j should start with 1并且正在运行upto < 5-1

这会给key

带来的8 N's

答案 1 :(得分:0)

这是你必须做的。检查每个坐标的同一行,下面和上面。

public static void main(String[] args) {
    char[][] grid = new char[][] { { 'm', 'm', 'N', 'N', 'N' }, { 'm', 'm', 'N', 'N', 'N' }, { 'm', 'm', 'N', 'N', 'N' }, { 'm', 'm', 'm', 'm', 'm' }, { 'm', 'm', 'm', 'm', 'm' }, };

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (grid[i][j] == 'N') {
                if (isThisCoordinateSurroundedByCharacter(grid, i, j, 'N')) {
                    System.out.println("Coordinates " + i + "," + j + " are surrounded.");
                }
            }
        }
    }
}
private static boolean isThisCoordinateSurroundedByCharacter(char[][] grid, int x, int y, char c) {
    // Check above of it.
    if (y > 0) {
        if (grid[x][y - 1] != c) {
            return false;
        }
        if (x > 0 && grid[x - 1][y - 1] != c) { // Check at the left
            return false;
        }
        if (x < grid.length - 1 && grid[x + 1][y - 1] != c) { // Check at
                                                                // the
                                                                // right
            return false;
        }
    }
    // Check in the same line
    if (x > 0 && grid[x - 1][y] != c || x < grid.length - 1 && grid[x + 1][y] != c) {
        return false;
    }
    // Check below
    if (y < grid.length - 1) {
        if (grid[x][y + 1] != c) {
            return false;
        }
        if (x > 0 && grid[x - 1][y + 1] != c) { // Check at the left
            return false;
        }
        if (x < grid.length - 1 && grid[x + 1][y + 1] != c) { // Check at
                                                                // the
                                                                // right
            return false;
        }
    }
    return true;
}

这是我使用此代码获得的输出:

Coordinates 0,3 are surrounded.
Coordinates 0,4 are surrounded.
Coordinates 1,3 are surrounded.
Coordinates 1,4 are surrounded.