Created
May 22, 2015 13:23
-
-
Save mayyuen318/b8d7cdf19d96c247ab4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Set; | |
public class Sudoku { | |
public static boolean checkRow(int[][] sudoku) { | |
for (int i = 0; i < 9; i++) { | |
Set<Integer> row = new HashSet<Integer>(); | |
for(int j = 0; j < 9; j++) { | |
if (sudoku[i][j] != 0) { | |
row.add(new Integer(sudoku[i][j])); | |
} | |
} | |
if (row.size() != 9) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean checkColumn(int [][] sudoku) { | |
for (int i = 0; i < 9; i++) { | |
Set<Integer> column = new HashSet<Integer>(); | |
for(int j = 0; j < 9; j++) { | |
if (sudoku[j][i] != 0) { | |
column.add(new Integer(sudoku[j][i])); | |
} | |
} | |
if (column.size() != 9) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean checkOneBox(int [][] sudoku, int a, int b) { | |
Set<Integer> box = new HashSet<Integer>(); | |
for (int i = a*3; i < a*3 + 3; i++) { | |
for(int j = b*3; j < b*3 + 3; j++) { | |
if (sudoku[i][j] != 0) { | |
box.add(new Integer(sudoku[i][j])); | |
} | |
} | |
} | |
if (box.size() != 9) { | |
return false; | |
} | |
return true; | |
} | |
public static boolean checkAllBoxes(int [][]sudoku) { | |
for (int a = 0; a < 3; a++) { | |
for (int b = 0; b < 3; b++){ | |
boolean checkAllBox = checkOneBox(sudoku, a, b); | |
if (!checkAllBox) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
int[][] sudoku = new int[][] { | |
{ 2, 1, 3, 4, 5, 0, 7, 9, 8 }, | |
{ 9, 0, 6, 0, 2, 7, 3, 0, 4 }, | |
{ 5, 4, 0, 3, 0, 9, 1, 2, 6 }, | |
{ 1, 0, 2, 0, 6, 4, 5, 7, 0 }, | |
{ 6, 7, 0, 5, 0, 3, 0, 4, 1 }, | |
{ 0, 3, 0, 0, 1, 0, 8, 0, 9 }, | |
{ 0, 6, 1, 0, 0, 5, 0, 8, 2 }, | |
{ 0, 0, 4, 6, 3, 0, 9, 0, 5 }, | |
{ 8, 0, 0, 0, 4, 1, 6, 3, 7 } | |
}; | |
int[][] correctSudoku = new int[][] { | |
{ 5, 3, 4, 6, 7, 8, 9, 1, 2 }, | |
{ 6, 7, 2, 1, 9, 5, 3, 4, 8 }, | |
{ 1, 9, 8, 3, 4, 2, 5, 6, 7 }, | |
{ 8, 5, 9, 7, 6, 1, 4, 2, 3 }, | |
{ 4, 2, 6, 8, 5, 3, 7, 9, 1 }, | |
{ 7, 1, 3, 9, 2, 4, 8, 5, 6 }, | |
{ 9, 6, 1, 5, 3, 7, 2, 8, 4 }, | |
{ 2, 8, 7, 4, 1, 9, 6, 3, 5 }, | |
{ 3, 4, 5, 2, 8, 6, 1, 7, 9 } | |
}; | |
for(int i=0; i<9; i++){ | |
for (int j=0; j<9; j++) { | |
if(sudoku[i][j]==0) | |
System.out.print(" "); | |
else | |
System.out.print(sudoku[i][j]); | |
if (j % 3 == 2 && j!=8) | |
System.out.print("|"); | |
if(j==8) | |
System.out.println(); | |
} | |
if(i%3 == 2 && i!=8) | |
System.out.println("---+---+---"); | |
} | |
boolean checkRow = checkRow(correctSudoku); | |
System.out.println("Row result:" + checkRow); | |
boolean checkColumn = checkColumn(correctSudoku); | |
System.out.println("Column result:" + checkColumn); | |
boolean checkAllBoxes = checkAllBoxes(correctSudoku); | |
System.out.println("All Boxes:" + checkAllBoxes); | |
} | |
}import java.util.HashSet; | |
import java.util.Set; | |
public class Sudoku { | |
public static boolean checkRow(int[][] sudoku) { | |
for (int i = 0; i < 9; i++) { | |
Set<Integer> row = new HashSet<Integer>(); | |
for(int j = 0; j < 9; j++) { | |
if (sudoku[i][j] != 0) { | |
row.add(new Integer(sudoku[i][j])); | |
} | |
} | |
if (row.size() != 9) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean checkColumn(int [][] sudoku) { | |
for (int i = 0; i < 9; i++) { | |
Set<Integer> column = new HashSet<Integer>(); | |
for(int j = 0; j < 9; j++) { | |
if (sudoku[j][i] != 0) { | |
column.add(new Integer(sudoku[j][i])); | |
} | |
} | |
if (column.size() != 9) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean checkOneBox(int [][] sudoku, int a, int b) { | |
Set<Integer> box = new HashSet<Integer>(); | |
for (int i = a*3; i < a*3 + 3; i++) { | |
for(int j = b*3; j < b*3 + 3; j++) { | |
if (sudoku[i][j] != 0) { | |
box.add(new Integer(sudoku[i][j])); | |
} | |
} | |
} | |
if (box.size() != 9) { | |
return false; | |
} | |
return true; | |
} | |
public static boolean checkAllBoxes(int [][]sudoku) { | |
for (int a = 0; a < 3; a++) { | |
for (int b = 0; b < 3; b++){ | |
boolean checkAllBox = checkOneBox(sudoku, a, b); | |
if (!checkAllBox) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
int[][] sudoku = new int[][] { | |
{ 2, 1, 3, 4, 5, 0, 7, 9, 8 }, | |
{ 9, 0, 6, 0, 2, 7, 3, 0, 4 }, | |
{ 5, 4, 0, 3, 0, 9, 1, 2, 6 }, | |
{ 1, 0, 2, 0, 6, 4, 5, 7, 0 }, | |
{ 6, 7, 0, 5, 0, 3, 0, 4, 1 }, | |
{ 0, 3, 0, 0, 1, 0, 8, 0, 9 }, | |
{ 0, 6, 1, 0, 0, 5, 0, 8, 2 }, | |
{ 0, 0, 4, 6, 3, 0, 9, 0, 5 }, | |
{ 8, 0, 0, 0, 4, 1, 6, 3, 7 } | |
}; | |
int[][] correctSudoku = new int[][] { | |
{ 5, 3, 4, 6, 7, 8, 9, 1, 2 }, | |
{ 6, 7, 2, 1, 9, 5, 3, 4, 8 }, | |
{ 1, 9, 8, 3, 4, 2, 5, 6, 7 }, | |
{ 8, 5, 9, 7, 6, 1, 4, 2, 3 }, | |
{ 4, 2, 6, 8, 5, 3, 7, 9, 1 }, | |
{ 7, 1, 3, 9, 2, 4, 8, 5, 6 }, | |
{ 9, 6, 1, 5, 3, 7, 2, 8, 4 }, | |
{ 2, 8, 7, 4, 1, 9, 6, 3, 5 }, | |
{ 3, 4, 5, 2, 8, 6, 1, 7, 9 } | |
}; | |
for(int i=0; i<9; i++){ | |
for (int j=0; j<9; j++) { | |
if(sudoku[i][j]==0) | |
System.out.print(" "); | |
else | |
System.out.print(sudoku[i][j]); | |
if (j % 3 == 2 && j!=8) | |
System.out.print("|"); | |
if(j==8) | |
System.out.println(); | |
} | |
if(i%3 == 2 && i!=8) | |
System.out.println("---+---+---"); | |
} | |
boolean checkRow = checkRow(correctSudoku); | |
System.out.println("Row result:" + checkRow); | |
boolean checkColumn = checkColumn(correctSudoku); | |
System.out.println("Column result:" + checkColumn); | |
boolean checkAllBoxes = checkAllBoxes(correctSudoku); | |
System.out.println("All Boxes:" + checkAllBoxes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment