输入输出样例

样例1

输入

  1. 1 1
  2. 1 1
  3. #010203

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x31\x3B\x32\x3B\x33\x6D\x20\x1B\x5B\x30\x6D\x0A

样例2

输入

  1. 2 2
  2. 1 2
  3. #111111
  4. #0
  5. #000000
  6. #111

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x38\x3B\x38\x3B\x38\x6D\x20\x20\x1B\x5B\x30\x6D\x0A

样例3

输入

  1. 1 1
  2. 1 1
  3. #0

输出

  1. \x20\x0A

样例4

输入

  1. 2 2
  2. 2 1
  3. #111111
  4. #0
  5. #000000
  6. #111

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x38\x3B\x38\x3B\x38\x6D\x20\x1B\x5B\x30\x6D\x0A\x1B\x5B\x34\x38\x3B\x32\x3B\x38\x3B\x38\x3B\x38\x6D\x20\x1B\x5B\x30\x6D\x0A

样例5

输入

  1. 2 2
  2. 2 1
  3. #111111
  4. #0
  5. #000000
  6. #000

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x38\x3B\x38\x3B\x38\x6D\x20\x1B\x5B\x30\x6D\x0A\x20\x0A

样例6

输入

  1. 3 2
  2. 1 2
  3. #0
  4. #0
  5. #010101
  6. #010102
  7. #0
  8. #0

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x30\x3B\x30\x3B\x31\x6D\x20\x1B\x5B\x30\x6D\x20\x20\x0A

样例7

输入

  1. 1 2
  2. 1 2
  3. #123456
  4. #abcdef

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x39\x34\x3B\x31\x32\x38\x3B\x31\x36\x32\x6D\x20\x1B\x5B\x30\x6D\x0A

样例8

输入

  1. 2 1
  2. 2 1
  3. #654321
  4. #fedcba

输出

  1. \x1B\x5B\x34\x38\x3B\x32\x3B\x31\x37\x37\x3B\x31\x34\x33\x3B\x31\x30\x39\x6D\x20\x1B\x5B\x30\x6D\x0A

题解一

多的测试样例来自:https://blog.csdn.net/qq_42295427/article/details/104425068?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
独立写出来,哭了,写了一个多小时。100分。
题目说的非常复杂,但不需要考虑字符颜色,只考虑背景颜色。什么时候需要改变,什么时候用默认,根据题目说明严格遵守。

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  7. String[] strs = reader.readLine().split(" ");
  8. int m = Integer.parseInt(strs[0]);
  9. int n = Integer.parseInt(strs[1]);
  10. strs = reader.readLine().split(" ");
  11. int p = Integer.parseInt(strs[0]);
  12. int q = Integer.parseInt(strs[1]);
  13. // 所有像素的颜色
  14. Color[][] pixs = new Color[n][m];
  15. for (int i = 0; i < n; ++i) {
  16. for (int j = 0; j < m; ++j) {
  17. pixs[i][j] = new Color(reader.readLine());
  18. }
  19. }
  20. Color[] block = new Color[p * q];
  21. Color currentColor = new Color();
  22. for (int i = 0; i < n / q; ++i) {
  23. for (int j = 0; j < m / p; ++j) {
  24. int baseX = i * q;
  25. int baseY = j * p;
  26. int len = 0;
  27. for (int x = 0; x < q; ++x) {
  28. for (int y = 0; y < p; ++y) {
  29. block[len++] = pixs[baseX + x][baseY + y];
  30. }
  31. }
  32. // 求色块的平均颜色
  33. Color avgColor = Color.average(block);
  34. // 颜色不同才需要改变
  35. if (!currentColor.equals(avgColor)) {
  36. if (avgColor.isDefault()) {
  37. reset();
  38. } else {
  39. change(avgColor);
  40. }
  41. currentColor = avgColor;
  42. }
  43. print(' ');
  44. }
  45. if (!currentColor.isDefault()) {
  46. reset();
  47. currentColor = new Color();
  48. }
  49. print('\n');
  50. }
  51. }
  52. /**
  53. * 颜色重置
  54. */
  55. private static void reset() {
  56. char[] chars = new char[4];
  57. chars[0] = 27;
  58. chars[1] = '[';
  59. chars[2] = '0';
  60. chars[3] = 'm';
  61. print(chars);
  62. }
  63. /**
  64. * 改变成指定颜色
  65. *
  66. * @param color 目标颜色
  67. */
  68. private static void change(Color color) {
  69. StringBuilder sBuilder = new StringBuilder(19);
  70. sBuilder.append((char) 27);
  71. sBuilder.append("[48;2;");
  72. sBuilder.append(color.R);
  73. sBuilder.append(';');
  74. sBuilder.append(color.G);
  75. sBuilder.append(';');
  76. sBuilder.append(color.B);
  77. sBuilder.append('m');
  78. print(sBuilder.toString().toCharArray());
  79. }
  80. /**
  81. * 按要求打印序列
  82. *
  83. * @param chars 字符数组
  84. */
  85. private static void print(char[] chars) {
  86. for (int i : chars) {
  87. // 两位十六进制, 不足左侧补0
  88. System.out.printf("\\x%02X", i);
  89. }
  90. }
  91. /**
  92. * 按要求打印序列
  93. *
  94. * @param ch 单个字符
  95. */
  96. private static void print(int ch) {
  97. System.out.printf("\\x%02X", ch);
  98. }
  99. }
  100. class Color {
  101. int R;
  102. int G;
  103. int B;
  104. /**
  105. * 默认颜色
  106. */
  107. public Color() {
  108. R = 0;
  109. G = 0;
  110. B = 0;
  111. }
  112. public Color(int R, int G, int B) {
  113. this.R = R;
  114. this.G = G;
  115. this.B = B;
  116. }
  117. /**
  118. * 解析颜色
  119. *
  120. * @param s 简化或未简化的颜色代码
  121. */
  122. public Color(String s) {
  123. StringBuilder sBuilder = new StringBuilder();
  124. switch (s.length()) {
  125. case 2:
  126. for (int i = 0; i < 6; ++i) {
  127. sBuilder.append(s.charAt(1));
  128. }
  129. s = sBuilder.toString();
  130. break;
  131. case 4:
  132. for (int i = 0; i < 6; ++i) {
  133. sBuilder.append(s.charAt(i / 2 + 1));
  134. }
  135. s = sBuilder.toString();
  136. break;
  137. default:
  138. s = s.replace("#", "");
  139. }
  140. R = Integer.parseInt(s.substring(0, 2), 16);
  141. G = Integer.parseInt(s.substring(2, 4), 16);
  142. B = Integer.parseInt(s.substring(4, 6), 16);
  143. // System.out.println(s);
  144. // System.out.println("R:" + R + " G:" + G + " B:" + B);
  145. }
  146. @Override
  147. public boolean equals(Object obj) {
  148. if (obj instanceof Color) {
  149. Color o = (Color) obj;
  150. return (R == o.R) && (G == o.G) && (B == o.B);
  151. }
  152. return false;
  153. }
  154. public boolean isDefault() {
  155. return (R == 0) && (G == 0) && (B == 0);
  156. }
  157. /**
  158. * 求色块的平均颜色
  159. *
  160. * @param colors 颜色数组
  161. * @return 平均颜色
  162. */
  163. public static Color average(Color[] colors) {
  164. int r = 0;
  165. int g = 0;
  166. int b = 0;
  167. for (Color color : colors) {
  168. r += color.R;
  169. g += color.G;
  170. b += color.B;
  171. }
  172. return new Color(r / colors.length, g / colors.length, b / colors.length);
  173. }
  174. }