Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Main {
- public static void dfs(int x, int y, char way, StringBuilder[] result) {
- result[x].setCharAt(y, way);
- int[] dx = {-1, 0, 0, 1};
- int[] dy = {0, -1, 1, 0};
- char[] ways = {'D', 'R', 'L', 'U'};
- for (int i = 0; i < 4; i++) {
- int tox = x + dx[i];
- int toy = y + dy[i];
- if (result[tox].charAt(toy) == '.') {
- dfs(tox, toy, ways[i], result);
- }
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- try {
- String in = bufferedReader.readLine();
- int n, m;
- String[] parsed = in.split(" ");
- n = Integer.parseInt(parsed[0]);
- m = Integer.parseInt(parsed[1]);
- String[] field = new String[n];
- StringBuilder[] result = new StringBuilder[n];
- int x = 0, y = 0;
- for (int i = 0; i < n; i++) {
- field[i] = bufferedReader.readLine();
- for (int j = 0; j < m; j++) {
- if (field[i].charAt(j) == 'S') {
- x = i;
- y = j;
- }
- }
- result[i] = new StringBuilder(field[i]);
- }
- bufferedReader.close();
- dfs(x, y, 'S', result);
- for (int i = 0; i < n; i++) {
- System.out.println(result[i].toString());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment