Advertisement
0705danny

Untitled

Oct 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include "sudoku.h"
  2.  
  3. //-------------------------------------------------------------------------------------------------
  4. // Start here to work on your MP7
  5. //-------------------------------------------------------------------------------------------------
  6. // Partners : slee528, jseo35, taehyun4
  7. // This week's MP is about making a sudoku using a 2-D array functions.
  8. // Each cell contains a number from 1 to 9 and each number can only be appeared only once.
  9. // We used the pseudocode of a typical backtracking algorithm to solve for sudoku puzzle.
  10. // Each function represents specific tasks for successfully using backtracking to solve for parse_sudoku
  11. // and row, col, 3x3, is valid, solve functions present specific areas that programs should implement.
  12.  
  13. // You are free to declare any private functions if needed.
  14.  
  15. // Function: is_val_in_row
  16. // Return true if "val" already existed in ith row of array sudoku.
  17. int is_val_in_row(const int val, const int i, const int sudoku[9][9]) {
  18.  
  19. assert(i>=0 && i<9);
  20. int a=0;
  21. int count=0;
  22. for(a=0; a<9;a++){
  23. if(sudoku[i][a]==val)
  24. count++;
  25. }
  26. if (count > 0)
  27. return 1;
  28. else
  29. return 0;
  30. // END TODO
  31. }
  32.  
  33. // Function: is_val_in_col
  34. // Return true if "val" already existed in jth column of array sudoku.
  35. int is_val_in_col(const int val, const int j, const int sudoku[9][9]) {
  36.  
  37. assert(j>=0 && j<9);
  38. int a=0;
  39. int count=0;
  40. for(a=0; a<9;a++){
  41. if(sudoku[a][j]==val)
  42. count++;
  43. }
  44. if (count > 0)
  45. return 1;
  46. else
  47. return 0;
  48. // END TODO
  49. }
  50.  
  51. // Function: is_val_in_3x3_zone
  52. // Return true if val already existed in the 3x3 zone corresponding to (i, j)
  53. int is_val_in_3x3_zone(const int val, const int i, const int j, const int sudoku[9][9]) {
  54.  
  55. assert(i>=0 && i<9);
  56. int a, b, c, d;
  57. a = i - i%3;
  58. b = j - j%3;
  59. for (c = 0; c< 3; c++){
  60. for (d = 0; d< 3; d++){
  61. if (sudoku[a+c][b+d] == val)
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. // END TODO
  67. }
  68.  
  69. // Function: is_val_valid
  70. // Return true if the val is can be filled in the given entry.
  71. int is_val_valid(const int val, const int i, const int j, const int sudoku[9][9]) {
  72.  
  73. assert(i>=0 && i<9 && j>=0 && j<9);
  74.  
  75. if(!is_val_in_3x3_zone(val, i, j, sudoku) && !is_val_in_col(val, j, sudoku) && !is_val_in_row(val, i, sudoku))
  76. return 1;
  77.  
  78. else
  79. return 0;
  80. // END TODO
  81. }
  82.  
  83. int is_full(int sudoku[9][9])
  84. {
  85. int a,b;
  86. for(a=0;a<9;a++)
  87. {
  88. for(b=0;b<9;b++)
  89. {
  90. if(sudoku[a][b]==0)
  91. return(0);
  92. }
  93. }
  94. return(1);
  95. }
  96.  
  97. // Procedure: solve_sudoku
  98. // Solve the given sudoku instance.
  99. int solve_sudoku(int sudoku[9][9]) {
  100.  
  101. int a, b, i, j;
  102. for (a = 0; a<9; a++){
  103. for(b = 0; b<9; b++){
  104. if (sudoku[a][b] == 0){
  105. printf("a:%d b:%d\n",a,b);
  106. i = a;
  107. j = b;
  108. }
  109. }
  110. }
  111. if(is_full(sudoku))
  112. return(1);
  113.  
  114. int num;
  115. for (num = 1; num <= 9; num++){
  116. if (is_val_valid(num, i, j, sudoku)){ //check if number is valid for this cell(i,j)
  117. sudoku[i][j] = num;
  118. if (solve_sudoku(sudoku)){ //recursive call, solve remaining cells.
  119. return 1;
  120. }
  121. sudoku[i][j] = 0;
  122. }
  123. }
  124. return 0;
  125. }
  126.  
  127. // Procedure: print_sudoku
  128. void print_sudoku(int sudoku[9][9])
  129. {
  130. int i, j;
  131. for(i=0; i<9; i++) {
  132. for(j=0; j<9; j++) {
  133. printf("%2d", sudoku[i][j]);
  134. }
  135. printf("\n");
  136. }
  137. }
  138.  
  139. // Procedure: parse_sudoku
  140. void parse_sudoku(const char fpath[], int sudoku[9][9]) {
  141. FILE *reader = fopen(fpath, "r");
  142. assert(reader != NULL);
  143. int i, j;
  144. for(i=0; i<9; i++) {
  145. for(j=0; j<9; j++) {
  146. fscanf(reader, "%d", &sudoku[i][j]);
  147. }
  148. }
  149. fclose(reader);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement