Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.chocosolver.samples;
- import org.chocosolver.solver.Model;
- import org.chocosolver.solver.Solution;
- import org.chocosolver.solver.variables.IntVar;
- public class NQueen {
- public void modelAndSolve(){
- int n = 6, roll = 29;
- int group[][] = {
- {0, 0, 3, 5, 5, 5},
- {0, 3, 3, 3, 5, 6},
- {0, 2, 3, 4, 5, 6},
- {0, 2, 4, 4, 4, 6},
- {1, 2, 2, 4, 7, 6},
- {1, 1, 1, 1, 7, 6}
- };
- int board[][] = {
- {0, 0, 0, 0, 0, 0},
- {0, 0, 0, 5, 0, 3},
- {5, 0, 0, 0, 0, 0},
- {0, 1, 0, 0, 0, 0},
- {5, 0, 0, 0, 0, 5},
- {0, 0, 0, 0, 0, 0}
- };
- int numGroup[] = {0, 0, 0, 0, 0, 0, 0, 0};
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- numGroup[group[i][j]]++;
- }
- }
- Model model = new Model("book-6 problem-14 suguru");
- IntVar[][] vars = new IntVar[n][n];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (board[i][j] != 0) {
- vars[i][j] = model.intVar("cell_" + i + "_" + j, board[i][j], board[i][j]);
- }
- else {
- vars[i][j] = model.intVar("cell_" + i + "_" + j, 1, numGroup[group[i][j]]);
- }
- }
- }
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- for (int k = 0; k < n; k++) {
- for (int l = 0; l < n; l++) {
- if (i == k && j == l) {
- continue;
- }
- if ((Math.abs(i - k) < 2 && Math.abs(j - l) < 2) || group[i][j] == group[k][l]) {
- model.arithm(vars[i][j], "!=",vars[k][l]).post();
- }
- }
- }
- }}
- Solution solution = model.getSolver().findSolution();
- if(solution != null){
- System.out.println(solution.toString());
- }
- }
- public static void main(String[] args) {
- new NQueen().modelAndSolve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement