Advertisement
Guest User

Untitled

a guest
Dec 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. struct coord{
  5. int x;
  6. int y;
  7. };
  8.  
  9. /* structure of parameters for play_sudoku function */
  10. struct __sudoku_param {
  11. int grid[9][9];
  12. struct coord ulc[81]; /* ulc : unassigned 'location candidates' */
  13. int ulc_size;
  14. int ulc_idx; /* next ulc index */
  15. };
  16.  
  17. bool play_sudoku(struct __sudoku_param*);
  18. void find_avail_number(struct coord, struct __sudoku_param*, bool*);
  19. void print_grid(struct __sudoku_param);
  20.  
  21. int main(void)
  22. {
  23. struct __sudoku_param sudoku_param;
  24.  
  25. /* Initialization */
  26. int ulc_size = 0;
  27. for(int i=0; i<9; i++){
  28. for(int j=0; j<9; j++){
  29. scanf("%d",&sudoku_param.grid[i][j]);
  30.  
  31. if (!sudoku_param.grid[i][j]) {
  32. sudoku_param.ulc[ulc_size].x = i;
  33. sudoku_param.ulc[ulc_size].y = j;
  34. ulc_size ++;
  35. }
  36. }
  37. }
  38. sudoku_param.ulc_size = ulc_size;
  39. sudoku_param.ulc_idx = 0;
  40.  
  41. /* Run */
  42. play_sudoku(&sudoku_param);
  43.  
  44. /* Print the result */
  45. print_grid(sudoku_param);
  46.  
  47. return 0;
  48. }
  49.  
  50. // Backtracking
  51. bool play_sudoku(struct __sudoku_param* sudoku_param)
  52. {
  53. struct coord point;
  54.  
  55. if (sudoku_param->ulc_idx == sudoku_param->ulc_size)
  56. return true;
  57.  
  58. point.x = sudoku_param->ulc[sudoku_param->ulc_idx].x;
  59. point.y = sudoku_param->ulc[sudoku_param->ulc_idx].y;
  60. sudoku_param->ulc_idx++;
  61.  
  62. bool is_avail[10];
  63. for (int i = 1; i < 10; i++)
  64. is_avail[i] = true;
  65. find_avail_number(point, sudoku_param, is_avail);
  66.  
  67. for (int i = 1; i < 10; i++) {
  68. if(is_avail[i]) {
  69. sudoku_param->grid[point.x][point.y] = i;
  70. if(play_sudoku(sudoku_param)) return true;
  71. sudoku_param->grid[point.x][point.y] = 0;
  72. }
  73. }
  74.  
  75. sudoku_param->ulc_idx--;
  76. return false;
  77. }
  78.  
  79. /* Find available numbers at the point */
  80. void find_avail_number(struct coord point, struct __sudoku_param* sudoku_param, bool* is_avail)
  81. {
  82. for(int idx = 0; idx < 9; idx++){
  83. is_avail[sudoku_param->grid[idx][point.y]] = false;
  84. is_avail[sudoku_param->grid[point.x][idx]] = false;
  85. }
  86.  
  87. int start_x = (point.x/3)*3;
  88. int start_y = (point.y/3)*3;
  89. for(int i = 0; i < 3; i++){
  90. for(int j = 0; j < 3; j++){
  91. is_avail[sudoku_param->grid[start_x+i][start_y+j]] = false;
  92. }
  93. }
  94. }
  95.  
  96. void print_grid(struct __sudoku_param sudoku_param)
  97. {
  98. for(int i=0; i<9; i++){
  99. for(int j=0; j<9; j++){
  100. printf("%d ", sudoku_param.grid[i][j]);
  101. }
  102. printf("\n");
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement