luishenriique

Sudoku Verifier

Jan 23rd, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package sudoku;
  2.  
  3. public class Sudoku {
  4.  
  5.     public static void main(String[] args) {
  6.         int[][] s1 = {
  7.             {1, 3, 2, 5, 7, 9, 4, 6, 8},
  8.             {4, 9, 8, 2, 6, 1, 3, 7, 5},
  9.             {7, 5, 6, 3, 8, 4, 2, 1, 9},
  10.             {6, 4, 3, 1, 5, 8, 7, 9, 2},
  11.             {5, 2, 1, 7, 9, 3, 8, 4, 6},
  12.             {9, 8, 7, 4, 2, 6, 5, 3, 1},
  13.             {2, 1, 4, 9, 3, 5, 6, 8, 7},
  14.             {3, 6, 5, 8, 1, 7, 9, 2, 4},
  15.             {8, 7, 9, 6, 4, 2, 1, 5, 3}
  16.         };
  17.         System.out.println(verificaSudoku(s1));
  18.        
  19.         // Coluna 8, valor 3 repetido
  20.         int[][] s2 = {
  21.             {1, 3, 2, 5, 7, 9, 4, 6, 8},
  22.             {4, 9, 8, 2, 6, 1, 3, 7, 5},
  23.             {7, 5, 6, 3, 8, 4, 2, 1, 9},
  24.             {6, 4, 3, 1, 5, 8, 7, 9, 2},
  25.             {5, 2, 1, 7, 9, 3, 8, 4, 6},
  26.             {9, 8, 7, 4, 2, 6, 5, 3, 1},
  27.             {2, 1, 4, 9, 3, 5, 6, 8, 7},
  28.             {3, 6, 5, 8, 1, 7, 9, 2, 4},
  29.             {8, 7, 9, 6, 4, 2, 1, 3, 5}
  30.         };
  31.         System.out.println(verificaSudoku(s2));
  32.        
  33.         // Bloco 1, valor 4 repetido
  34.         // Valor 0 na linha 4 para não falhar no teste de linha e coluna
  35.         int[][] s3 = {
  36.             {1, 3, 2, 5, 7, 9, 4, 6, 8},
  37.             {4, 9, 8, 2, 6, 1, 3, 7, 5},
  38.             {7, 4, 6, 3, 8, 0, 2, 1, 9},
  39.             {6, 0, 3, 1, 5, 8, 7, 9, 2},
  40.             {5, 2, 1, 7, 9, 3, 8, 4, 6},
  41.             {9, 8, 7, 4, 2, 6, 5, 3, 1},
  42.             {2, 1, 4, 9, 3, 5, 6, 8, 7},
  43.             {3, 6, 5, 8, 1, 7, 9, 2, 4},
  44.             {8, 7, 9, 6, 4, 2, 1, 5, 3}
  45.         };
  46.         System.out.println(verificaSudoku(s3));
  47.     }
  48.    
  49.     private static boolean verificaSudoku(int[][] m){
  50.         return verificaHorizontais(m) && veirifcaVerticais(m) && verificaBlocos(m);
  51.     }
  52.    
  53.     private static boolean verificaHorizontais(int[][] m){
  54.         for(int[] l : m)
  55.             if(!verificaVetor(l))
  56.                 return false;
  57.         return true;
  58.     }
  59.    
  60.     private static boolean veirifcaVerticais(int[][] m){
  61.         int[] l = new int[9];
  62.         for(int j = 0; j < m[0].length; j++){
  63.             for(int i = 0; i < m.length; i++)
  64.                 l[i] = m[i][j];
  65.             if(!verificaVetor(l))
  66.                 return false;
  67.         }
  68.         return true;
  69.     }
  70.    
  71.     private static boolean verificaBlocos(int[][] m){
  72.         int[] l = new int[9];
  73.         for(int j = 0; j < 3; j++)
  74.             for(int i = 0; i < 3; i++){
  75.                 for(int z = 0; z < 3; z++)
  76.                     System.arraycopy(m[z+i*3], j*3, l, z*3, 3);
  77.                 if(!verificaVetor(l))
  78.                     return false;
  79.             }
  80.         return true;
  81.     }
  82.    
  83.     private static boolean verificaVetor(int[] v){
  84.         for(int i = 0; i < v.length; i++)
  85.             for(int j = i + 1; j < v.length; j++)
  86.                 if(v[i] == v[j])
  87.                     return false;
  88.         return true;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment