Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Jeroen van Hoof
- * 13-15 September 2011
- * Cellulitis
- */
- import java.util.Scanner;
- class Sudoku {
- int SIZE = 9; // size of the grid
- int DMAX = 9; // maximal digit to be filled in
- int BOXSIZE = 3; // size of the boxes (subgrids that should contain all digits)
- int[][] grid; // the puzzle grid; 0 represents empty
- int solutionnr = 0; //solution counter
- // coordinates of next empty square
- int rempty;
- int cempty;
- // ----------------- conflict calculation --------------------
- // is there a conflict when we fill in d at position r,c?
- //@pre: 1<=n && n<=DMAX && grid[r][c]==0
- //@post: ...
- boolean givesConflict(int r, int c, int n) { // r = row, c = column, n = number
- if (rowConflict(r, n) || colConflict(c, n) || boxConflict(r, c, n)) {
- return true;
- } else {
- return false;
- }
- }
- //Is there a row conflict for value d in row r?
- //@ pre 1 <- d <= g && 1 <= r <= g
- //@ post return true iff grid grid[i][x] == d for some 1 <= for some 1 <= r <= g
- boolean rowConflict(int r, int n) {
- for (int i = 0; i < SIZE; i++){
- if (grid[i][r] == n){
- return true;
- }
- }
- return false;
- }
- boolean colConflict(int c, int n) {
- for (int i = 0; i < SIZE; i++){
- if (grid[c][i] == n){
- return true;
- }
- }
- return false;
- }
- boolean boxConflict(int r, int c, int n) {
- for (int i = 0; i < BOXSIZE; i++){
- for (int j = 0; j < BOXSIZE; j++){
- if (grid[(int) Math.floor(r/3) + i][(int) Math.floor(c/3) + j] == n) {
- return true;
- }
- }
- }
- return false;
- }
- // --------- solving ----------
- // finds the next empty square (in "reading order")
- // writes the coordinates in rempty and cempty
- // returns false if there is no empty square in the current grid
- boolean findEmptySquare() {
- for (int i = 0; i < SIZE; i++){
- for (int j = 0; j < SIZE; j++){
- if (grid[j][i] == 0) {
- cempty = j;
- rempty = i;
- return true;
- }
- }
- }
- return false;
- }
- // prints all solutions that are extensions of current grid
- // leaves grid in original state
- boolean solve() {
- if (!findEmptySquare()){
- print();
- return false;
- }
- else {
- for (int n = 1; n < 9; n++){
- if (!givesConflict(rempty, cempty, n)){
- grid[rempty][cempty] = n;
- if (solve()){
- return true;
- }
- }
- }
- grid[rempty][cempty] = 0; // reset on backtrack
- return false;
- }
- }
- // ------------------------- misc -------------------------
- // print the grid, 0s are printed as spaces
- void print() {
- String line = "";
- String line1 = "-------------------------";
- String line2 = "-------------------------";
- System.out.println(line2);
- for (int i = 0; i < SIZE; i++){
- if ( i%3 == 0 && i != 0){
- System.out.println(line1);
- }
- for (int j = 0; j < SIZE; j++){
- if ( j%3 == 0){
- line += "| ";
- }
- if (grid[i][j] == 0){
- line += ". ";
- }else{
- line += grid[i][j] + " ";
- }
- }
- System.out.println(line + "|");
- line = "";
- }
- System.out.println(line2);
- }
- // --------------- where it all starts --------------------
- void solveIt() {
- // I got some help here from Floris Schippers and Hein van Beers
- Scanner scanner = new Scanner(System.in);
- grid = new int[9][9];
- for(int i = 0; i < SIZE; i++){
- for(int j = 0; j < SIZE; j++){
- boolean StopLoop = false;
- while(!scanner.hasNextInt() && !StopLoop){
- if(".".equals(scanner.next())){
- StopLoop = true;
- }
- }
- if(StopLoop){
- grid[i][j] = 0;
- }else
- grid[i][j] = scanner.nextInt();
- }
- }
- if(solutionnr == 1){System.out.println("Found " + solutionnr + " solution");
- }else{System.out.println("Found " + solutionnr + " solutions");
- }
- }
- public static void main(String[] args) {
- new Sudoku().solveIt();
- new Sudoku().print();
- }
- }
Add Comment
Please, Sign In to add comment