0

こんにちは私は数独のようなパズルであるケンケンソルバーを作っています。私はケージのセルの数を持っているケージ構造を持っています。ケージの値を試しているときはいつでも制約を適用したいと思います。このために、私は毎回パズルに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;
}

今、私は私のアプローチでここで立ち往生しています..私はケージの制約チェックに行く方法がわかりません。これは私が試みたものですが、何が欠けているのか、次に何をすべきかわかりません。

4

1 に答える 1

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;
}
于 2012-10-08T16:58:35.113 に答える