Advertisement
Wazedrifat

CSP suguru

Jul 11th, 2020
1,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. package org.chocosolver.samples;
  2. import org.chocosolver.solver.Model;
  3. import org.chocosolver.solver.Solution;
  4. import org.chocosolver.solver.variables.IntVar;
  5.  
  6. public class NQueen {
  7.    
  8.     public void modelAndSolve(){
  9.         int n = 6, roll = 29;
  10.         int group[][] = {
  11.             {0, 0, 3, 5, 5, 5},
  12.             {0, 3, 3, 3, 5, 6},
  13.             {0, 2, 3, 4, 5, 6},
  14.             {0, 2, 4, 4, 4, 6},
  15.             {1, 2, 2, 4, 7, 6},
  16.             {1, 1, 1, 1, 7, 6}
  17.         };
  18.         int board[][] = {
  19.             {0, 0, 0, 0, 0, 0},
  20.             {0, 0, 0, 5, 0, 3},
  21.             {5, 0, 0, 0, 0, 0},
  22.             {0, 1, 0, 0, 0, 0},
  23.             {5, 0, 0, 0, 0, 5},
  24.             {0, 0, 0, 0, 0, 0}
  25.         };
  26.         int numGroup[] = {0, 0, 0, 0, 0, 0, 0, 0};
  27.        
  28.         for (int i = 0; i < n; i++) {
  29.             for (int j = 0; j < n; j++) {
  30.                 numGroup[group[i][j]]++;
  31.             }
  32.         }
  33.        
  34.         Model model = new Model("book-6 problem-14 suguru");
  35.         IntVar[][] vars = new IntVar[n][n];
  36.        
  37.         for (int i = 0; i < n; i++) {
  38.             for (int j = 0; j < n; j++) {
  39.                 if (board[i][j] != 0) {
  40.                     vars[i][j] = model.intVar("cell_" + i + "_" + j, board[i][j], board[i][j]);
  41.                 }
  42.                 else {
  43.                     vars[i][j] = model.intVar("cell_" + i + "_" + j, 1, numGroup[group[i][j]]);
  44.                 }
  45.             }
  46.         }
  47.        
  48.         for(int i = 0; i < n; i++){
  49.         for(int j = 0; j < n; j++){
  50.             for (int k = 0; k < n; k++) {
  51.             for (int l = 0; l < n; l++) {
  52.                 if (i == k && j == l) {
  53.                     continue;
  54.                 }
  55.                
  56.                 if ((Math.abs(i - k) < 2 && Math.abs(j - l) < 2) || group[i][j] == group[k][l]) {
  57.                     model.arithm(vars[i][j], "!=",vars[k][l]).post();
  58.                 }
  59.             }
  60.             }      
  61.         }}
  62.                
  63.         Solution solution = model.getSolver().findSolution();
  64.         if(solution != null){
  65.             System.out.println(solution.toString());
  66.         }
  67.     }
  68.  
  69.     public static void main(String[] args) {
  70.         new NQueen().modelAndSolve();
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement