Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //Robert Millaway
  5. //8/31
  6. //Sudokode p1
  7.  
  8. #define column_check 1
  9. #define row_check 2
  10. #define box_check 3
  11. #define NONNUMBER_FOUND 4
  12.  
  13.  
  14.  
  15. int wrong(char *array, int type, int errorLocation);
  16. int check(char *array);
  17.  
  18.  
  19. int main( char *argv[])
  20. {
  21.  
  22.  
  23. /* Reading the Matrix from file */
  24. int c;
  25. int i = 0, j = 0;
  26.  
  27. char matrix[9][9];
  28. for(i = 0; i < 9; i++)
  29. {
  30. char row[10];
  31. scanf("%s", row);
  32.  
  33. for(j = 0; j < 9; j++)
  34. {
  35. matrix[i][j] = row[i];
  36. }
  37. }
  38.  
  39. /* Printing the matrix */
  40. printf("\n -- sudoku -- \n");
  41. for (i = 0; i < 9; i++)
  42. {
  43. for (j = 0; j < 9; j++)
  44. printf("%c ", matrix[i][j]);
  45. printf("\n");
  46. }
  47.  
  48. char buffer[9];
  49.  
  50. //column
  51. for (i = 0; i < 9; i++)
  52. {
  53. for (j = 0; j < 9; j++)
  54. buffer[j] = matrix[i][j];
  55.  
  56. if (check(buffer) != 0)
  57. wrong(buffer, column_check, i);
  58. }
  59.  
  60. //row
  61. for (j = 0; j < 9; j++)
  62. {
  63. for (i = 0; i < 9; i++)
  64. {
  65. buffer[i] = matrix[i][j];
  66. }
  67. if (check(buffer) != 0)
  68. wrong(buffer, row_check, j);
  69. }
  70.  
  71. //box
  72. int count = 0;
  73. int icnt = 0;
  74. int jcnt = 0;
  75. int matcnt = 0;
  76.  
  77. for (i = 0; i <= 6; i+= 3)
  78. {
  79. for (j = 0; j <= 6; j += 3)
  80. {
  81. for (icnt = 0, count = 0; icnt < 3; icnt++)
  82. {
  83. for (jcnt = 0; jcnt < 3; jcnt++)
  84. {
  85. buffer[count++] = matrix[i+icnt][j+jcnt];
  86. }
  87.  
  88. }
  89.  
  90. ++matcount;
  91. if (check(buffer) != 0)
  92. wrong(buffer, box_check, matcnt);
  93. }
  94. }
  95.  
  96. if (flag == 0)
  97. printf("\n yes :)");
  98.  
  99. printf("\n");
  100. return 0;
  101. }
  102.  
  103. int check(char *array)
  104. {
  105. int i, count;
  106. for (i = 0; i < 9; i++)
  107. {
  108. count = i;
  109. while (count < 9)
  110. {
  111. if (array[i] == array[++count])
  112. return 1;
  113. }
  114. }
  115. return 0;
  116. }
  117.  
  118. int wrong(char *array, int type, int errorLocation)
  119. {
  120. int i;
  121. flag = 1;
  122. if (type == column_check)
  123. {
  124. printf("\n no %d: \n\t", ++errorLocation);
  125. for (i = 0; i < 9; i++)
  126. printf("%c ", array[i]);
  127. }
  128. else if (type == row_check)
  129. {
  130. printf("\n no %d: \n", ++errorLocation);
  131. for (i = 0; i < 9; i++)
  132. printf("\n\t %c ", array[i]);
  133. }
  134. else if (type == box_check)
  135. {
  136. printf("\n no %d: \n", errorLocation);
  137. for (i = 0; i < 9; i++)
  138. {
  139. printf("%c ", array[i]);
  140. if ((i == 2) || (i == 5))
  141. printf("\n");
  142. }
  143.  
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement