Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // This program currently finds all valid 4x4 Sudoku grids
  2.  
  3. // There are 288 possible SOLUTIONS to this problem!
  4. // If you just randomly put values 1-4 into each of the 16 squares,
  5. // there would be 4,294,967,296 POSSIBILITES to try!
  6. // This recursive method only searches 9,860 of the possibilities!
  7.  
  8. // ----------
  9. // ASSIGNMENT
  10. // ----------
  11. // 1) Convert the code below to find and print out valid 9x9 grids
  12.  
  13. // If you just randomly put values 1-9 into each of the 81 squares,
  14. // there would be 1.96627e77 POSSIBILITES to try!
  15.  
  16. // There are 6,670,903,752,021,072,936,960 SOLUTIONS so as long
  17. // each output is a valid grid then you have done your job
  18.  
  19. // 2) Find the solution to this specific 9x9 Sudoku puzzle:
  20. // 8 7 6 9 0 0 0 0 0
  21. // 0 1 0 0 0 6 0 0 0
  22. // 0 4 0 3 0 5 8 0 0
  23. // 4 0 0 0 0 0 2 1 0
  24. // 0 9 0 5 0 0 0 0 0
  25. // 0 5 0 0 4 0 3 0 6
  26. // 0 2 9 0 0 0 0 0 8
  27. // 0 0 4 6 9 0 1 7 3
  28. // 0 0 0 0 0 1 0 0 4
  29.  
  30. #include <iostream>
  31. #include <vector>
  32.  
  33. void PrintFourByFourGrid(const std::vector<int>& grid)
  34. {
  35. for (int i = 0; i < 9; ++i)
  36. {
  37. for (int j = 0; j < 9; ++j)
  38. {
  39. std::cout << grid[(i*9) + j] << " ";
  40. }
  41. std::cout << std::endl;
  42. }
  43. std::cout << std::endl;
  44. }
  45.  
  46. bool IsLegalRowColumnWhatever(const std::vector<int>& vector)
  47. {
  48. std::vector<int> occurrences(vector.size() + 1);
  49.  
  50. try
  51. {
  52. for (const int& i : vector)
  53. {
  54. ++occurrences.at(i);
  55. }
  56. }
  57.  
  58. catch (const std::exception&)
  59. {
  60. return false;
  61. }
  62.  
  63. for (std::size_t i = 1; i < occurrences.size(); ++i)
  64. {
  65. if (occurrences[i] > 1)
  66. {
  67. return false;
  68. }
  69. }
  70.  
  71. return true;
  72. }
  73.  
  74. bool IsFourByFourSudokuLegal(const std::vector<int>& grid)
  75. {
  76. int rows[] = { 0, 9, 18, 27, 36, 45, 54, 63, 72 };
  77. for (const int& i : rows)
  78. {
  79. std::vector<int> v{ grid[i], grid[i + 1], grid[i + 2], grid[i + 3], grid[i+4], grid[i+5], grid[i+6], grid[i+7], grid[i+8] };
  80. if (IsLegalRowColumnWhatever(v) == false)
  81. {
  82. return false;
  83. }
  84. }
  85.  
  86. int columns[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  87. for (const int& i : columns)
  88. {
  89. std::vector<int> v{ grid[i], grid[i + 9], grid[i + 18], grid[i + 27], grid[i + 36], grid[i + 45], grid[i + 54], grid[i + 63], grid[i + 72] };
  90. if (IsLegalRowColumnWhatever(v) == false)
  91. {
  92. return false;
  93. }
  94. }
  95.  
  96. int squares[] = { 0, 3, 6, 27, 30, 33, 54, 57, 60 };
  97. for (const int& i : squares)
  98. {
  99. std::vector<int> v{ grid[i], grid[i + 1], grid[i + 2], grid[i + 9], grid[i + 10], grid[i + 11], grid[i + 18], grid[i + 19], grid[i + 20] };
  100. if (IsLegalRowColumnWhatever(v) == false)
  101. {
  102. return false;
  103. }
  104. }
  105.  
  106. return true;
  107. }
  108.  
  109. void GenerateValidFourByFourGrids(std::vector<int>& grid, int index)
  110. {
  111. if (index == 81)
  112. {
  113. PrintFourByFourGrid(grid);
  114. }
  115. else
  116. {
  117. for (int i = 1; i <= 9; ++i)
  118. {
  119. grid[index] = i;
  120. if (IsFourByFourSudokuLegal(grid) == true)
  121. {
  122. GenerateValidFourByFourGrids(grid, index + 1);
  123. }
  124. }
  125. grid[index] = 0;
  126. }
  127. }
  128.  
  129. void GenerateAllValidFourByFourGrids()
  130. {
  131. std::vector<int> grid(81, 0);
  132. GenerateValidFourByFourGrids(grid, 0);
  133. }
  134.  
  135. int main()
  136. {
  137. GenerateAllValidFourByFourGrids();
  138. system("PAUSE");
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement