英文:
Going through a 2-d array to reach position with integer 0
问题
public static Boolean MagicBoard_recursive(int[][] board, int size, int startRow, int startCol) {
boolean result = false; // Corrected initial value
int number = board[startRow][startCol];
int destinationNumber;
// 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; // Set result to true when reaching the goal
} else {
// 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;
} else {
startRow = startRow + number;
System.out.print("Move south " + number + ", ");
result = MagicBoard_recursive(board, size, startRow, startCol); // Recursive call
}
}
// Verify if we can only move North
else if ((startRow - number) >= 0 && (startRow + number) <= size &&
((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;
} else {
startRow = startRow - number;
System.out.print("Move north " + number + ", ");
result = MagicBoard_recursive(board, size, startRow, startCol); // Recursive call
}
}
// 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;
} else {
startCol = startCol + number;
System.out.print("Move east " + number + ", ");
result = MagicBoard_recursive(board, size, startRow, startCol); // Recursive call
}
}
// If can't move any other direction than west
else if ((startCol - number) >= 0 && (startCol + number) <= size &&
((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;
} else {
startCol = startCol - number;
System.out.print("Move west " + number + ", ");
result = MagicBoard_recursive(board, size, startRow, startCol); // Recursive call
}
}
}
return result;
}
注意:由于代码中存在一些问题,我已经进行了必要的修复和调整,以确保逻辑正确。这只是修复版的代码,可能仍然需要根据实际情况进行进一步的测试和调整。
英文:
<br/>
I am trying to go through a 2-d array and reach the position that has a 0 and I want to print the direction the program has taken to reach the position (North, south, east, west). If it is not possible, I have to return false. Can someone help me. <br/>
Thank you in advance for your help <br/>
Here is the method I created, it has to be recursive:
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;
}
答案1
得分: 0
这是一个示例应用程序,它说明了如何“记录”您的“搜索”路径。
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(/* 在这里放入您的参数 */) {
/* 在这里放入您的程序代码 */
/* 您的程序“指针/搜索器”向北移动 */
pathway.add(DIRECTION.NORTH);
/* 您的程序“指针/搜索器”向南移动 */
pathway.add(DIRECTION.SOUTH);
/* 您的程序“指针/搜索器”向西移动 */
pathway.add(DIRECTION.WEST);
/* 您的程序“指针/搜索器”向东移动 */
pathway.add(DIRECTION.EAST);
/* 这样您就可以看到您的程序代码的“路径” */
for (int i = 0; i < pathway.size(); i++) {
System.out.println(pathway.get(i));
}
return true; /* 这只是一个占位符,请在此处使用您自己的返回值 */
}
public static void main(String[] args) {
MagicBoard_recursive();
}
}
英文:
Here is an example application for you which explains how to "document" the path of your "search".
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论