Java 数独类谜题的框架约束

Java 数独类谜题的框架约束,java,algorithm,puzzle,sudoku,Java,Algorithm,Puzzle,Sudoku,嗨,我正在做一个解算器,这是一个类似数独的难题。我有一个笼子的结构,里面有很多细胞。每当尝试为框架设置值时,我都希望应用约束。为此,我每次都将roe/column/cage约束调用到我的谜题中 然而,我对这个问题感到震惊。这是我所有3个约束的代码。对于cage约束,我希望查看特定单元格cage的所有单元格,并查看通过的数量是否满足我们的标准 //Row Constraint Check: Checks if num is an acceptable value for the given

嗨,我正在做一个解算器,这是一个类似数独的难题。我有一个笼子的结构,里面有很多细胞。每当尝试为框架设置值时,我都希望应用约束。为此,我每次都将roe/column/cage约束调用到我的谜题中

然而,我对这个问题感到震惊。这是我所有3个约束的代码。对于cage约束,我希望查看特定单元格cage的所有单元格,并查看通过的数量是否满足我们的标准

    //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;
}
//行约束检查:检查num是否是给定行的可接受值
公共静态布尔rowConstraintCheck(int-rowIndex,int-num){
对于(int columnIndex=0;columnIndex/**
*框架约束检查:检查num是否为框架的可接受值
*给定的笼子
*
*前提条件:给定单元格为空,已传递rowConstraintCheck()和
*ColumnContcheck()
*/
公共静态布尔值检查(
int行索引,int列索引,int num){
int cageIndex=CellToManageMapper[rowIndex][columnIndex];
Cage Cage=cages.get(cageIndex);//或使用的任何类名
字符串currentOperator=cage.cageOperator;
int targetValue=cage.cageValue;
//笼中所有单元格(包括新单元格)的总和和乘积。
int sum=num;
int乘积=num;
//在框架中看到的最后一个非零值,不包括新值。
int last=-1;
int numberOfEmptyCellsInCage=0;
int numberOfCellsInCage=cage.placeHolders.length;
如果(numberOfCellsInCage==1)
{
//单细胞笼
返回值(targetValue==num);
}
对于(int j=0;j
您目前遇到的实际问题是什么?您粘贴的代码如何无法满足您的要求?@AndrzejDoyle很抱歉描述得不够详细..添加了更多详细信息..基本上我被卡住了..不知道该做什么和如何做..我只是在这里添加了我的尝试…这仍然没有说明什么。您的代码怎么样ling,关于什么类型的输入?你得到了什么结果,你期望得到什么?目前我不知道你的代码是什么(我不知道
celltocatemapper
包含什么,或者
占位符
字段是用来做什么的),我也不知道这意味着什么。你需要解释这两个问题才能回答问题。我可以在这里发布我的完整程序吗?你可以发布它,但不要期望它有多大帮助。目前还不清楚你有什么定义明确的问题需要帮助解决。
/**
 * 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;
}