fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. bool checkArray(int arr[]) {
  5. bool found[9] = {false};
  6. for (int i = 0; i < 9; i++) {
  7. if (arr[i] < 1 || arr[i] > 9) return false;
  8. if (found[arr[i] - 1]) return false;
  9. found[arr[i] - 1] = true;
  10. }
  11. return true;
  12. }
  13.  
  14. bool checkSubgrids(int grid[9][9]) {
  15. for (int row = 0; row < 9; row += 3) {
  16. for (int col = 0; col < 9; col += 3) {
  17. int subgrid[9], idx = 0;
  18. for (int i = 0; i < 3; i++) {
  19. for (int j = 0; j < 3; j++) {
  20. subgrid[idx++] = grid[row + i][col + j];
  21. }
  22. }
  23. if (!checkArray(subgrid)) return false;
  24. }
  25. }
  26. return true;
  27. }
  28.  
  29. int main() {
  30. int sudoku[9][9];
  31. for (int i = 0; i < 9; i++) {
  32. for (int j = 0; j < 9; j++) {
  33. cin >> sudoku[i][j];
  34. }
  35. }
  36. for (int i = 0; i < 9; i++) {
  37. if (!checkArray(sudoku[i])) {
  38. cout << "incorect" << "\n";
  39. return 0;
  40. }
  41. }
  42. for (int j = 0; j < 9; j++) {
  43. int column[9];
  44. for (int i = 0; i < 9; i++) {
  45. column[i] = sudoku[i][j];
  46. }
  47. if (!checkArray(column)) {
  48. cout << "incorect" << "\n";
  49. return 0;
  50. }
  51. }
  52. if (!checkSubgrids(sudoku)) {
  53. cout << "incorect" << "\n";
  54. return 0;
  55. }
  56. cout << "corect" << "\n";
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5284KB
stdin
3 6 9 1 2 4 5 8 7
7 2 8 6 5 9 3 1 4
1 4 5 7 3 8 2 6 9
2 9 7 3 6 1 8 4 5
5 8 3 9 4 2 6 7 1
6 1 4 5 8 7 9 2 3
9 7 2 8 1 5 4 3 6
4 5 6 2 7 3 1 9 8
8 3 9 4 9 6 7 5 2
stdout
incorect