Назад к лучшим решениям Status: AC, problem ZSUD, contest ZEL07. By klopyrev (Konstantin Lopyrev), 2007-02-20 00:30:53.
  1. import java.io.*;
  2. public class Main
  3. {
  4. private static int[] maze;
  5. private static boolean found;
  6. private static boolean[] [] col;
  7. private static boolean[] [] row;
  8. private static boolean[] [] box;
  9. private static boolean[] empty;
  10. private static final int[] c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8};
  11. private static final int[] r = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8};
  12. private static final int[] b = {0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 6, 7, 7, 7, 8, 8, 8, 6, 6, 6, 7, 7, 7, 8, 8, 8};
  13. private static long time;
  14. public static void solve (int cur)
  15. {
  16. if (cur == maze.length)
  17. {
  18. found = true;
  19. return;
  20. }
  21. if (!empty [cur])
  22. {
  23. solve (cur + 1);
  24. return;
  25. }
  26. for (int i = 1 ; i <= 9 && !found ; i++)
  27. {
  28. if (!row [r [cur]] [i - 1] && !col [c [cur]] [i - 1] && !box [b [cur]] [i - 1])
  29. {
  30. row [r [cur]] [i - 1] = true;
  31. col [c [cur]] [i - 1] = true;
  32. box [b [cur]] [i - 1] = true;
  33. maze [cur] = i;
  34. solve (cur + 1);
  35. row [r [cur]] [i - 1] = false;
  36. col [c [cur]] [i - 1] = false;
  37. box [b [cur]] [i - 1] = false;
  38. }
  39. }
  40. }
  41.  
  42.  
  43. public static void main (String[] args) throws IOException
  44. {
  45. time = System.currentTimeMillis ();
  46. int T = Integer.parseInt (in.readLine ());
  47. for (int cases = 0 ; cases < T ; cases++)
  48. {
  49. char[] temp = in.readLine ().toCharArray ();
  50. col = new boolean [9] [9];
  51. row = new boolean [9] [9];
  52. box = new boolean [9] [9];
  53. maze = new int [temp.length];
  54. empty = new boolean [temp.length];
  55. int unfilled = 0;
  56. for (int i = 0 ; i < maze.length ; i++)
  57. {
  58. if (temp [i] != '.')
  59. {
  60. maze [i] = temp [i] - '0';
  61. col [c [i]] [maze [i] - 1] = true;
  62. row [r [i]] [maze [i] - 1] = true;
  63. box [b [i]] [maze [i] - 1] = true;
  64. }
  65. else
  66. {
  67. maze [i] = 0;
  68. empty [i] = true;
  69. unfilled++;
  70. }
  71. }
  72. boolean change = true;
  73. while (unfilled != 0 && change)
  74. {
  75. change = false;
  76. int[] [] placeRows = new int [9] [9];
  77. int[] [] rowLoc = new int [9] [9];
  78. int[] [] placeCols = new int [9] [9];
  79. int[] [] colLoc = new int [9] [9];
  80. int[] [] placeBoxes = new int [9] [9];
  81. int[] [] boxA = new int [9] [9];
  82. int[] [] boxB = new int [9] [9];
  83. for (int i = 0 ; i < 81 ; i++)
  84. {
  85. if (empty [i])
  86. {
  87. int number = -1;
  88. boolean valid = true;
  89. for (int j = 1 ; j <= 9 ; j++)
  90. {
  91. if (!row [r [i]] [j - 1] && !col [c [i]] [j - 1] && !box [b [i]] [j - 1])
  92. {
  93. placeRows [r [i]] [j - 1]++;
  94. rowLoc [r [i]] [j - 1] = c [i];
  95. placeCols [c [i]] [j - 1]++;
  96. colLoc [c [i]] [j - 1] = r [i];
  97. placeBoxes [b [i]] [j - 1]++;
  98. boxA [b [i]] [j - 1] = r [i];
  99. boxB [b [i]] [j - 1] = c [i];
  100. if (number == -1)
  101. number = j;
  102. else
  103. valid = false;
  104. }
  105. }
  106. if (valid)
  107. {
  108. row [r [i]] [number - 1] = true;
  109. col [c [i]] [number - 1] = true;
  110. box [b [i]] [number - 1] = true;
  111. maze [i] = number;
  112. empty [i] = false;
  113. change = true;
  114. unfilled--;
  115. }
  116. }
  117. }
  118. if (unfilled != 0)
  119. {
  120. for (int i = 0 ; i < 9 ; i++)
  121. {
  122. for (int j = 0 ; j < 9 ; j++)
  123. {
  124. if (placeRows [i] [j] == 1)
  125. {
  126. boolean done = false;
  127. int cols = rowLoc [i] [j];
  128. int place = i * 9 + cols;
  129. if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
  130. {
  131. done = true;
  132. row [r [place]] [j] = true;
  133. col [c [place]] [j] = true;
  134. box [b [place]] [j] = true;
  135. maze [place] = j + 1;
  136. change = true;
  137. empty [place] = false;
  138. unfilled--;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. if (unfilled != 0)
  145. {
  146. for (int i = 0 ; i < 9 ; i++)
  147. {
  148. for (int j = 0 ; j < 9 ; j++)
  149. {
  150. if (placeCols [i] [j] == 1)
  151. {
  152. boolean done = false;
  153. int rows = colLoc [i] [j];
  154. int place = rows * 9 + i;
  155. if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
  156. {
  157. done = true;
  158. row [r [place]] [j] = true;
  159. col [c [place]] [j] = true;
  160. box [b [place]] [j] = true;
  161. maze [place] = j + 1;
  162. change = true;
  163. empty [place] = false;
  164. unfilled--;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. if (unfilled != 0)
  171. {
  172. for (int i = 0 ; i < 9 ; i++)
  173. {
  174. for (int j = 0 ; j < 9 ; j++)
  175. {
  176. if (placeBoxes [i] [j] == 1)
  177. {
  178.  
  179. boolean done = false;
  180. int rows = boxA [i] [j];
  181. int cols = boxB [i] [j];
  182. int place = rows * 9 + cols;
  183. if (!row [r [place]] [j] && !col [c [place]] [j] && !box [b [place]] [j] && empty [place])
  184. {
  185. done = true;
  186. row [r [place]] [j] = true;
  187. col [c [place]] [j] = true;
  188. box [b [place]] [j] = true;
  189. maze [place] = j + 1;
  190. change = true;
  191. empty [place] = false;
  192. unfilled--;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. found = false;
  200. if (unfilled == 0)
  201. found = true;
  202. if (!found)
  203. solve (0);
  204. System.out.println ("Y");
  205. String output = "";
  206. for (int i = 0 ; i < maze.length ; i++)
  207. output += maze [i];
  208. System.out.println (output);
  209.  
  210. }
  211. }
  212. }
  213.  
  214.