Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class SudokuCSP {
  4.     public int[][] board = boardSetUp();
  5.     public int count = 0;
  6.     public ArrayList<int[][]> boardList = new ArrayList<int[][]>();
  7.  
  8.     public static void main(String[] args) {
  9.         SudokuCSP sudoku = new SudokuCSP();
  10.         sudoku.commitSudoku();
  11.     }
  12.  
  13.     public SudokuCSP() {
  14.     }
  15.  
  16.     public int[][] boardSetUp() {
  17.         int[][] board2 = new int[9][9];
  18.         board2[0][1] = 9;
  19.         board2[0][7] = 2;
  20.         board2[0][8] = 5;
  21.         board2[1][0] = 4;
  22.         board2[1][2] = 5;
  23.         board2[1][3] = 2;
  24.         board2[2][1] = 6;
  25.         board2[2][4] = 3;
  26.         board2[3][0] = 2;
  27.         board2[3][1] = 8;
  28.         board2[3][3] = 3;
  29.         board2[3][6] = 1;
  30.         board2[3][8] = 9;
  31.         board2[4][3] = 8;
  32.         board2[4][5] = 1;
  33.         board2[5][0] = 3;
  34.         board2[5][2] = 7;
  35.         board2[5][5] = 6;
  36.         board2[5][7] = 4;
  37.         board2[5][8] = 8;
  38.         board2[6][4] = 1;
  39.         board2[6][7] = 8;
  40.         board2[7][5] = 3;
  41.         board2[7][6] = 7;
  42.         board2[7][8] = 2;
  43.         board2[8][0] = 6;
  44.         board2[8][1] = 3;
  45.         board2[8][7] = 9;
  46.  
  47.         return board2;
  48.     }
  49.  
  50.     public boolean checkRows(int value, int xPos, int yPos, int[][] b) {
  51.         for (int i = 0; i < 9; i++) {
  52.             if (b[yPos][i] == value && xPos != i)
  53.                 return false;
  54.         }
  55.         return true;
  56.  
  57.     }
  58.  
  59.     public boolean checkCols(int value, int xPos, int yPos, int[][] b) {
  60.         for (int i = 0; i < 9; i++) {
  61.             if (b[i][xPos] == value && yPos != i)
  62.                 return false;
  63.         }
  64.         return true;
  65.     }
  66.  
  67.     public boolean checkSquare(int value, int xPos, int yPos, int[][] b) {
  68.         for (int x = xPos / 3 * 3; x < xPos / 3 * 3; x++) {
  69.             for (int y = yPos / 3 * 3; y < yPos / 3 * 3; y++) {
  70.                 if (x != xPos && y != yPos && value == b[y][x]) {
  71.                     return false;
  72.                 }
  73.             }
  74.         }
  75.         return true;
  76.     }
  77.  
  78.     public boolean isLegalBoard(int value, int xPos, int yPos, int[][] b) {
  79.         return checkRows(value, xPos, yPos, b)
  80.                 && checkCols(value, xPos, yPos, b)
  81.                 && checkSquare(value, xPos, yPos, b);
  82.     }
  83.  
  84.     public int[] getNextPos(int b[][]) {
  85.         int[] pos = new int[2];
  86.         for (int x = 0; x < 9; x++) {
  87.             for (int y = 0; y < 9; y++) {
  88.                 if (b[y][x] == 0) {
  89.                     pos[0] = y;
  90.                     pos[1] = x;
  91.                     return pos;
  92.                 }
  93.             }
  94.         }
  95.         pos[0] = 10;
  96.         pos[1] = 10;
  97.         return pos;
  98.     }
  99.  
  100.     public boolean isGoal(int[] pos) {
  101.         if (pos[0] == 10 && pos[1] == 10)
  102.             return true;
  103.         return false;
  104.     }
  105.  
  106.     public void generateBoards(int[][] b, int[] pos) {
  107.         for (int i = 1; i < 10; i++) {
  108.             int[][] nb = generateDuplicate(b);
  109.             nb[pos[0]][pos[1]] = i;
  110.             if (isLegalBoard(i, pos[1], pos[0], nb)) {
  111.                 boardList.add(0, nb);
  112.                 count++;
  113.             }
  114.         }
  115.     }
  116.  
  117.     public static int[][] generateDuplicate(int[][] b){
  118.         int[][] nb = new int[9][9];
  119.         for(int y=0;y<9;y++){
  120.             for(int x=0;x<9;x++){
  121.                 nb[x][y] = b[x][y];
  122.             }
  123.         }
  124.         return nb;
  125.     }
  126.    
  127.     public void commitSudoku() {
  128.         boardList.add(0, board);
  129.         int[] pos = getNextPos(board);
  130.         while (!boardList.isEmpty() && !isGoal(pos)) {
  131.             int[][] newBoard = boardList.remove(0);
  132.             generateBoards(newBoard, pos);
  133.             pos = getNextPos(boardList.get(0));
  134.  
  135.         }
  136.         for (int x = 0; x < 9; x++) {
  137.             System.out.println();
  138.             for (int y = 0; y < 9; y++) {
  139.                 System.out.print("" + boardList.get(0)[x][y] + " ");
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement