/** * Buggy Robot: solution by Steven Zeil */ import java.awt.Point; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; public class BuggyRobot_sjz { private BufferedReader input; public BuggyRobot_sjz(BufferedReader input) { this.input = input; } public static void main(String[] args) throws FileNotFoundException, IOException { if (args.length > 0) { for (int i = 0; i < args.length; ++i) { try (BufferedReader input = new BufferedReader(new FileReader(args[i]))) { new BuggyRobot_sjz(input).solve(); } } } else { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); new BuggyRobot_sjz(input).solve(); } } class State { public int x; public int y; public int steps; public State (int xx, int yy, int s) { x = xx; y = yy; steps = s; } public String toString() { return "(" + x + "," + y + "," + steps + ")"; } public boolean equals (Object obj) { State s = (State)obj; return (x == s.x) && (y == s.y) && (steps == s.steps); } public int hashCode () { return x + 51 * y + 113 * steps; } } class ExtendedState implements Comparable { public State s; public int distance; public ExtendedState (State ss, int dd) { s = ss; distance = dd; } public String toString() { return "[" + s + ":" + distance + "]"; } public boolean equals (Object obj) { ExtendedState es = (ExtendedState)obj; return (distance == es.distance) && s.equals(es.s); } public int hashCode () { return s.hashCode() * 1013 + distance; } @Override public int compareTo(ExtendedState es) { return distance - es.distance; } } private void solve() throws IOException { String line = input.readLine(); Scanner numInput = new Scanner(line); int nRows = numInput.nextInt(); int nCols = numInput.nextInt(); Point start = null; Point stop = null; String[] map = new String[nRows]; for (int i = 0; i < nRows; ++i) { map[i] = input.readLine(); int startPos = map[i].indexOf('R'); if (startPos >= 0) { start = new Point(startPos, i); } int stopPos = map[i].indexOf('E'); if (stopPos >= 0) { stop = new Point(stopPos, i); } } String command = input.readLine(); solve (nRows, nCols, map, start, stop, command); numInput.close(); } class DistanceTable { Map distances; public DistanceTable() { distances = new HashMap(); } public int get(State s) { Integer k = distances.get(s); if (k == null) { return Integer.MAX_VALUE; } else { return k; } } public void put(State s, int d) { distances.put(s, d); } } private void solve(int nRows, int nCols, String[] map, Point start, Point stop, String command) { PriorityQueue pq = new PriorityQueue<>(); Map distances = new HashMap(); State startState = new State(start.x, start.y, 0); distances.put(startState, 0); pq.add(new ExtendedState(startState, 0)); int reachedExit = -1; while (reachedExit < 0 && !pq.isEmpty()) { ExtendedState from = pq.remove(); if (from.s.x == stop.x && from.s.y == stop.y) { reachedExit = from.distance; break; } // Try moving forward with no changes to command string if (from.s.steps < command.length()) { char moveCh = command.charAt(from.s.steps); State dest = move(map, from.s, moveCh); ++dest.steps; update (pq, distances, new ExtendedState(dest, from.distance)); } // Try also adding a move just before the current one for (int move = 0; move < 4; ++move) { State dest = move(map, from.s, move); update (pq, distances, new ExtendedState(dest, from.distance+1)); } } System.out.println(reachedExit); } private State move(String[] map, State s, int move) { int dx = 0; int dy = 0; switch (move) { case 0: dx = 1; break; case 1: dx = -1; break; case 2: dy = 1; break; case 3: dy = -1; break; default: } int x = s.x + dx; int y = s.y + dy; boolean valid = (x >= 0 && x < map[0].length()); valid = valid && (y >= 0 && y < map.length); valid = valid && (map[y].charAt(x) != '#'); if (!valid) { x = s.x; y = s.y; } return new State (x, y, s.steps); } private void update(PriorityQueue pq, Map distances, ExtendedState es) { Integer priorDistance = distances.get(es.s); if (priorDistance == null || priorDistance > es.distance) { distances.put(es.s, es.distance); pq.add(es); } } private State move(String[] map, State s, char moveCh) { int dx = 0; int dy = 0; switch (moveCh) { case 'R': dx = 1; break; case 'L': dx = -1; break; case 'D': dy = 1; break; case 'U': dy = -1; break; default: } int x = s.x + dx; int y = s.y + dy; boolean valid = (x >= 0 && x < map[0].length()); valid = valid && (y >= 0 && y < map.length); valid = valid && (map[y].charAt(x) != '#'); if (!valid) { x = s.x; y = s.y; } return new State (x, y, s.steps); } }