p10957.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <algorithm>
  2. #include <array>
  3. #include <iostream>
  4. #include <map>
  5. #include <utility>
  6. #include <vector>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <climits>
  10. #include <queue>
  11. #include <list>
  12. using namespace std;
  13. typedef unsigned int nat;
  14. vector<vector<nat>> sudoku(9, vector<nat>(9,0));
  15. void reset_sudoku()
  16. {
  17. for(auto &c : sudoku)
  18. {
  19. for(auto &cell : c)
  20. {
  21. cell = 0;
  22. }
  23. }
  24. }
  25. bool set_pos(pair<nat,nat> pos, nat val)
  26. {
  27. if(val)
  28. {
  29. for(auto &c : sudoku)
  30. {
  31. if(c[pos.second] == val)
  32. return false;
  33. }
  34. for(auto &cell : sudoku[pos.first])
  35. {
  36. if(cell == val)
  37. return false;
  38. }
  39. for(nat y = 3*(pos.second/3); y < 3*(pos.second/3)+3; ++y)
  40. {
  41. for(nat x = 3*(pos.first/3); x < 3*(pos.first/3)+3; ++x)
  42. {
  43. if(sudoku[x][y] == val)
  44. return false;
  45. }
  46. }
  47. }
  48. sudoku[pos.first][pos.second] = val;
  49. return true;
  50. }
  51. bool get_next(pair<nat,nat> &pos)
  52. {
  53. for(nat y = 0; y < 9; ++y)
  54. {
  55. for(nat x = 0; x < 9; x++)
  56. {
  57. if(!sudoku[x][y])
  58. {
  59. pos = {x,y};
  60. return true;
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66. void print_sudoku()
  67. {
  68. for(nat y = 0; y < 9; ++y)
  69. {
  70. for(nat x = 0; x < 9; x++)
  71. {
  72. cout << sudoku[x][y] << " ";
  73. }
  74. cout << endl;
  75. }
  76. }
  77. nat solve( )
  78. {
  79. pair<nat,nat> next_cell;
  80. nat solutions = 0;
  81. if(get_next(next_cell))
  82. {
  83. bool possible = false;
  84. for(nat i = 1; i < 10; ++i)
  85. {
  86. if(set_pos(next_cell, i))
  87. {
  88. possible = true;
  89. solutions += solve();
  90. set_pos(next_cell, 0);
  91. if(solutions > 1)
  92. return solutions;
  93. }
  94. }
  95. if(!possible)
  96. return 0;
  97. else
  98. return solutions;
  99. }
  100. //cout << "One solution is:" << endl;
  101. //print_sudoku();
  102. return 1;
  103. }
  104. int main()
  105. {
  106. nat test_case = 1;
  107. while(true)
  108. {
  109. reset_sudoku();
  110. bool success = true;
  111. for(nat y = 0; y < 9; ++y)
  112. {
  113. for(nat x = 0; x < 9; x++)
  114. {
  115. nat val;
  116. cin >> val;
  117. if(cin.eof())
  118. return 0;
  119. success = success && set_pos( {x, y}, val );
  120. }
  121. }
  122. cout << "Case " << test_case++ << ": ";
  123. if(success)
  124. {
  125. //print_sudoku();
  126. nat s = solve();
  127. if(s == 0)
  128. cout << "Impossible." << endl;
  129. else if(s == 1)
  130. cout << "Unique." << endl;
  131. else
  132. cout << "Ambiguous." << endl;
  133. }
  134. else
  135. {
  136. cout << "Illegal." << endl;
  137. }
  138. }
  139. return 0;
  140. }