我编写了一个基于尝试解决骑士巡回赛问题的程序。我相信我已经想出了一个合适的解决方案,一切看起来都很好。
我很好奇的一个小问题是一小段代码,它基于展望未来可能的方格来实现最佳移动。
如果我这样实现(implementation1)——</p>
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)这样无害且看似微不足道的小改动——</p>
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 ] 并让返回的结果在表达式中使用。我之前已经设法编写这样的代码而没有任何问题。
这是被调用的方法——</p>
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;
}