Java 如何通过位置单元传递坐标(上/下/左/右):递归

Java 如何通过位置单元传递坐标(上/下/左/右):递归,java,recursion,Java,Recursion,所以我已经在overflow上搜索了大约30分钟,我找不到任何我认为对我有帮助的东西。我来了 我正在做一个迷宫算法,基本上,我在做这个递归方法,但idk,如果我正在做的工作。听起来很蠢…它不会在我的显示器上显示带有箭头(方向)的迷宫,一个,两个显然我需要“在它旁边传递一个坐标(例如,上面的单元格)”,我需要以某种方式编码它。这是我的方法 private boolean findPath(char[][] maze, int r, int c) { // Base Case #1: If Cur

所以我已经在overflow上搜索了大约30分钟,我找不到任何我认为对我有帮助的东西。我来了

我正在做一个迷宫算法,基本上,我在做这个递归方法,但idk,如果我正在做的工作。听起来很蠢…它不会在我的显示器上显示带有箭头(方向)的迷宫,一个,两个显然我需要“在它旁边传递一个坐标(例如,上面的单元格)”,我需要以某种方式编码它。这是我的方法

private boolean findPath(char[][] maze, int r, int c) {

// Base Case #1: If Current Cell == 'H'
if(maze[r][c] == 'H') { 
  return true; // Stop Recursion, Conclusion: Path Found from 'G' to 'H'
} 

// Base Case #2: If Current Cell == '#' (Wall)
else if(maze[r][c] == '#') {
  return false; // Stop Recursion, Conclusion: Path NOT Found
}

// Base Case #3: If Current Cell == '.' (Blocked Path/Dead End)
else if(maze[r][c] == '.') {
  return false; // Stop Recursion, Conclusion: Path NOT Found
} 

// Recursive Cases:
if(maze[r][c] == ' ') { // Check If Current Cell == ' '
  maze[r][c] = '^'; // If Yes, Place Symbol
  findPath(maze, r-1, c); // Recursively Go Up
  // code here ???? add missing code??? HELP MEEEE
  if(maze[r][c] == 'H') { // Check If We Have Reached The End
    return true; // If Yes, Return True
  }
}
// Viewing Purposes
    for (int x = 0; x < maze.length; x++) { // Go Through Rows
      display.newLine(); // Viewing Purposes
      for (int y = 0; y < maze[x].length; y++) { // Go Through Columns
        display.writeC(maze[x][y]); // Viewing Purposes
      }
    }
return false; // If End of Method Reached. Therefore, Hansel Hasn't Been Found Yet
}
private boolean findPath(char[][]迷宫,int r,int c){
//基本情况#1:如果当前单元格='H'
如果(迷宫[r][c]='H'){
return true;//停止递归,结论:找到从“G”到“H”的路径
} 
//基本情况#2:如果当前单元格='#'(墙)
else if(迷宫[r][c]='#'){
return false;//停止递归,结论:找不到路径
}
//基本情况#3:如果当前单元格='。(阻塞路径/死端)
else如果(迷宫[r][c]='.')){
return false;//停止递归,结论:找不到路径
} 
//递归情况:
如果(迷宫[r][c]=''){//检查当前单元格是否=''
迷宫[r][c]='^';//如果是,则放置符号
findPath(maze,r-1,c);//递归地向上
//代码在这里???添加缺少的代码???帮助MEEEE
如果(迷宫[r][c]='H'){//检查我们是否已经到达终点
return true;//如果是,则返回true
}
}
//查看目的
对于(intx=0;x
编写递归方法时要做的一件非常重要的事情是,如果该方法返回值,请使用递归调用返回的返回值。如果您没有使用它,就像您在这里所做的那样:

if(maze[r][c] == ' ') { // Check If Current Cell == ' '
  maze[r][c] = '^'; // If Yes, Place Symbol
  // you are discarding the return value here!
  findPath(maze, r-1, c); // Recursively Go Up
那么你可能出了什么问题

findPath
的返回值告诉您是否可以从某个位置找到路径。你可以利用这些信息。如果上升可以找到一条路径,那么您就知道您已经标记了正确的路径,并且可以返回true。如果往上走不能找到路径,那么你知道往上走不是正确的方向,所以你要检查另一个方向。如果在检查了所有方向后,您仍然没有找到路径,那么您知道您在错误的路径上,因此您将
maze[r][c]
设置回
'
,并返回false

private static boolean findPath(char[][] maze, int r, int c) {

    // Base Case #1: If Current Cell == 'H'
    if(maze[r][c] == 'H') {
       return true; // Stop Recursion, Conclusion: Path Found from 'G' to 'H'
    }

    // (Your other base cases are covered by the last "return false" statement) 

    // Recursive Cases:
    if(maze[r][c] == ' ') { // Check If Current Cell == ' '
        maze[r][c] = '^';
        if (findPath(maze, r-1, c)) {
            return true;
        }
        maze[r][c] = 'v';
        if (findPath(maze, r+1, c)) {
            return true;
        }
        maze[r][c] = '>';
        if (findPath(maze, r, c+1)) {
            return true;
        }
        maze[r][c] = '<';
        if (findPath(maze, r, c-1)) {
            return true;
        }
        maze[r][c] = ' ';
    }
    return false; // If End of Method Reached. Therefore, Hansel Hasn't Been Found Yet
}
输出:

true
#####H#####
#v# #^# # #
#v# #^# # #
#v# #^# # #
#>>>>^    #
###########

请注意,我的代码假设迷宫四周都有墙或
H
。如果迷宫中没有,则需要首先检查
c
r
是否在2D数组的范围内。如果它们超出范围,则应返回false。

嘿,John,你能详细说明一下你期望的是什么吗?嘿,谢谢你的回答,所以基本上我从a点到b点…我想让我的a点,我称之为“G”,检查一组方向,如果可能的话,试着移动到它们,同时用一个与方向对应的箭头标记它们。我明白了,非常感谢你,但很快你就到了已写入//(您的其他基本情况包含在最后一条“return false”语句中)你能解释一下我喜欢我理解这是真的,但是一个深入的解释bcuz我应该把它们作为基本情况放在那里…比如,呃,我的意思是,如果我遇到了死胡同或什么的话,我应该标记一个周期,然后回去运行它,最终阻塞错误的路径。。。。我还没到那一步,但我想这是另一个递归的例子……如果我用词不当,我很抱歉weird@JohnDao我的意思是,对
的检查是多余的,因为如果
迷宫[r][c]
,即使没有明确检查
#
,最后一个
返回值false也会被命中。如果遇到
时需要做一些特殊的事情,那是另一个问题。试着先自己做,如果你被卡住了,发布另一个问题,清楚地说明你想做什么,并展示你的尝试。哦,好吧,我现在明白了,虽然这是多余的,但保留下来可以吗?再次感谢大家@JohnDao是的,你可以保留它,代码仍然可以工作,但这是不必要的。好的,谢谢你,你对行迷宫[r][c]=''有什么建议意见吗;在检查了所有方向之后
private static boolean findPath(char[][] maze, int r, int c) {

    // Base Case #1: If Current Cell == 'H'
    if(maze[r][c] == 'H') {
       return true; // Stop Recursion, Conclusion: Path Found from 'G' to 'H'
    }

    // (Your other base cases are covered by the last "return false" statement) 

    // Recursive Cases:
    if(maze[r][c] == ' ') { // Check If Current Cell == ' '
        maze[r][c] = '^';
        if (findPath(maze, r-1, c)) {
            return true;
        }
        maze[r][c] = 'v';
        if (findPath(maze, r+1, c)) {
            return true;
        }
        maze[r][c] = '>';
        if (findPath(maze, r, c+1)) {
            return true;
        }
        maze[r][c] = '<';
        if (findPath(maze, r, c-1)) {
            return true;
        }
        maze[r][c] = ' ';
    }
    return false; // If End of Method Reached. Therefore, Hansel Hasn't Been Found Yet
}