我已經編寫了一個基於試圖解決Knight's Tour問題的程序。我相信我已經提出了一個合適的解決方案,一切都很好。數組語句中的方法調用導致程序「掛起」
我很好奇的一個小問題是一小部分代碼實現了基於展望未來可能的廣場的最佳舉措。
如果我實現它像這樣(implementation1) -
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
軟件將掛起 - 爲什麼?
如果我做一個小無毒,看似微不足道的變化,如本(implementation2) -
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」和命令行才發現相同的結果進行編譯。我不明白爲什麼我不能在像這樣的數組參數內調用此方法 - vertical [HERE]或horizontal [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);
}
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;
}
作爲增加了額外的,這個版本是相當的可讀性 – Tedil