日期:2014-05-20 浏览次数:20693 次
private int currentMinSteps = Integer.MAX_INT;
// this is the recursion I mentioned
void find (Tile currentTile, int currentSteps) {
Tile tiles[] = findTilesAround(currentTile);
sortTilesByItsNumber(tiles);
for (Tile tile: tiles) {
if (hasVisited(tile)) continue;
if (calculateCurrentTotal(tile) == halfTotal){
if (isSolutionCuttable()) // the step I mentioned to test if the solution can be cut by just once
currentMinSteps = currentMinSteps < currentSteps ? currentMinSteps : currentSteps;
continue;
}
else if (currentSteps >= currentMinSteps) continue; // this is where time gets saved
else
find (tile, currentSteps + 1); // the recursive step, which will find all solutions
}
}