fork download
  1. import java.io.*;
  2. import java.math.*;
  3. import java.util.*;
  4. import java.lang.*;
  5.  
  6. public class Main{
  7.  
  8. public static InputStream inputStream = System.in;
  9. public static OutputStream outputStream = System.out;
  10. public static FastReader in = new FastReader(inputStream);
  11. public static PrintWriter out = new PrintWriter(outputStream);
  12.  
  13.  
  14. public static void main(String[] args)throws java.lang.Exception{
  15. new Main().run();
  16. out.close();
  17. }
  18.  
  19. int N;
  20. int M;
  21. boolean[][] dfsNode;
  22. char[][] grid;
  23. char[][] filled;
  24. String[] sep;
  25.  
  26. void run()throws java.lang.Exception{
  27. N = Integer.parseInt(br.readLine().trim());
  28. sep = new String[N];
  29. for(int i=0; i<N; i++){
  30. ArrayList<char[]> al = new ArrayList<char[]>();
  31. while(true){
  32. String s = br.readLine();
  33. if(s.contains("_")){
  34. sep[i] = s;
  35. break;
  36. }
  37. char[] arr = s.toCharArray();
  38. al.add(arr);
  39. }
  40. grid = new char[al.size()][];
  41. for(int j=0; j<al.size(); j++){
  42. grid[j] = al.get(j);
  43. }
  44. // ArrayUtils.printGrid(grid);
  45. int stari = -1;
  46. int starj = -1;
  47. for(int j=0; j<grid.length; j++){
  48. for(int k=0; k<grid[j].length; k++){
  49. if(grid[j][k] == '*'){
  50. stari = j;
  51. starj = k;
  52. break;
  53. }
  54. }
  55. }
  56. dfsNode = new boolean[grid.length][];
  57. filled = new char[grid.length][];
  58. for(int j=0; j<grid.length; j++){
  59. char[] arr = new char[grid[j].length];
  60. for(int k=0; k<grid[j].length; k++){
  61. arr[k] = grid[j][k];
  62. }
  63. filled[j] = arr;
  64. dfsNode[j] = new boolean[grid[j].length];
  65. }
  66. fillColour(stari, starj);
  67. for(int j=0; j<filled.length; j++){
  68. for(int k=0; k<filled[j].length; k++){
  69. if(filled[j][k] == '*'){
  70. out.print(' ');
  71. }else{
  72. out.print(filled[j][k]);
  73. }
  74. }
  75. out.println();
  76. }
  77. out.println(sep[i]);
  78. }
  79. }
  80.  
  81. void fillColour(int row, int col){
  82. if(row<0 || row>=grid.length || col<0 || col>=grid[row].length){
  83. return;
  84. }
  85. if(dfsNode[row][col]){
  86. return;
  87. }
  88.  
  89. // fill on border?
  90. if(grid[row][col]!=' ' && grid[row][col]!='*'){
  91. return;
  92. }
  93.  
  94. filled[row][col] = '#';
  95. dfsNode[row][col] = true;
  96. fillColour(row-1, col);
  97. fillColour(row+1, col);
  98. fillColour(row, col-1);
  99. fillColour(row, col+1);
  100. }
  101. }
  102.  
  103. class ArrayUtils{
  104.  
  105. public static void initGraph(ArrayList<Integer>[] graph){
  106. for(int i=0; i<graph.length; i++){
  107. graph[i] = new ArrayList<Integer>();
  108. }
  109. }
  110.  
  111. public static void fill2d(long[][] arr){
  112. for(int i=0; i<arr.length; i++){
  113. Arrays.fill(arr[i], -1L);
  114. }
  115. }
  116.  
  117. public static void fill2d(int[][] arr){
  118. for(int i=0; i<arr.length; i++){
  119. Arrays.fill(arr[i], -1);
  120. }
  121. }
  122.  
  123. public static void input2d(int[][] arr, FastReader in){
  124. for(int i=0; i<arr.length; i++){
  125. for(int j=0; j<arr[0].length; j++){
  126. arr[i][j] = in.nextInt();
  127. }
  128. }
  129. }
  130.  
  131. public static void input1d(int[] arr, FastReader in){
  132. for(int i=0; i<arr.length; i++){
  133. arr[i] = in.nextInt();
  134. }
  135. }
  136.  
  137. public static void printGrid(char[][] arr){
  138. System.out.println("Character grid : ");
  139. for(int i=0; i<arr.length; i++){
  140. for(int j=0; j<arr[i].length; j++){
  141. System.out.print(arr[i][j]);
  142. }
  143. System.out.println();
  144. }
  145. System.out.println("--------------------------------------");
  146. }
  147. }
  148.  
  149.  
  150. class FastReader{
  151. private boolean finished = false;
  152.  
  153. private InputStream stream;
  154. private byte[] buf = new byte[1024];
  155. private int curChar;
  156. private int numChars;
  157. private SpaceCharFilter filter;
  158.  
  159. public FastReader(InputStream stream){
  160. this.stream = stream;
  161. }
  162.  
  163. public int read(){
  164. if (numChars == -1){
  165. throw new InputMismatchException ();
  166. }
  167. if (curChar >= numChars){
  168. curChar = 0;
  169. try{
  170. numChars = stream.read (buf);
  171. } catch (IOException e){
  172. throw new InputMismatchException ();
  173. }
  174. if (numChars <= 0){
  175. return -1;
  176. }
  177. }
  178. return buf[curChar++];
  179. }
  180.  
  181. public int peek(){
  182. if (numChars == -1){
  183. return -1;
  184. }
  185. if (curChar >= numChars){
  186. curChar = 0;
  187. try{
  188. numChars = stream.read (buf);
  189. } catch (IOException e){
  190. return -1;
  191. }
  192. if (numChars <= 0){
  193. return -1;
  194. }
  195. }
  196. return buf[curChar];
  197. }
  198.  
  199. public int nextInt(){
  200. int c = read ();
  201. while (isSpaceChar (c))
  202. c = read ();
  203. int sgn = 1;
  204. if (c == '-'){
  205. sgn = -1;
  206. c = read ();
  207. }
  208. int res = 0;
  209. do{
  210. if(c==','){
  211. c = read();
  212. }
  213. if (c < '0' || c > '9'){
  214. throw new InputMismatchException ();
  215. }
  216. res *= 10;
  217. res += c - '0';
  218. c = read ();
  219. } while (!isSpaceChar (c));
  220. return res * sgn;
  221. }
  222.  
  223. public long nextLong(){
  224. int c = read ();
  225. while (isSpaceChar (c))
  226. c = read ();
  227. int sgn = 1;
  228. if (c == '-'){
  229. sgn = -1;
  230. c = read ();
  231. }
  232. long res = 0;
  233. do{
  234. if (c < '0' || c > '9'){
  235. throw new InputMismatchException ();
  236. }
  237. res *= 10;
  238. res += c - '0';
  239. c = read ();
  240. } while (!isSpaceChar (c));
  241. return res * sgn;
  242. }
  243.  
  244. public String nextString(){
  245. int c = read ();
  246. while (isSpaceChar (c))
  247. c = read ();
  248. StringBuilder res = new StringBuilder ();
  249. do{
  250. res.appendCodePoint (c);
  251. c = read ();
  252. } while (!isSpaceChar (c));
  253. return res.toString ();
  254. }
  255.  
  256. public boolean isSpaceChar(int c){
  257. if (filter != null){
  258. return filter.isSpaceChar (c);
  259. }
  260. return isWhitespace (c);
  261. }
  262.  
  263. public static boolean isWhitespace(int c){
  264. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  265. }
  266.  
  267. private String readLine0(){
  268. StringBuilder buf = new StringBuilder ();
  269. int c = read ();
  270. while (c != '\n' && c != -1){
  271. if (c != '\r'){
  272. buf.appendCodePoint (c);
  273. }
  274. c = read ();
  275. }
  276. return buf.toString ();
  277. }
  278.  
  279. public String nextLine(){
  280. String s = readLine0 ();
  281. while (s.trim ().length () == 0)
  282. s = readLine0 ();
  283. return s;
  284. }
  285.  
  286. public String nextLine(boolean ignoreEmptyLines){
  287. if (ignoreEmptyLines){
  288. return nextLine ();
  289. }else{
  290. return readLine0 ();
  291. }
  292. }
  293.  
  294. public BigInteger nextBigInteger(){
  295. try{
  296. return new BigInteger (nextString ());
  297. } catch (NumberFormatException e){
  298. throw new InputMismatchException ();
  299. }
  300. }
  301.  
  302. public char nextCharacter(){
  303. int c = read ();
  304. while (isSpaceChar (c))
  305. c = read ();
  306. return (char) c;
  307. }
  308.  
  309. public double nextDouble(){
  310. int c = read ();
  311. while (isSpaceChar (c))
  312. c = read ();
  313. int sgn = 1;
  314. if (c == '-'){
  315. sgn = -1;
  316. c = read ();
  317. }
  318. double res = 0;
  319. while (!isSpaceChar (c) && c != '.'){
  320. if (c == 'e' || c == 'E'){
  321. return res * Math.pow (10, nextInt ());
  322. }
  323. if (c < '0' || c > '9'){
  324. throw new InputMismatchException ();
  325. }
  326. res *= 10;
  327. res += c - '0';
  328. c = read ();
  329. }
  330. if (c == '.'){
  331. c = read ();
  332. double m = 1;
  333. while (!isSpaceChar (c)){
  334. if (c == 'e' || c == 'E'){
  335. return res * Math.pow (10, nextInt ());
  336. }
  337. if (c < '0' || c > '9'){
  338. throw new InputMismatchException ();
  339. }
  340. m /= 10;
  341. res += (c - '0') * m;
  342. c = read ();
  343. }
  344. }
  345. return res * sgn;
  346. }
  347.  
  348. public boolean isExhausted(){
  349. int value;
  350. while (isSpaceChar (value = peek ()) && value != -1)
  351. read ();
  352. return value == -1;
  353. }
  354.  
  355. public String next(){
  356. return nextString ();
  357. }
  358.  
  359. public SpaceCharFilter getFilter(){
  360. return filter;
  361. }
  362.  
  363. public void setFilter(SpaceCharFilter filter){
  364. this.filter = filter;
  365. }
  366.  
  367. public interface SpaceCharFilter{
  368. public boolean isSpaceChar(int ch);
  369. }
  370. }
Success #stdin #stdout 0.08s 381184KB
stdin
2
XXXXXXXXX
X   X   X
X *     X
X   X   X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X   X
X * X
X   X
XXXXX
_____
stdout
XXXXXXXXX
X###X###X
X#######X
X###X###X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X###X
X###X
X###X
XXXXX
_____