数独类型拼图的笼子约束

时间:2012-10-08 11:38:03

标签: java algorithm puzzle sudoku

您好我正在制作kenken求解器,这就像数独一样。我有一个笼子结构,有一些笼子的细胞。我想在每次尝试笼子的值时应用约束。为此,我每次都会调用roe / column / cage约束我的拼图。

然而,我对这个问题的笼子约束感到震惊。这是我所有3个约束的代码。对于笼子约束,我想查看特定细胞笼子的所有细胞,看看传递的数量是否满足我们的标准。

    //Row Constraint Check: Checks if num is an acceptable value for the given Row
public static boolean rowConstraintCheck(int rowIndex, int num){
    for(int columnIndex = 0; columnIndex < puzzleDimension; columnIndex++){
        if(puzzleArray[rowIndex][columnIndex] == num){
            return false;
        }
    }
    return true;
}
//Column Constraint Check: Checks if num is an acceptable value for the given Column    
public static boolean columnConstraintCheck(int columnIndex, int num){
    for(int rowIndex = 0; rowIndex < puzzleDimension; rowIndex++){
        if(puzzleArray[rowIndex][columnIndex] == num){
            return false;
        }
    }
    return true;
}
//Cage constraint Check: Checks if num is an acceptable value for the given Cage
public static boolean cageConstraintCheck(int rowIndex, int columnIndex, int num){
    if(true){
        int cageToCell =  cellToCageMapper[rowIndex][columnIndex];          
        String currentOperator = cages.get(cageToCell).cageOperator;
        int currentTotal = cages.get(cageToCell).cageValue;
        int numberOfCages = cages.get(cageToCell).placeHolders.length;            
        //System.out.println(rowIndex+"."+ columnIndex+"."+ cageToCell +"."+ currentOperator +"."+ currentTotal +"."+ numberOfCages);

         int flagNonZeroCages = 0;
         for(int j=0;j<numberOfCages;j++) {
            int tempIndex = cages.get(cageToCell).placeHolders[j];
            int tempCellRow = (int) (Math.floor(tempIndex/puzzleDimension));
            int tempCellCol = (tempIndex % puzzleDimension);
            if(puzzleArray[tempCellRow][tempCellCol] != 0){
                flagNonZeroCages++;System.out.println("bingo"+j);   
            }
        }
        if(flagNonZeroCages == numberOfCages){
            System.out.println("bingo");            
        }

         System.out.println();
        return true;
    }
    return false;
}

现在我的方法被困在这里..我不知道怎么去笼子约束检查。这就是我的尝试,但不确定我错过了什么以及接下来该做什么。

1 个答案:

答案 0 :(得分:1)

/**
 * Cage constraint Check: Checks if num is an acceptable value for the
 * given Cage
 *
 * Precondition: Given cell is empty, and has passed rowConstraintCheck() and
 * columnConstraintCheck()
 */
public static boolean cageConstraintCheck(
        int rowIndex, int columnIndex, int num) {

    int cageIndex =  cellToCageMapper[rowIndex][columnIndex];          
    Cage cage = cages.get(cageIndex); // or whatever class-name you are using

    String currentOperator = cage.cageOperator;
    int targetValue = cage.cageValue;

    // Sum and product of all cells in cage, including the new one.
    int sum = num;
    int product = num;

    // Last non-zero value seen in the cage, not counting the new one.
    int last = -1;

    int numberOfEmptyCellsInCage = 0;
    int numberOfCellsInCage = cage.placeHolders.length;            

    if (numberOfCellsInCage == 1)
    {
        // Single-cell cage
        return (targetValue == num);
    }

    for (int j = 0; j < numberOfCellsInCage; j++) {
        int cellIndex = cage.placeHolders[j];
        int cellRow = (cellIndex / puzzleDimension); // Integer division
        int cellCol = (cellIndex % puzzleDimension);
        int cellValue = puzzleArray[cellRow][cellCol];
        if (cellValue == 0) {
            // Empty cell
            numberOfEmptyCellsInCage++;
        }
        else {
            // Update the tracking variables
            sum += cellValue;
            product *= cellValue;
            last = cellValue;
        }
    }

    if (numberOfEmptyCellsInCage == 1 && last != -1) {
        // The new number will be placed in the only empty spot in the cage.

        // For subtraction and division, there will only be two cells. Sort
        // their values onto 'low' and 'high'.
        int low = num < last ? num : last;
        int high = num + last - low;

        switch (currentOperator.charAt(0)) {
            case '+':
                if (targetValue != sum) {
                    // The new value would produce an incorrect sum
                    return false;
                }
                break;
            case '*':
                if (targetValue != product) {
                    // The new value would produce an incorrect product
                    reutrn false;
                }
                break;
            case '-':
                if (targetValue != high - low) {
                    // The new value would produce an incorrect difference
                    return false;
                }
                break;
            case '/':
                if (high % low != 0 || targetValue != high / low) {
                    // The new value would produce an incorrect quotient
                    return false;
                }
                break;
        }
    }

    return true;
}