[英]2-dimensional array of 1s and 0s
I need to create a random maze of 1
s and 0
s, and it does not have to be possible to solve. 我需要创建一个1
s和0
s的随机迷宫,并且不必解决。 The program I am writing needs to be able to search through this maze and tell me whether not it is possible to get from the beginning to end. 我正在编写的程序必须能够在这个迷宫中进行搜索,并告诉我是否有可能从头到尾获得帮助。 0
s are fine to go through, but 1
s are like walls. 0
s可以通过,但是1
s就像墙壁。 Currently I am just trying to create the random array, and what I have so far compiles, but it returns this: [[I@4ea20232
. 目前,我只是在尝试创建随机数组,到目前为止我已经编译了该数组,但是它返回以下内容: [[I@4ea20232
。 So far I have this: 到目前为止,我有这个:
import java.util.Random;
public class SearchMaze{
public static void main(String [] args){
int n = 8;
int m = 7;
int[][] maze = new int[n][m];
Random rand = new Random();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
maze[i][j] = rand.nextInt(2);
System.out.println(maze);
}
}
I am trying to get something like this: 我试图得到这样的东西:
1000000
1111100
0000001
1110111
1010101
1010101
0000110
1110000
I can't seem to find why something like this wouldn't work. 我似乎找不到为什么这样的东西行不通的原因。
The problem is that the toString
of an array returns useless information. 问题是数组的toString
返回无用的信息。 Using a list will not help much because it will print everything on one line with commas. 使用列表无济于事,因为它将所有内容用逗号打印在一行上。 Try to use nested loops to output the data. 尝试使用嵌套循环来输出数据。
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
System.out.print(maze[i][j]);
}
System.out.print('\n');
}
When you print an object like System.out.println(a);
当您打印类似System.out.println(a);
的对象时System.out.println(a);
, you're really calling the toString()
method of that object. ,您实际上是在调用该对象的toString()
方法。 Thus you're really calling System.out.println(a.toString());
因此,您实际上是在调用System.out.println(a.toString());
. 。
The toString()
method of Arrays isn't particularly useful, so by doing System.out.println(maze)
you're not going to see anything informative. Arrays的toString()
方法不是特别有用,因此通过执行System.out.println(maze)
您将不会看到任何有用的信息。
Fortunately, the Arrays
class has helper methods that can reduce your problem to a single line . 幸运的是, Arrays
类具有帮助程序方法,可以将您的问题减少到一行 。 Try using: 尝试使用:
System.out.println(Arrays.deepToString(maze));
Given that your maze
is 2d, you can use some replaceAll calls to format it like your post: 假设您的maze
是二维的,则可以使用一些replaceAll调用来将其格式化为类似于您的帖子:
System.out.println(Arrays.deepToString(maze)
.replaceAll("],\\s\\[", "\n")
.replaceAll(",\\s|]|\\[", "")
);
Instead filling the maze with random values, you need to start will all 1
s and fill it with random paths eg going north or east for a given length. 取而代之的是用随机值填充迷宫,您需要开始全部1
s并用随机路径填充它,例如向北或向东移动给定长度。
Just like the loops you have you need to print them this way too. 就像您拥有的循环一样,您也需要以这种方式打印它们。
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
Not the answer to your problem, as it has already been answered, but how do you know where to begin and where to end? 不是已经解决的问题的答案,而是如何知道从哪里开始和从哪里结束?
Anyway, I came here to steer you towards the A* algorithm - it's a path finding algorithm that would be great for this type of application. 无论如何,我来这里是为了指导您使用A *算法-这是一种适合此类应用程序的寻路算法。
I think a better solution would be to create an object for the maze: 我认为更好的解决方案是为迷宫创建一个对象:
public class Maze {
public Maze(final int[][] maze) {
this.maze = maze;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
builder.append(maze[i][j]);
}
builder.append("\r\n");
}
return builder.toString();
}
private final int[][] maze;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.