Advertisement
Guest User

Untitled

a guest
Jan 17th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. using namespace std;
  6. #define UNASSIGNED 0
  7. #define N 9
  8.  
  9. bool FindUnassignedLocation(int grid[N][N], int &row, int &col);
  10. bool isSafe(int grid[N][N], int row, int col, int num);
  11.  
  12. /* assign values to all unassigned locations for Sudoku solution
  13. */
  14. bool SolveSudoku(int grid[N][N])
  15. {
  16. int row, col;
  17. if (!FindUnassignedLocation(grid, row, col))
  18. return true;
  19. for (int num = 1; num <= 9; num++)
  20. {
  21. if (isSafe(grid, row, col, num))
  22. {
  23. grid[row][col] = num;
  24. if (SolveSudoku(grid))
  25. return true;
  26. grid[row][col] = UNASSIGNED;
  27. }
  28. }
  29. return false;
  30. }
  31.  
  32. /* Searches the grid to find an entry that is still unassigned. */
  33. bool FindUnassignedLocation(int grid[N][N], int &row, int &col)
  34. {
  35. for (row = 0; row < N; row++)
  36. for (col = 0; col < N; col++)
  37. if (grid[row][col] == UNASSIGNED)
  38. return true;
  39. return false;
  40. }
  41.  
  42. /* Returns whether any assigned entry n the specified row matches
  43. the given number. */
  44. bool UsedInRow(int grid[N][N], int row, int num)
  45. {
  46. for (int col = 0; col < N; col++)
  47. if (grid[row][col] == num)
  48. return true;
  49. return false;
  50. }
  51.  
  52. /* Returns whether any assigned entry in the specified column matches
  53. the given number. */
  54. bool UsedInCol(int grid[N][N], int col, int num)
  55. {
  56. for (int row = 0; row < N; row++)
  57. if (grid[row][col] == num)
  58. return true;
  59. return false;
  60. }
  61.  
  62. /* Returns whether any assigned entry within the specified 3x3 box matches
  63. the given number. */
  64. bool UsedInBox(int grid[N][N], int boxStartRow, int boxStartCol, int num)
  65. {
  66. for (int row = 0; row < 3; row++)
  67. for (int col = 0; col < 3; col++)
  68. if (grid[row+boxStartRow][col+boxStartCol] == num)
  69. return true;
  70. return false;
  71. }
  72.  
  73. /* Returns whether it will be legal to assign num to the given row,col location.
  74. */
  75. bool isSafe(int grid[N][N], int row, int col, int num)
  76. {
  77. return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) &&
  78. !UsedInBox(grid, row - row % 3 , col - col % 3, num);
  79. }
  80.  
  81. /* print grid */
  82. void printGrid(int grid[N][N])
  83. {
  84. for (int row = 0; row < N; row++)
  85. {
  86. for (int col = 0; col < N; col++)
  87. cout<<grid[row][col]<<" ";
  88. cout<<endl;
  89. }
  90. }
  91.  
  92. /* Main */
  93. int main()
  94. {
  95. int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
  96. {5, 2, 0, 0, 0, 0, 0, 0, 0},
  97. {0, 8, 7, 0, 0, 0, 0, 3, 1},
  98. {0, 0, 3, 0, 1, 0, 0, 8, 0},
  99. {9, 0, 0, 8, 6, 3, 0, 0, 5},
  100. {0, 5, 0, 0, 9, 0, 6, 0, 0},
  101. {1, 3, 0, 0, 0, 0, 2, 5, 0},
  102. {0, 0, 0, 0, 0, 0, 0, 7, 4},
  103. {0, 0, 5, 2, 0, 6, 3, 0, 0}};
  104. if (SolveSudoku(grid) == true)
  105. printGrid(grid);
  106. else
  107. cout<<"No solution exists"<<endl;
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement