Java 在给定的算法中几乎没有变化
这是关于改变迷宫算法的 我这是什么意思?我们得到了一个二维数组,其中0代表“不可能通过”,1代表“可能通过”。 一个从x到y的算法(也被称为:猫到老鼠)。 这正是下面的算法所做的 作为我们的投入:Java 在给定的算法中几乎没有变化,java,algorithm,Java,Algorithm,这是关于改变迷宫算法的 我这是什么意思?我们得到了一个二维数组,其中0代表“不可能通过”,1代表“可能通过”。 一个从x到y的算法(也被称为:猫到老鼠)。 这正是下面的算法所做的 作为我们的投入: {1, 0, 0,}, {1, 1, 0}, {0, 1, 1} }; 以及输出: (0,0) // ressembles the coordinates of the 1 in top left corner (1,0) // ressembles the 1 under the first 1
{1, 0, 0,},
{1, 1, 0},
{0, 1, 1} };
以及输出:
(0,0) // ressembles the coordinates of the 1 in top left corner
(1,0) // ressembles the 1 under the first 1 I just explained
(1,1) // ...
(2,1)
(2,2)
我想改变一些小事情:
public List<Coordinate> solve() {
return getMazePath(0, 0, new Stack<Coordinate>());
}
公共列表求解(){
返回getMazePath(0,0,new Stack());
}
不知何故,我需要对第二个零执行0-1操作,但如果我无法访问x和y声明,该怎么办?我真的相信0-1会让我从左下角开始,而不是左上角,对吗
对于2.)需要对列(也称为y)进行更改。
它需要-1而不是+1,对吗
对不起,这墙文字,我真的试图让它简短,但我似乎失败了:p
无论如何,我希望有人会读到这篇文章^^
没有更改的算法:
import java.util.Arrays;
import java.util.*;
final class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Alg {
private final int[][] maze;
public Alg(int[][] maze) {
if (maze == null) {
throw new NullPointerException("The input maze cannot be null");
}
if (maze.length == 0) {
throw new IllegalArgumentException("The size of maze should be greater than 0");
}
this.maze = maze;
}
public List<Coordinate> solve() {
return getMazePath(0, 0, new Stack<Coordinate>());
}
private List<Coordinate> getMazePath(int row, int col, Stack<Coordinate> stack) {
assert stack != null;
stack.add(new Coordinate(row, col));
if ((row == maze.length - 1) && (col == maze[0].length - 1)) {
Coordinate[] coordinateArray = stack.toArray(new Coordinate[stack.size()]);
return Arrays.asList(coordinateArray);
}
for (int j = col; j < maze[row].length; j++) {
if ((j + 1) < maze[row].length && maze[row][j + 1] == 1) {
return getMazePath(row, j + 1, stack);
}
if ((row + 1) < maze.length && maze[row + 1][col] == 1) {
return getMazePath(row + 1, col, stack);
}
}
return Collections.emptyList();
}
public static void main(String[] args) {
int[][] m = { {1, 0, 0,},
{1, 1, 0},
{0, 1, 1} };
Alg maze = new Alg(m);
for (Coordinate coord : maze.solve()) {
System.out.println("("+coord.getX() + "," + coord.getY()+")");
}
}
}
导入java.util.array;
导入java.util.*;
最终类坐标{
私人最终int x;
私人终审法院;
公共坐标(整数x,整数y){
这个.x=x;
这个。y=y;
}
公共int getX(){
返回x;
}
公共int getY(){
返回y;
}
}
公共类Alg{
私人最终int[][]迷宫;
公共Alg(int[][]迷宫){
if(maze==null){
抛出新的NullPointerException(“输入迷宫不能为null”);
}
如果(maze.length==0){
抛出新的IllegalArgumentException(“迷宫大小应大于0”);
}
这个迷宫=迷宫;
}
公共列表求解(){
返回getMazePath(0,0,new Stack());
}
私有列表getMazePath(int行、int列、堆栈){
断言堆栈!=null;
叠加(新坐标(行、列));
if((row==maze.length-1)和&(col==maze[0].length-1)){
坐标[]坐标array=stack.toArray(新坐标[stack.size()]);
返回数组.asList(coordinarray);
}
对于(int j=col;j
查看getMazePath
的方法声明。当前0,0作为行
和列
传递给该参数。因此,您将发送2,0(对于第2行第0列,左下角),而不是像当前编码的那样向该方法发送0,0
方向移动在
getMazePath()
方法内的for
循环中,两个if语句检查列j+1
(右侧列)或行行+1
(当前行下方的行)中是否有1。你可以修改它们,用减号代替加号分别向左或向上移动。重复项:,,,,。丹尼尔:你在浪费时间。这张海报一次又一次地回答了同样的问题,他一直忽略答案,试图让别人为他写代码。非常感谢你的回答。你会得到绿色支票,因为这是我一直在寻找的,它现在就像一个魅力。对于你的气垫船,我真的不知道你的问题是什么。这是我在这个网站上的第一个也是最后一个问题,如果没有像你这样有生命的人,我真的不需要这个问题。@HovercraftFullOfEels谢谢你的提醒。我不知道他会这么做。对我来说似乎是一个简单的问题和一个简单的答案,当我开始键入我的答案时,没有人对这个问题投反对票。。。