fork download
  1. #include <stdio.h>
  2. #define N 3
  3. #define N2 N*N
  4. #define N3 N*N*N
  5. #define N4 N2*N2
  6.  
  7. // global
  8. const int n2 = N2;
  9. const int n3 = N3;
  10. const int n4 = N4;
  11. char board[N4 + 1];
  12. char candidate[N4][N2 + 2];
  13.  
  14. // prototype
  15. void initialize();
  16. int continueCheck();
  17. void printBoard();
  18. void decideAll();
  19. void spread(int);
  20.  
  21. void candidatePrint()
  22. {
  23. int i;
  24. for (i = 0; i < n4; ++i)
  25. if (board[i] == ' ')printf("candidate[%d] = %s\n", i, candidate[i]);
  26. }
  27.  
  28. void initialize()
  29. {
  30. int i;
  31. printBoard();
  32. for (i = 0; i < n4; ++i)
  33. sprintf(candidate[i], " 123456789" );
  34. for (i = 0; i < n4; ++i)
  35. if (board[i] > '0')
  36. spread(i);
  37. }
  38.  
  39. int continueCheck()
  40. {
  41. char *s = board;
  42. printBoard();
  43. for (s = board; *s; ++s)
  44. if (*s < '0') return 1;
  45. return 0;
  46. }
  47.  
  48. void printBoard()
  49. {
  50. int i = 0;
  51. char *x = board;
  52. printf("\n------------------");
  53. for (; *x; ++i, ++x) {
  54. if (i % n2 == 0)putchar('\n');
  55. printf("%c ", *x);
  56. }
  57. printf("\n------------------");
  58. }
  59.  
  60. void decideAll()
  61. {
  62. int i, count;
  63. char *s, w;
  64. for (i = 0; i < n4; ++i) {
  65. if (board[i] != ' ')continue;
  66. // count candidate chars
  67. for (s = candidate[i], count = 0; *s; ++s) {
  68. if (*s != ' ') {
  69. w = *s;
  70. ++count;
  71. }
  72. }
  73. if (count == 1) {
  74. board[i] = w;
  75. spread(i);
  76. }
  77. }
  78. }
  79.  
  80. void spread(const int n)
  81. {
  82. int i, j, ii, jj, w;
  83. w = board[n] - '0';
  84. // horizontal
  85. i = n / n2;
  86. for (j = 0; j < n2; ++j)
  87. candidate[i * n2 + j ][w] = ' ';
  88. // vertical
  89. j = n % n2;
  90. for (i = 0 ; i < n2; ++i)
  91. candidate[i * n2 + j ][w] = ' ';
  92. // local
  93. for (i = n / n2 / N * N, ii = 0; ii < N; ++i, ++ii) {
  94. for (j = n % n2 / N * N, jj = 0; jj < N; ++j, ++jj) {
  95. candidate[i * n2 + j][w] = ' ';
  96. }
  97. }
  98. }
  99.  
  100. int main()
  101. {
  102. sprintf(board,
  103. "53 7 " \
  104. "6 195 " \
  105. " 98 6 " \
  106. "8 6 3" \
  107. "4 8 3 1" \
  108. "7 2 6" \
  109. " 6 28 " \
  110. " 419 5" \
  111. " 8 79"
  112. );
  113. initialize();
  114. do {
  115. decideAll();
  116. } while (continueCheck());
  117.  
  118. return 0;
  119. }
  120.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
------------------
5 3     7         
6     1 9 5       
  9 8         6   
8       6       3 
4     8   3     1 
7       2       6 
  6         2 8   
      4 1 9     5 
        8     7 9 
------------------
------------------
5 3     7         
6     1 9 5       
  9 8         6   
8       6       3 
4     8 5 3     1 
7     9 2       6 
  6     3 7 2 8 4 
      4 1 9   3 5 
        8     7 9 
------------------
------------------
5 3     7         
6     1 9 5       
  9 8   4 2   6 7 
8     7 6       3 
4 2   8 5 3   9 1 
7     9 2       6 
  6   5 3 7 2 8 4 
2   7 4 1 9 6 3 5 
        8 6 1 7 9 
------------------
------------------
5 3   6 7 8     2 
6     1 9 5   4 8 
1 9 8 3 4 2 5 6 7 
8     7 6   4   3 
4 2 6 8 5 3 7 9 1 
7     9 2   8 5 6 
9 6 1 5 3 7 2 8 4 
2 8 7 4 1 9 6 3 5 
3     2 8 6 1 7 9 
------------------
------------------
5 3 4 6 7 8 9 1 2 
6 7 2 1 9 5 3 4 8 
1 9 8 3 4 2 5 6 7 
8     7 6 1 4 2 3 
4 2 6 8 5 3 7 9 1 
7 1 3 9 2 4 8 5 6 
9 6 1 5 3 7 2 8 4 
2 8 7 4 1 9 6 3 5 
3   5 2 8 6 1 7 9 
------------------
------------------
5 3 4 6 7 8 9 1 2 
6 7 2 1 9 5 3 4 8 
1 9 8 3 4 2 5 6 7 
8 5 9 7 6 1 4 2 3 
4 2 6 8 5 3 7 9 1 
7 1 3 9 2 4 8 5 6 
9 6 1 5 3 7 2 8 4 
2 8 7 4 1 9 6 3 5 
3 4 5 2 8 6 1 7 9 
------------------