Skip to content

Instantly share code, notes, and snippets.

@arthurxavierx
Last active December 17, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurxavierx/5630768 to your computer and use it in GitHub Desktop.
Save arthurxavierx/5630768 to your computer and use it in GitHub Desktop.
Sudoku verifier
/**
* sudoku.c
* @author Arthur Xavier <arthur.xavierx@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
size_t i, j;
ushort linhas, colunas, quadrados;
ushort sudoku[9][9];
bool valido = true;
for(i = 0; i < 9; i++)
for(j = 0; j < 9; j++)
scanf("%hu", sudoku[i] + j);
for(i = 0; i < 9; i++) {
for(j = 0, linhas = 0, colunas = 0, quadrados = 0; j < 9; j++) {
linhas |= 1 << (sudoku[i][j] - 1);
colunas |= 1 << (sudoku[j][i] - 1);
quadrados |= 1 << (sudoku[(i/3)*3 + j/3][(i%3)*3 + j%3] - 1);
}
valido &= (linhas == 511) && (colunas == 511) && (quadrados == 511);
}
printf(valido ? "Solucao atende as regras!\n" : "Solucao nao atende as regras!\n");
return 0;
}
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
4 6 5 9 2 3 8 1 7
7 3 8 4 1 5 6 9 2
2 9 1 7 6 8 3 4 5
5 7 6 3 9 1 4 2 8
8 4 9 5 7 2 1 3 6
1 2 3 8 4 6 5 7 9
3 8 7 1 5 9 2 6 4
9 5 2 6 3 4 7 8 1
6 1 4 2 8 7 9 5 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment