diff options
author | InigoGutierrez <inigogf.95@gmail.com> | 2019-12-25 20:18:48 +0100 |
---|---|---|
committer | InigoGutierrez <inigogf.95@gmail.com> | 2019-12-25 20:18:48 +0100 |
commit | a3b244cb98c455c2c88ee17b8424fa6fa8cf805d (patch) | |
tree | ac86c8f14cb511602a9fece8c6547115eda4c6c3 /c | |
parent | 28f18e1a3bbb833e9a18725acb7d4b261e53c9d4 (diff) | |
download | AceptaElReto-a3b244cb98c455c2c88ee17b8424fa6fa8cf805d.tar.gz AceptaElReto-a3b244cb98c455c2c88ee17b8424fa6fa8cf805d.zip |
Resuelto 345 en c++: Sudokus correctos.
Diffstat (limited to 'c')
-rw-r--r-- | c/345_sudokusCorrectos.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/c/345_sudokusCorrectos.cpp b/c/345_sudokusCorrectos.cpp new file mode 100644 index 0000000..37b078c --- /dev/null +++ b/c/345_sudokusCorrectos.cpp @@ -0,0 +1,77 @@ +#include <iostream> + +int sudoku[9][9]; +int casos; +int row, col; +int checker[9]; + +void cleanChecker() { + int i; + for (i = 0; i < 9; i++) { + checker[i] = 0; + } +} + +int touchChecker(int row, int col) { + if (checker[sudoku[row][col]-1]) + return 1; + checker[sudoku[row][col]-1] = 1; + return 0; +} + +// Devuelve 1 si encuentra error, 0 si no +int region(int startRow, int startCol) { + int row, col; + cleanChecker(); + for (row = startRow; row < startRow + 3; row++) { + for (col = startCol; col < startCol + 3; col++) { + if (touchChecker(row, col)) + return 1; + } + } + return 0; +} + +std::string computeCase() { + // Leer caso + for (row = 0; row < 9; row++) { + for (col = 0; col < 9; col++) { + std::cin >> sudoku[row][col]; + } + } + + // Computar caso + // Filas + for (row = 0; row < 9; row++) { + cleanChecker(); + for (col = 0; col < 9; col++) { + if (touchChecker(row, col)) { + return "NO"; + } + } + } + // Columnas + for (col = 0; col < 9; col++) { + cleanChecker(); + for (row = 0; row < 9; row++) { + if (touchChecker(row, col)) { + return "NO"; + } + } + } + // Celdas 3x3 + for (row = 0; row < 9; row+=3) { + for (col = 0; col < 9; col+=3) { + if (region(row, col)) { + return "NO"; + } + } + } + return "SI"; +} + +int main(void) { + for (std::cin >> casos; casos > 0; casos--) { + std::cout << computeCase() << std::endl; + } +} |