ZhenyaDudko

Untitled

Sep 4th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6.  
  7. public class Main {
  8.  
  9. public static void dfs(int x, int y, char way, StringBuilder[] result) {
  10. result[x].setCharAt(y, way);
  11. int[] dx = {-1, 0, 0, 1};
  12. int[] dy = {0, -1, 1, 0};
  13. char[] ways = {'D', 'R', 'L', 'U'};
  14. for (int i = 0; i < 4; i++) {
  15. int tox = x + dx[i];
  16. int toy = y + dy[i];
  17. if (result[tox].charAt(toy) == '.') {
  18. dfs(tox, toy, ways[i], result);
  19. }
  20. }
  21. }
  22.  
  23. public static void main(String[] args) throws IOException {
  24. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  25. try {
  26. String in = bufferedReader.readLine();
  27. int n, m;
  28. String[] parsed = in.split(" ");
  29. n = Integer.parseInt(parsed[0]);
  30. m = Integer.parseInt(parsed[1]);
  31. String[] field = new String[n];
  32. StringBuilder[] result = new StringBuilder[n];
  33. int x = 0, y = 0;
  34. for (int i = 0; i < n; i++) {
  35. field[i] = bufferedReader.readLine();
  36. for (int j = 0; j < m; j++) {
  37. if (field[i].charAt(j) == 'S') {
  38. x = i;
  39. y = j;
  40. }
  41. }
  42. result[i] = new StringBuilder(field[i]);
  43. }
  44. bufferedReader.close();
  45. dfs(x, y, 'S', result);
  46. for (int i = 0; i < n; i++) {
  47. System.out.println(result[i].toString());
  48. }
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment