package yukicoder; import java.util.ArrayDeque; import java.util.Scanner; public class Main{ public static void main(String[] args)throws Exception{ new Main().solve(); } void solve(){ Scanner sc=new Scanner(System.in); int h=sc.nextInt(); int w=sc.nextInt(); int sx=-1,sy=-1; char[][] table=new char[h][w]; for(int i=0;i que=new ArrayDeque

(); que.add(new P(sx,sy,"Knight",0)); while(!que.isEmpty()){ P p=que.poll(); if(table[p.y][p.x]=='G'){ System.out.println(p.n); return; }else if(p.state.equals("Knight")){ arrived_Knight[p.y][p.x]=true; for(int i=0;i<8;i++){ int nx=p.x+dx1[i]; int ny=p.y+dy1[i]; if(nx<0||ny<0||nx>=w||ny>=h)continue; if(arrived_Knight[ny][nx])continue; if(table[ny][nx]=='R'){ que.add(new P(nx,ny,"Bishop",p.n+1)); }else{ que.add(new P(nx,ny,"Knight",p.n+1)); } } }else if(p.state.equals("Bishop")){ arrived_Bishop[p.y][p.x]=true; for(int i=0;i<4;i++){ int nx=p.x+dx2[i]; int ny=p.y+dy2[i]; if(nx<0||ny<0||nx>=w||ny>=h)continue; if(arrived_Bishop[ny][nx])continue; if(table[ny][nx]=='R'){ que.add(new P(nx,ny,"Knight",p.n+1)); }else{ que.add(new P(nx,ny,"Bishop",p.n+1)); } } } } System.out.println(-1); } final int[] dx1={1,1,-1,-1,2,2,-2,-2}; final int[] dy1={2,-2,2,-2,1,-1,1,-1}; final int[] dx2={1,1,-1,-1}; final int[] dy2={1,-1,1,-1}; class P{ int x; int y; String state;//Knight,Bishop int n; P(int x,int y,String state,int n){ this.x=x; this.y=y; this.state=state; this.n=n; } } }