Advertisement
0705danny

Untitled

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