遍历二维数组以达到整数值为0的位置

huangapple go评论145阅读模式
英文:

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(&quot;We have reached the goal square containing 0.&quot;);
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) &lt; size &amp;&amp; (startRow-number) &lt; 0 &amp;&amp; ((startCol+number) &gt; size || (startCol-number) &lt; 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(&quot;The MagicBoard is unsolvable&quot;);
result = false;
break;
}
// If don&#39;t go back and forth, then we continue
else {
startRow = startRow+number;
System.out.print(&quot;Move south &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// Verify if we can only move North
else if((startRow+number) &gt; size &amp;&amp; (startRow-number) &gt; 0 &amp;&amp; ((startCol+number) &gt; size || (startCol-number) &lt; 0)){
destinationNumber = board[startRow-number][startCol];
// Make sure that we won&#39;t go back and forth if we go at that position
if((startRow == (startRow-number+destinationNumber))) {
System.out.print(&quot;The MagicBoard is unsolvable&quot;);
result = false;
break;
}
// If don&#39;t go back and forth, then we continue
else {
startRow = startRow-number;
System.out.print(&quot;Move north &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If can only go east
else if((startCol+number)&lt;size &amp;&amp; (startCol-number)&lt;0 &amp;&amp; ((startRow+number)&gt;size || (startRow-number)&lt;0)) {
destinationNumber = board[startRow][startCol+number];
// Make sure that we won&#39;t go back and forth if we go at that position
if((startCol == (startCol+number-destinationNumber))) {
System.out.print(&quot;The MagicBoard is unsolvable&quot;);
result = false;
break;
}
// If we don&#39;t go back and forth, then we continue
else {
startCol = startCol+number;
System.out.print(&quot;Move east &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If can&#39;t move any other direction that west
else if((startCol+number) &gt; size &amp;&amp; (startCol-number)&gt; 0 &amp;&amp; ((startRow+number)&gt;size || (startRow-number)&lt;0)) {
destinationNumber = board[startRow][startCol-number];
// Make sure that we won&#39;t go back and forth if we go at that position
if((startCol == (startCol-number+destinationNumber))) {
System.out.print(&quot;The MagicBoard is unsolvable&quot;);
result = false; 
break;
}
// If we don&#39;t go back and forth, then we continue
else {
startCol = startCol-number;
System.out.print(&quot;Move west &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// If we can move any direction
else {
// try moving south
SOUTH:
if(startRow-number &lt; 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(&quot;Move south &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
NORTH:
if(startRow-number &gt; 0) {
destinationNumber = board[startRow-number][startCol];
if((startRow == (startRow-number+destinationNumber))) {
break NORTH;
}
else {
startRow = startRow-number;
System.out.print(&quot;Move north &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// try moving east
EAST:
if(startCol-number&lt;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(&quot;The MagicBoard is unsolvable&quot;);
break EAST;
}
else {
startCol = startCol+number;
System.out.print(&quot;Move east &quot; + number + &quot;, &quot;);
MagicBoard_recursive(board, size, startRow, startCol);
}
}
// Try moving west	
WEST:
if(startCol-number &gt; 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(&quot;The MagicBoard is unsolvable&quot;);
result = false;
break WEST;
}
else {
startCol = startCol-number;
System.out.print(&quot;Move west &quot; + number + &quot;, &quot;);
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&lt;DIRECTION&gt; pathway = new ArrayList&lt;DIRECTION&gt;();
public static Boolean MagicBoard_recursive(/* your arguments are here */) {
/* your program code is here */
/* your program &quot;pointer/searcher&quot; moves NORTH */
pathway.add(DIRECTION.NORTH);
/* your program &quot;pointer/searcher&quot; moves SOUTH */
pathway.add(DIRECTION.SOUTH);
/* your program &quot;pointer/searcher&quot; moves WEST */
pathway.add(DIRECTION.WEST);
/* your program &quot;pointer/searcher&quot; moves EAST */
pathway.add(DIRECTION.EAST);
/* this way you can see the &quot;path&quot; of your program code */
for(int i = 0; i &lt; 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();
}
}

huangapple
  • 本文由 发表于 2020年10月24日 09:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定