【发布时间】:2021-05-12 06:32:07
【问题描述】:
public static int bombs(char[][] maze, int row, int col, ArrayList<int[]> checked, int bombs)
{
int size = maze.length;
String value = Character.toString(maze[row][col]);
if (value.equals("#"))//add one to bomb if it went through a wall
{
bombs = bombs+1;
}
if(row==size-1&&col==size-1)//if it has reached the end
{
return steps;
}
if(inBounds(row,col,size)) //if point is in bounds
{
if(!pointWasChecked(row,col,checked))
{ //if point has not been checked
checked.add(new int[]{row, col});
}
}
return 0;
}
/*
. . # # #
. # . . #
# # # # #
# . # . .
# # # . .
example maze, this one's smallest path should be 2 bombs used
. is a path
# is a wall
/*
这是一种遍历迷宫的递归方法,它在到达墙壁时使用炸弹,但我想返回所需的最小数量的炸弹。我已经知道如何通过再次调用该方法并执行 row+1、row-1、col+1、col-1 来遍历迷宫。我只是对我需要返回什么感到困惑,这样我才能走到最后,不要最终返回零,并返回最少数量的炸弹。 感谢您的帮助!
【问题讨论】:
-
递归方法是一种通过调用自身来获取结果的方法。您在上面发布的内容看起来并不像使用递归。