import java.util.*; import java.io.*; public class RightHandRule { char[][] map; int x, y; static BufferedReader in; static final boolean DEBUG = false; public RightHandRule(int x, int y) throws IOException{ this.x = x; this.y = y; map = new char[x][y]; for(int i=0;i=0;i--){ char[] line = in.readLine().toCharArray(); for(int lx = 0;lx= x|| (ey+h.dsy)>= y) return false; switch(map[ex+h.dsx][ey+h.dsy]){ // space ahead is empty: can go straight, turn right, or reach exit // based on what is ahead-right case ' ': switch(map[ex+h.drx][ey+h.dry]){ case 'X': case 'G': if(DEBUG) System.out.println("case \' X\' straight"); ex+=h.dsx; ey+=h.dsy; break; case 'E': if(DEBUG) System.out.println("case \' E\' leave"); return lookForGoal(ex+h.dsx,ey+h.dsy); case ' ': if(DEBUG) System.out.println("case \' \' turn right"); ex+=h.drx; ey+=h.dry; h = h.right; break; } break; // space ahead is wall, can turn left case 'X': if(DEBUG) System.out.println("case \'X\' turn left"); h = h.left; break; // space ahead is exit, exit without seeing goal case 'E': if(DEBUG) System.out.println("case \'E\' leave"); return false; } } } boolean lookForGoal(int ex, int ey){ for(int xx = ex-1;xx>0&&map[xx][ey]!='X' && map[xx][ey]!='E';xx--) if(map[xx][ey]=='G') return true; for(int xx = ex+1;xx0&&map[ex][yy]!='X' && map[ex][yy]!='E';yy--) if(map[ex][yy]=='G') return true; for(int yy = ey+1;yy