【发布时间】:2021-02-07 14:31:54
【问题描述】:
我正在做一个像迷宫一样通过二维数组的方法,目标是到达包含整数 0 的位置。我能够记录路径,但它不会移动,它保持在初始位置。
这是我所说的不动的例子:
Please input the size of the board (between 5 and 20):
5
Please choose a starting index from the 4 options below:
Press 1 for "top-left"
Press 2 for "top-right"
Press 3 for "bottom-left"
Press 4 for "bottom-rigth"
Enter your choice:
1
2 1 1 1 4
2 3 4 1 3
1 1 1 2 4
3 0 3 3 2
3 2 3 4 2
Move south 2, Move north 2, Move east 2, Move west 2,
有人可以帮帮我吗?
到目前为止,这是我的方法:
public static boolean MagicBoard_recursive(int[][] board, int size, int startRow, int startCol) {
boolean solvable = true;
int number = board[startRow][startCol];
int[] moves = {startRow+number, startRow-number, startCol+number, startCol-number}; // This array contains all the possible moves we can do
for(int i = 0; i < 4; i++) {
// If we reached the position containing the integer 0
if(board[startRow][startCol] == 0) {
solvable = true;
break;
}
if(startRow+number > size && startRow-number < 0 && startCol+number > size && startCol-number<0) {
solvable = false;
break;
}
solvable = true;
// If we try moving south
if(i == 0) {
//int destinationNumber = board[startRow+number][startCol];
// If we move to this position, will we be able to continue, if not, then we try another move
if(startRow+number > size) {
solvable = false;
continue;
}
else {
while(solvable) {
startRow +=number;
System.out.print("Move south " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
}
// If try moving north
else if(i == 1) {
// If we move to this position, will we be able to continue. If not, then we try another move
if(startRow-number < 0) {
solvable = false;
continue;
}
else {
while(solvable) {
startRow -=number;
System.out.print("Move south " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
}
// If try moving east
else if(i == 2) {
if(startCol+number > size) {
solvable = false;
continue;
}
else {
while(solvable) {
startCol += number;
System.out.print("Move east " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
}
// If try moving west
else if(i == 3) {
if(startCol-number < 0) {
solvable = false;
break;
}
else {
while(solvable) {
startCol -=number;
System.out.print("Move west " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
}
}
return solvable;
}
这是一张图片,展示了程序解决棋盘的动作示例:
【问题讨论】:
-
我觉得你需要进一步澄清运动是如何发生的。我仍然不太了解所需的运动机制。
-
@OmarAbdelBari,我会尝试更好地解释自己。我要做的是一个包含小于或等于板尺寸的随机数的板,并且有一个位置包含整数 0。根据用户选择的起始位置,可能有也可能没有包含整数 0 的位置的可能路径。如果可以解决它,我必须记录程序采用的路径。如果没有可能到达 0 的路径,那么我必须返回 false。我的解释清楚了吗?
-
什么是合法的举动?例如,在#1 中,我向南移动。为什么它在#2 中一直到板底?
-
@NomadMaker 圆圈标记板上的开始位置。圆圈中的整数表示圆圈可以在棋盘上移动一定数量的方块。在一个步骤中,圆圈可以向东、向西、向北或向南移动。在每一步,选择的方向都是固定的。圆圈不能移出棋盘。唯一合法的起始位置是四个角方块。棋盘必须恰好包含一个目标方格,其值为 0,可以位于棋盘上的任何位置。
-
@Jennifer 我认为有一种更简单的方法可以使用队列和类似于广度优先搜索的方法来解决这个问题。
标签: java recursion multidimensional-array