fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. class Rextester {
  7. List<Set<String>> words;
  8. int[][] counts;
  9.  
  10. //try to run with the following input:
  11. //1
  12. //6 6 2
  13. //++++++
  14. //+____+
  15. //++++_+
  16. //++++_+
  17. //++++_+
  18. //++++++
  19. //pain
  20. //nice
  21. //
  22.  
  23. public static void main(String[] args) throws IOException {
  24. Rextester r = new Rextester();
  25. r.run();
  26. }
  27.  
  28. public void run() throws IOException {
  29. String line = in.readLine();
  30. int t = Integer.parseInt(line);
  31. for (int k = 1; k <= t; k++) {
  32. line = in.readLine();
  33. String[] parts = line.split(" ");
  34. int x = Integer.parseInt(parts[0]);
  35. int y = Integer.parseInt(parts[1]);
  36. int amount = Integer.parseInt(parts[2]);
  37.  
  38. char[][] c = new char[y+2][x+2];
  39. for (int i = 1; i <= y; i++) {
  40. line = "+" + in.readLine() + "+";
  41. c[i] = line.toCharArray();
  42. }
  43. for (int i = 0; i <= x+1; i++) {
  44. c[0][i] = '+';
  45. c[y+1][i] = '+';
  46. }
  47.  
  48. words = new ArrayList<>();
  49. counts = new int[y+2][x+2];
  50. for (int i = 2; i <= 250; i++)
  51. words.add(new HashSet<String>());
  52. for (int i = 0; i < amount; i++) {
  53. line = in.readLine();
  54. words.get(line.length()).add(line);
  55. }
  56. in.readLine();
  57.  
  58. Stack<Placeholder> placeholders = new Stack<>();
  59. for (int i = 1; i <= y; i++)
  60. for (int j = 1; j <= x; j++)
  61. if (c[i][j-1] == '+' && c[i][j] == '_' && c[i][j+1] == '_') {
  62. int xx = j; int ll = 0;
  63. while (c[i][j] == '_') {
  64. ll++;
  65. j++;
  66. }
  67. placeholders.push(new Placeholder(xx, i, ll, true));
  68. }
  69.  
  70. for (int j = 1; j <= x; j++)
  71. for (int i = 1; i <= y; i++)
  72. if (c[i-1][j] == '+' && c[i][j] == '_' && c[i+1][j] == '_') {
  73. int yy = i; int ll = 0;
  74. while (c[i][j] == '_') {
  75. ll++;
  76. i++;
  77. }
  78. placeholders.push(new Placeholder(j, yy, ll, false));
  79. }
  80.  
  81. c = solve(c, placeholders);
  82. System.out.println("Case #" + k + ":");
  83. for (int i = 1; i <= y; i++) {
  84. for (int j = 1; j <= x; j++)
  85. System.out.print(c[i][j]);
  86. System.out.println();
  87. }
  88. if (k!=t)
  89. System.out.println();
  90. }
  91. }
  92.  
  93. char[][] solve (char[][] c, Stack<Placeholder> placeholders) {
  94. if (placeholders.isEmpty())
  95. return c;
  96.  
  97. Placeholder pl = placeholders.pop();
  98. for (String word : words.get(pl.l)) {
  99. if (fill(c, word, pl)) {
  100. char[][] ret = solve(c, placeholders);
  101. if (ret != null)
  102. return ret;
  103. unfill(c, pl);
  104. }
  105. }
  106. placeholders.push(pl);
  107. return null;
  108. }
  109.  
  110. boolean fill (char[][] c, String word, Placeholder pl) {
  111. if (pl.h) {
  112. for (int i = pl.x; i < pl.x + pl.l; i++)
  113. if (c[pl.y][i] != '_' && c[pl.y][i] != word.charAt(i - pl.x))
  114. return false;
  115. for (int i = pl.x; i < pl.x + pl.l; i++) {
  116. c[pl.y][i] = word.charAt(i - pl.x);
  117. counts[pl.y][i]++;
  118. }
  119. return true;
  120.  
  121. } else {
  122. for (int i = pl.y; i < pl.y + pl.l; i++)
  123. if (c[i][pl.x] != '_' && c[i][pl.x] != word.charAt(i - pl.y))
  124. return false;
  125. for (int i = pl.y; i < pl.y + pl.l; i++) {
  126. c[i][pl.x] = word.charAt(i - pl.y);
  127. counts[i][pl.x]++;
  128. }
  129. return true;
  130. }
  131. }
  132.  
  133. void unfill(char[][] c, Placeholder pl) {
  134. if (pl.h) {
  135. for (int i = pl.x; i < pl.x + pl.l; i++)
  136. if (--counts[pl.y][i] == 0)
  137. c[pl.y][i] = '_';
  138. } else {
  139. for (int i = pl.y; i < pl.y + pl.l; i++)
  140. if (--counts[i][pl.x] == 0)
  141. c[i][pl.x] = '_';
  142. }
  143. }
  144. }
  145. class Placeholder {
  146. int x, y, l;
  147. boolean h;
  148. public Placeholder(int x, int y, int l, boolean h) {
  149. this.x = x;
  150. this.y = y;
  151. this.l = l;
  152. this.h = h;
  153. }
  154. }
  155.  
Success #stdin #stdout 0.04s 4386816KB
stdin
1
6 6 2
++++++
+____+
++++_+
++++_+
++++_+
++++++
pain
nice
stdout
Case #1:
++++++
+pain+
++++i+
++++c+
++++e+
++++++