java - 通过二维数组到达整数 0 的位置
问题描述
我正在尝试通过二维数组并到达具有 0 的位置,并且我想打印程序到达该位置的方向(北、南、东、西)。如果不可能,我必须返回 false。有人能帮我吗。
提前感谢您的帮助
这是我创建的方法,它必须是递归的:
public static Boolean MagicBoard_recursive(int[][] board, int size, int startRow, int startCol) {
boolean result = true;
int number = board[startRow][startCol];
int destinationNumber;
while(!result) {
// If we reach the position that contains the 0
if(board[startRow][startCol] == 0) {
System.out.println("We have reached the goal square containing 0.");
result = true;
break;
}
// If we can only move in 1 direction, either North, South, East or West
// Verify if we can only move south
if((startRow+number) < size && (startRow-number) < 0 && ((startCol+number) > size || (startCol-number) < 0)) {
destinationNumber = board[startRow+number][startCol];
// Make sure that the destination position will not make us go back and forth
if((startRow == (startRow+number-destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
result = false;
break;
}
// If don't go back and forth, then we continue
else {
startRow = startRow+number;
System.out.print("Move south " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// Verify if we can only move North
else if((startRow+number) > size && (startRow-number) > 0 && ((startCol+number) > size || (startCol-number) < 0)){
destinationNumber = board[startRow-number][startCol];
// Make sure that we won't go back and forth if we go at that position
if((startRow == (startRow-number+destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
result = false;
break;
}
// If don't go back and forth, then we continue
else {
startRow = startRow-number;
System.out.print("Move north " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If can only go east
else if((startCol+number)<size && (startCol-number)<0 && ((startRow+number)>size || (startRow-number)<0)) {
destinationNumber = board[startRow][startCol+number];
// Make sure that we won't go back and forth if we go at that position
if((startCol == (startCol+number-destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
result = false;
break;
}
// If we don't go back and forth, then we continue
else {
startCol = startCol+number;
System.out.print("Move east " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If can't move any other direction that west
else if((startCol+number) > size && (startCol-number)> 0 && ((startRow+number)>size || (startRow-number)<0)) {
destinationNumber = board[startRow][startCol-number];
// Make sure that we won't go back and forth if we go at that position
if((startCol == (startCol-number+destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
result = false;
break;
}
// If we don't go back and forth, then we continue
else {
startCol = startCol-number;
System.out.print("Move west " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If we can move any direction
else {
// try moving south
SOUTH:
if(startRow-number < 0) {
destinationNumber = board[startRow+number][startCol];
// Verify if we go back and forth if we go south, if we do, then we the result will be
if((startRow == (startRow+number-destinationNumber))) {
break SOUTH;
}
else {
startRow = startRow+number;
System.out.print("Move south " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
NORTH:
if(startRow-number > 0) {
destinationNumber = board[startRow-number][startCol];
if((startRow == (startRow-number+destinationNumber))) {
break NORTH;
}
else {
startRow = startRow-number;
System.out.print("Move north " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// try moving east
EAST:
if(startCol-number<0) {
destinationNumber = board[startRow][startCol+number];
// If will go back and forth at that position, then we exit the EAST label and we go to the next label
if((startCol == (startCol+number-destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
break EAST;
}
else {
startCol = startCol+number;
System.out.print("Move east " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// Try moving west
WEST:
if(startCol-number > 0) {
destinationNumber = board[startRow][startCol-number];
// If we go back and forth in that position, then we exit the EAST label
if((startCol == (startCol-number+destinationNumber))) {
System.out.print("The MagicBoard is unsolvable");
result = false;
break WEST;
}
else {
startCol = startCol-number;
System.out.print("Move west " + number + ", ");
MagicBoard_recursive(board, size, startRow, startCol);
}
}
}
}
return result;
}
解决方案
这是一个示例应用程序,它解释了如何“记录”“搜索”的路径。
package direction;
import java.util.ArrayList;
import java.util.List;
enum DIRECTION {
NORTH,
SOUTH,
WEST,
EAST
}
public class Direction {
private static List<DIRECTION> pathway = new ArrayList<DIRECTION>();
public static Boolean MagicBoard_recursive(/* your arguments are here */) {
/* your program code is here */
/* your program "pointer/searcher" moves NORTH */
pathway.add(DIRECTION.NORTH);
/* your program "pointer/searcher" moves SOUTH */
pathway.add(DIRECTION.SOUTH);
/* your program "pointer/searcher" moves WEST */
pathway.add(DIRECTION.WEST);
/* your program "pointer/searcher" moves EAST */
pathway.add(DIRECTION.EAST);
/* this way you can see the "path" of your program code */
for(int i = 0; i < pathway.size(); i++) {
System.out.println(pathway.get(i));
}
return true; /* this is just a placeholder, use your own return value here */
}
public static void main(String[] args) {
MagicBoard_recursive();
}
}
推荐阅读
- javascript - Jquery数据表内置排序不起作用
- flutter - 如何使用flutter downgrade命令切换到旧版本的Flutter SDK?
- java - 如何使用jsp中的href链接在浏览器中打开pdf和文本文件
- python - 具有一个特征的多个时间步长的 keras 嵌入层修改
- javascript - 404 页面未找到/未找到在 Angular Routing for Mobile View 中找到的网址的网页
- thymeleaf - 如何将所选项目保存在下拉选项中,并在编辑期间使用 thymeleaf 检索该数据?
- progressive-web-apps - 删除 PWA 并重新安装
- javascript - 为什么按钮标签中的 onlick 功能无法正常工作
- javascript - 使用 flex 修复标题现在在 ipad 中工作
- javascript - 如何为 react dom 中新创建的视频元素设置 srcObj?另外,我怎样才能 play() 这个新创建的视频元素?