Skip to content

Instantly share code, notes, and snippets.

@jronal
Created July 6, 2017 03:37
Show Gist options
  • Select an option

  • Save jronal/174c1ba9a1deb489df7701a2f2077d56 to your computer and use it in GitHub Desktop.

Select an option

Save jronal/174c1ba9a1deb489df7701a2f2077d56 to your computer and use it in GitHub Desktop.
sudoku codefights
boolean sudoku2(char[][] grid) {
int n = grid.length;
int[] filaVerifica = new int[n];
int[] columnaVerfica = new int[n];
int[] escaque = new int[n];
for (int i = 0; i < n; i++) {
Arrays.fill(filaVerifica, 0);
Arrays.fill(columnaVerfica, 0);
Arrays.fill(escaque, 0);
for (int j = 0; j < n; j++) {
// verificar filas
if (grid[i][j] != '.') {
int index = Character.getNumericValue(grid[i][j]);
filaVerifica[index - 1] += 1;
}
// verificar columnas
if (grid[j][i] != '.') {
int index = Character.getNumericValue(grid[j][i]);
columnaVerfica[index - 1] += 1;
}
// verificar escaques
if (grid[j / 3 + (i / 3) * 3][j % 3 + (i % 3) * 3] != '.') {
int index = Character.getNumericValue(grid[j / 3 + (i / 3) * 3][j % 3 + (i % 3) * 3]);
escaque[index - 1] += 1;
}
}
for (int z = 0; z < n; z++) {
if (filaVerifica[z] > 1) return false;
if (columnaVerfica[z] > 1) return false;
if (escaque[z] > 1) return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment