2012-11-13 55 views
2

我已经编写了一个基于试图解决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]或horizo​​ntal [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; 
} 

回答

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

在上述情况下,index value不相同。

的问题是,由一次调用的betterMove方法的第二个时间内,currentRow值已发生变化,如你在你的方法传递currentRow作为paramters之一,因此,其用于horizontal阵列index与您在if (moveMade)之外调用方法时使用的方法不同。

注意,区别仅在于2nd statement。第一个将在两个方面相同。

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

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

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 
} 
在这种情况下

现在,这两个阵列具有相同index value - temp1。另外,如果您使用方法调用的返回值不止一次,并且该方法正在修改您的任何一个passed parameter,则应该总是调用该方法一次,并存储返回值并使用它相当。这样可以避免你目睹奇怪的结果。

+0

作为增加了额外的,这个版本是相当的可读性 – Tedil

相关问题