我已经编写了一个基于试图解决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