java - 数组语句内的方法调用导致程序出现 "hang"

标签 java knights-tour

我编写了一个程序,试图解决骑士之旅问题。我相信我已经想出了一个合适的解决方案,并且一切看起来都很好。

我很好奇的一个小问题是一小部分代码,它根据对 future 可能的方 block 的展望来实现最佳的移动。

如果我像这样实现它(实现1)-

if( moveMade )  // if any move is possible
{
    currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
    board[ currentRow ][ currentColumn ] = squareCounter;
    squareCounter++;
    moveMade = false;
}
else
moveMade = true;    // if there are no moves possible this is the end

软件会挂起 – 为什么?

如果我做了一个像这样无害且看似微不足道的小改变(实现2) -

int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );

if( moveMade )  // if any move is possible
{
    currentRow += vertical[ temp1 ];
    currentColumn += horizontal[ temp1 ];
    board[ currentRow ][ currentColumn ] = squareCounter;
    squareCounter++;
    moveMade = false;
}
else
    moveMade = true;    // if there are no moves possible this is the end

然后一切都很好,代码将得出结论。

我正在使用 netbeans 7.1 来编写我的软件,并且认为它一定与 IDE 有关,因此我尝试仅使用“javac”和命令行来编译它,结果却发现了相同的结果。我不明白为什么我不能在像这样的数组参数中调用此方法 - 垂直 [ HERE ] 或水平 [ HERE ] 并在表达式中使用返回的结果。我以前曾成功地编写过这样的代码,没有任何问题。

这是被调用的方法 -

public static int betterMove( int moverMatrix[], int theHorizontal[], int theVertical[], int accessBoard[][], int newCurrentRow, int newCurrentColumn )
{
    int[] equalMatrix = new int [ 8 ];  // records the positions which are equal in value with a (1)
    int[] bmValueMatrix = new int[ 8 ]; // holds the numbers taken from accessibility heuristic
    int[] finalTable = new int[ 8 ];    // the best move discovered
    int best = bestMove( moverMatrix ); // the lowest number in the given array
    int startPos = best + 1;
    int moveNumber = 0;
    int originalCurrentRow = newCurrentRow;
    int originalCurrentColumn = newCurrentColumn;

    equalMatrix[ best ] = 1;    // mark the lowest value position with a 1

    initVMatrix( bmValueMatrix );
    initVMatrix( finalTable );

    for( int i = startPos; i < 8; i++ ) // mark the elements of equal value in equalMatrix with (1)
    {
        if( moverMatrix[ best ] == moverMatrix[ i ] )
            equalMatrix[ i ] = 1;
    }

    for( int j = 0; j < 8; j++ )    // go through each element of equalMatrix and look forward
    {                               // for best accessibility heuristic
        newCurrentRow = originalCurrentRow;
        newCurrentColumn = originalCurrentColumn;
        if( equalMatrix[ j ] == 1 )
        {
            newCurrentRow += theVertical[ j ];
            newCurrentColumn += theHorizontal[ j ];
            while( moveNumber < 8 )
            {
                if( newCurrentRow + theVertical[ moveNumber ] >= 0 &&
                        newCurrentRow + theVertical[ moveNumber ] < 8 )
                {
                    if( newCurrentColumn + theHorizontal[ moveNumber ] >= 0 &&
                            newCurrentColumn + theHorizontal[ moveNumber ] < 8 )
                    {
                        bmValueMatrix[ moveNumber ] = accessBoard[ newCurrentRow + theVertical[ moveNumber ] ]
                                                                    [ newCurrentColumn + theHorizontal[ moveNumber ] ]; 
                    }   // end if
                }   // end if
                moveNumber++;
            }   // end while 
            moveNumber = 0;
            finalTable[ j ] = bestMove( bmValueMatrix );
            initVMatrix( bmValueMatrix );
        }   // end if

    }   // end for
    return bestMove( finalTable );   
}

上面return语句中使用的方法bestmove -

public static int bestMove( int theMoves[] )
{
    int theLowest = 10,
        idealMove = 0;

    for( int i = 0; i < 8; i++ )
    {
        if( theMoves[ i ] < theLowest )
        {
            theLowest = theMoves[i];
            idealMove = i;
        }
    }
    return idealMove;
}

最佳答案

currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];

在上述情况下,两个数组的索引值不相同。

问题是,当您第二次调用 betterMove 方法时,currentRow 值已更改,并且当您传递 currentRow 时> 在您的方法中作为参数之一,因此,用于水平数组的索引与调用时使用的索引不同if (moveMade) 之外的方法。

请注意,差异仅针对第二条语句。第一个方法与这两种方法相同。

所以,这可能是一个问题。

因此,您需要首先存储该方法执行的返回值,然后使用该返回值作为verticalhorizo​​ntal数组的索引。

int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );

if( moveMade )  // if any move is possible
{
    currentRow += vertical[ temp1 ];
    currentColumn += horizontal[ temp1 ];
    // Rest of the code
}

现在,在这种情况下,两个数组具有相同的索引值 - temp1

此外,如果您多次使用某个方法调用的返回值,并且该方法正在修改任何传递的参数,则您应该始终调用该方法一次,并存储返回值,并使用它。这将使您免于目睹奇怪的结果。

关于java - 数组语句内的方法调用导致程序出现 "hang",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13363084/

相关文章:

python - Pygame 运行几秒钟后变得无响应

java - 骑士之旅需要回溯

java - 如何遍历 Hashmap 中的元素?

java - 特定对象的时间 日期

用于文件系统而不是网络的 Java NIO 框架?

javascript - 用回溯法解决 Knight's Tour (javascript)

python - 使用回溯在 8x8 棋盘上实现骑士之旅

java - 带邻接表的骑士巡回算法

java - 如何从方法中增加实例变量

Java ReadWriteLock 重入