0,1矩阵中的递归路径查找(并保存所有可能的路径)java
我试图编写一个递归方法,该方法将在不回溯到int矩阵中包含值0,1的位置的情况下查找路径。0可以踩踏,1不能踩踏。我还限制了一条比50步更长的路径 位置是具有行和列值(x,y)的对象。 locationEquals是一个函数,如果两个位置相同,则返回true;如果两个位置不相同,则返回false。 map变量是我试图在其中查找路径的矩阵0,1矩阵中的递归路径查找(并保存所有可能的路径)java,java,recursion,matrix,Java,Recursion,Matrix,我试图编写一个递归方法,该方法将在不回溯到int矩阵中包含值0,1的位置的情况下查找路径。0可以踩踏,1不能踩踏。我还限制了一条比50步更长的路径 位置是具有行和列值(x,y)的对象。 locationEquals是一个函数,如果两个位置相同,则返回true;如果两个位置不相同,则返回false。 map变量是我试图在其中查找路径的矩阵 private static List<List<Location>> options = new ArrayList<List&
private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path)
{
Location current = path.get(path.size() - 1);
boolean done = false;
if(locationEquals(current,new Location(24,38)))
{
options.add(path);
return;
}
if(path.size() > 50) done = true;
if(!done)
{
try
{
if(map[current.row][current.col + 1] == 0)
{
if(!path.contains(new Location(current.row, current.col + 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row - 1][current.col] == 0)
{
if(!path.contains(new Location(current.row - 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row][current.col - 1] == 0)
{
if(!path.contains(new Location(current.row, current.col - 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row + 1][current.col] == 0)
{
if(!path.contains(new Location(current.row + 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
}
private static List options=new ArrayList();
公共静态void路径查找(列表路径)
{
当前位置=path.get(path.size()-1);
布尔完成=假;
if(位置等于(当前,新位置(24,38)))
{
选项。添加(路径);
返回;
}
如果(path.size()>50)done=true;
如果(!完成)
{
尝试
{
if(map[current.row][current.col+1]==0)
{
如果(!path.contains(新位置(current.row,current.col+1)))
{
列表温度=路径;
临时添加(新位置(current.row,current.col+1));
路径查找(临时);
}
}
}
捕获(例外e){}
尝试
{
if(map[current.row-1][current.col]==0)
{
如果(!path.contains)(新位置(current.row-1,current.col)))
{
列表温度=路径;
临时添加(新位置(当前行-1,当前列));
路径查找(临时);
}
}
}
捕获(例外e){}
尝试
{
if(map[current.row][current.col-1]==0)
{
如果(!path.contains(新位置(current.row,current.col-1)))
{
列表温度=路径;
临时添加(新位置(current.row,current.col-1));
路径查找(临时);
}
}
}
捕获(例外e){}
尝试
{
if(map[current.row+1][current.col]==0)
{
如果(!path.contains)(新位置(current.row+1,current.col)))
{
列表温度=路径;
临时添加(新位置(当前行+1,当前列));
路径查找(临时);
}
}
}
捕获(例外e){}
}
执行以下代码后,“选项”为空,这意味着它找不到方法。但是这个矩阵中肯定有方法,所以我的代码中有一个我找不到的错误。问题是,每次执行递归的下一步时,都没有创建新的列表(您的temp变量不是真正的临时变量,因为它只是对路径的引用,而不是路径的副本) 为了解决这个问题,我将
List temp=path;
替换为List temp=new ArrayList(path);
因此,代码是:
private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path) {
Location current = path.get(path.size() - 1);
boolean done = false;
if (locationEquals(current, new Location(24, 38))) {
options.add(path);
return;
}
if (path.size() > 50) done = true;
if (!done) {
try {
if (map[current.row][current.col + 1] == 0) {
if (!path.contains(new Location(current.row, current.col + 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row - 1][current.col] == 0) {
if (!path.contains(new Location(current.row - 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row][current.col - 1] == 0) {
if (!path.contains(new Location(current.row, current.col - 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row + 1][current.col] == 0) {
if (!path.contains(new Location(current.row + 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
}
}
private static List options=new ArrayList();
公共静态void路径查找(列表路径){
当前位置=path.get(path.size()-1);
布尔完成=假;
if(位置等于(当前,新位置(24,38))){
选项。添加(路径);
返回;
}
如果(path.size()>50)done=true;
如果(!完成){
试一试{
if(map[current.row][current.col+1]==0){
如果(!path.contains(新位置(current.row,current.col+1))){
列表温度=新的阵列列表(路径);
临时添加(新位置(current.row,current.col+1));
路径查找(临时);
}
}
}捕获(例外e){
}
试一试{
if(map[current.row-1][current.col]==0){
如果(!path.contains)(新位置(current.row-1,current.col))){
列表温度=新的阵列列表(路径);
临时添加(新位置(当前行-1,当前列));
路径查找(临时);
}
}
}捕获(例外e){
}
试一试{
if(map[current.row][current.col-1]==0){
如果(!path.contains(新位置(current.row,current.col-1))){
列表温度=新的阵列列表(路径);
临时添加(新位置(current.row,current.col-1));
路径查找(临时);
}
}
}捕获(例外e){
}
试一试{
if(map[current.row+1][current.col]==0){
如果(!path.contains)(新位置(current.row+1,current.col))){
列表温度=新的阵列列表(路径);
临时添加(新位置(当前行+1,当前列));
路径查找(临时);
}
}
}捕获(例外e){
}
}
}
这听起来是一个学习如何使用调试器的好机会。提示:List temp=path;
不做您认为它能做的事情。问题是我无法在我正在编写代码的环境中调试我的程序。这是某种webkit,我必须使用它。虽然当我使用某种调试选项时,它显示了不可用性不知怎的,“路径”大小达到了50+,递归没有停止,它表明,从PathFind的第一次迭代开始,它只进入了4个选项中的一个,而显然我可以分支到所有4个选项。帮助…请显示您调试此项的尝试。您发布的代码甚至没有额外的print*8语句来跟踪intermed的值iate变量。例如,遵循**路径。@EliranZiv在列表
上预生成一个半深副本,创建新的temp
列表,并将其实例化为ArrayList
。然后使用temp.addAll(路径);
添加路径中的所有项目。这将允许temp
成为独立的女性errr列表