gpt4 book ai didi

java - Java中的递归迷宫

转载 作者:行者123 更新时间:2023-12-01 12:37:18 25 4
gpt4 key购买 nike

我编写了一段简短的 Java 代码,用于解决从 S 到 G 的简单迷宫问题。

我不明白问题出在哪里。

 import java.util.Scanner;

public class tester
{
static char [][] grid={
{'.','.'},
{'.','.'},
{'S','G'},
};

static int a=2;
static int b=2;


static boolean findpath(int x, int y)
{
if((x > grid.length-1) || (y > grid[0].length-1) || (x < 0 || y < 0))
{
return false;
}
else if(x==a && y==b){
return true;
}

else if (findpath(x,y-1) == true){
return true;
}
else if (findpath(x+1,y) == true){
return true;
}
else if (findpath(x,y+1) == true) {
return true;
}
else if (findpath(x-1,y) == true){
return true;
}
return false;

}

public static void main(String[] args){
boolean result=findpath(2,0);
System.out.print(result);
}

}

我直接给出起始位置,目标在 a 和 b 中定义。帮忙吧。

最佳答案

没有像 (2;2) 这样的单元格。数组从 0 开始编号。您还会获得不定式递归,因为您多次访问一个单元格。

关于java - Java中的递归迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com