嗨,我正在制作 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;
}
现在我被困在我的方法中..我不知道如何进行笼式约束检查。这是我尝试过的,但不确定我缺少什么以及下一步该做什么。
最佳答案
/**
* 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;
}
关于java - 数独类谜题的笼式约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780958/