java-检查数组

java-检查数组,java,arrays,algorithm,Java,Arrays,Algorithm,今晚,我编写了一个代码,其中包含一个.txt文件,该文件的名称来自命令行参数。它将文本文件的每个字符放入一个数组中。 这是这些文本文件的一个示例 7 11 ########### # # # # ### # # # # # K ### # # # # ########### 我想检查K是否有可能走过每一个“,”是墙。所以,如果我在这里检查这个lvl,它应该打印为false,因为在右边有一面墙,而在这里 5 8 ######## # K #

今晚,我编写了一个代码,其中包含一个.txt文件,该文件的名称来自命令行参数。它将文本文件的每个字符放入一个数组中。 这是这些文本文件的一个示例

7 11
###########
#     #   #
#   ###   #
#   #     #
# K ###   #
#     #   #
###########
我想检查K是否有可能走过每一个“,”是墙。所以,如果我在这里检查这个lvl,它应该打印为false,因为在右边有一面墙,而在这里

5 8
########
#  K   #
# ###  #
#      #
########
这应该是真的。这就是我取得的成绩(对不起,德语变量名和注释):

这应该是真的,但这不是因为下半部分的字段刚刚连接到墙,但仍然可以访问。。在我的脚本中,我没有检查索引[0][n]和[1][n]处的数组,也没有检查[n][0]&&[n][1],因此我没有收到此错误,但我希望。。我的算法不起作用的情况还有:

8 11
###########
#         #
# ######  #
# #K   #  #
# #    #  #
# ######  #
#         #
###########

也许你对如何改进代码有一些想法。。我完全是个初学者,只会说两周的java。。提前感谢您阅读本文:)

有几种方法可以实现这一点,对于我的回答,我将使用广度优先遍历网格

char [][] grid;
Deque<Point> queue = new ArrayDeque<>();
HashSet<Point> visited = new HashSet<>();
int kx; // x coordinate of k
int ky; // y coordinate of k
Point start = new Point(kx, ky);
queue.add(start);

int [] dx = {0, 0, 1, -1, -1, 1, 1, -1};
int [] dy = {1, -1, 0, 0, -1, 1, -1, 1};
while (!queue.isEmpty()) {
    Point p = queue.poll();
    if (visited.contains(p)) {
        continue;
    }
    visited.add(p);
    for (int i = 0; i < dx.length; i++) {
        int nx = (int)p.getX()+dx[i];
        int ny = (int)p.getY()+dy[i];
        if (validCoordinates(nx, ny) && grid[nx][ny] == ' ')) {
            queue.add(new Point(nx, ny));
        }
    }
}

boolean allReachable = true;
outer: for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[0].length; j++) {
        if (grid[i][j] == ' ' && !visited.contains(new Point(i, j))) {
            allReachable = false;
            break outer;
        }
    }
}

System.out.println(allReachable ? "All white spaces are reachable" : "Not all white spaces are reachable");
char[][]网格;
Deque queue=new ArrayDeque();
访问的HashSet=新HashSet();
int kx;//k的x坐标
int-ky;//k的y坐标
点开始=新点(kx,ky);
queue.add(开始);
int[]dx={0,0,1,-1,-1,1,1,-1};
int[]dy={1,-1,0,0,-1,1,-1,1};
而(!queue.isEmpty()){
p点=queue.poll();
如果(包含(p)){
持续
}
添加(p);
对于(int i=0;i
这个想法是从k开始,访问所有打开的相邻单元,然后递归地执行。每次访问打开的单元格时,都会将其添加到集合中。访问完可能的单元格后,在网格中循环,对于每个打开的单元格,检查它是否在访问的集合中。如果至少有一个打开的单元格不在已访问的集合中,则表示您未能从起始位置访问所有打开的单元格


顺便说一句,我没有在实现中包含
validCoordinates
方法,它非常简单,可以自己解决。希望这能有所帮助。

您的代码无法用于具有特定属性的迷宫,如上一个示例。使用flood fill()来获得正确的结果如何在代码中实现这一点?是否有脚本要导入,或者我必须自己导入?洪水填充是解决此类问题的众所周知的解决方案;在SO搜索框中键入“flood fill”将返回100多个问题;应该很容易找到一个可以帮助您在代码中执行下一步的方法。validCoordinates方法是什么?对不起,我甚至不知道如何制作自己的函数。。。我一直使用公共静态void main(String args[]):<我是否必须像导入java.io一样导入它。*?在oracle java文档页面上找不到它:Xso我试图理解您的代码,但我已经开始对行Deque queue=new ArrayDeque()(我知道你的想法,但我不明白你是如何利用它来编写代码的。\n我试图通过导入java来使代码正常工作。*;公共类解决方案{public static void main(String args[]){}但它不起作用validCoordinates是一种方法,如果传递的参数在网格的边界内,它将返回true。如果您还不知道如何定义自己的方法,那么您可以用检查0Deque是否为接口来替换validCoordinates。ArrayDeque类实现该接口。ArrayDeque是一种数据结构它的行为类似于双端队列,这意味着您可以从队列的任意一侧添加/删除元素。
8 11
###########
#         #
# ######  #
# #K   #  #
# #    #  #
# ######  #
#         #
###########
char [][] grid;
Deque<Point> queue = new ArrayDeque<>();
HashSet<Point> visited = new HashSet<>();
int kx; // x coordinate of k
int ky; // y coordinate of k
Point start = new Point(kx, ky);
queue.add(start);

int [] dx = {0, 0, 1, -1, -1, 1, 1, -1};
int [] dy = {1, -1, 0, 0, -1, 1, -1, 1};
while (!queue.isEmpty()) {
    Point p = queue.poll();
    if (visited.contains(p)) {
        continue;
    }
    visited.add(p);
    for (int i = 0; i < dx.length; i++) {
        int nx = (int)p.getX()+dx[i];
        int ny = (int)p.getY()+dy[i];
        if (validCoordinates(nx, ny) && grid[nx][ny] == ' ')) {
            queue.add(new Point(nx, ny));
        }
    }
}

boolean allReachable = true;
outer: for (int i = 0; i < grid.length; i++) {
    for (int j = 0; j < grid[0].length; j++) {
        if (grid[i][j] == ' ' && !visited.contains(new Point(i, j))) {
            allReachable = false;
            break outer;
        }
    }
}

System.out.println(allReachable ? "All white spaces are reachable" : "Not all white spaces are reachable");