Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sudoku {
- public static final int n = 3;
- public static final int n2 = n*n;
- private byte [][] board;
- public Sudoku()
- {
- board = new byte[n2][n2];
- for(int i = 0; i <n2; i++)
- {
- for(int j = 0; j <n2; j++)
- {
- board[i][j] = 0;
- }
- }
- }
- public String toString()
- {
- String result = "";
- for(int i = 0; i <n2; i++)
- {
- for(int j = 0; j <n2; j++)
- {
- result += board[i][j] + ",";
- }
- result += "\n";
- }
- return result;
- }
- public void fromString(String txt)
- {
- int k = 0;
- for(int i = 0; i <n2; i++)
- {
- for(int j = 0; j <n2; j++, k++)
- {
- board[i][j] = Byte.valueOf(txt.substring(k,k+1));
- }
- }
- }
- private static boolean isGroupLeegal(Byte group[])
- {
- boolean [] visited = new boolean[n2];
- for(int i = 0; i < n2; i++)
- {
- visited[i] = false;
- }
- for(int i = 0; i < group.length; i++)
- {
- if(group[i] == 0)
- {
- continue;
- }
- if(visited[group[i]-1]) {
- return false;
- }
- visited[group[i]-1] = true;
- }
- return true;
- }
- public boolean isBoardCorrect()
- {
- Byte[] group = new Byte[n2];
- //wiersze
- for(int i = 0; i < n2; i++)
- {
- for(int j = 0; j < n2; j++)
- {
- group[j]=board [i][j];
- }
- if(!isGroupLeegal(group))
- {
- return false;
- }
- }
- //kolumny
- for(int i = 0; i < n2; i++)
- {
- for(int j = 0; j < n2; j++)
- {
- group[j]=board [j][i];
- }
- if(!isGroupLeegal(group))
- {
- return false;
- }
- }
- //kwadrat
- return true;
- }
- public static void main(String[] args)
- {
- String sudokuValue = "003020600900305001001806400008102900700000008006708200002609500800203009005010300";
- Sudoku s = new Sudoku();
- s.fromString(sudokuValue);
- System.out.println(s);
- System.out.println(s.isBoardCorrect());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement