2012-10-08 6 views
0

안녕하세요 저는 스도쿠와 같은 퍼즐 인 kenken 솔버를 만들고 있습니다. 케이지에 셀 수를 가진 새장 구조가 있습니다. 우리가 새장 값을 시도 할 때마다 제약 조건을 적용하고 싶습니다. 이것을 위해 나는 항상 내 퍼즐에 로우/컬럼/케이지 구속을 부른다.스도쿠 유형 퍼즐을위한 케이지 제약

그러나 나는 문제에 대한 케이지 제약에 부딪혔다. 다음은 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; 
} 

나는 이제 나의 접근법에 갇혀있다. 나는 케이지 제약 검사를하는 방법을 모른다. 이것은 내가 시도한 것이지만, 나는 무엇을 놓치고 다음에 무엇을 해야할지 확신하지 못한다.

+0

현재 실제 문제는 무엇입니까? 붙여 넣은 코드가 요구 사항을 어떻게 충족시키지 못합니까? –

+0

@AndrzejDoyle 덜 설명하는 것에 대해 유감스럽게 생각합니다 .. 기본적으로 나는 갇혀 있습니다 .. 어떻게 해야할지와 어떻게해야할지 모르겠군요 ... 방금 여기에 내 시도를 추가했습니다 ... – CodeMonkey

+2

아직 그 말은하지 않습니다. ** 입력의 유형에 따라 코드가 어떻게 실패합니까? 어떤 결과를 얻고 있으며 무엇을 기대 했습니까? 지금은 코드가 무엇인지 알 수 없습니다 ('cellToCageMapper'에 무엇이 들어 있는지,'placeHolders' 필드가 무엇인지 알지 못합니다). 그리고 그 코드가 의미하는 바를 알지도 못합니다. 질문을 답할 수 있도록하기 위해이 두 가지를 모두 설명해야합니다. –

답변

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; 
}