#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long #define INF (1 << 30) #define INFLL (1LL << 60) const int DIRX1[8] = {1,2,2,1,-1,-2,-2,-1}; const int DIRY1[8] = {2,1,-1,-2,-2,-1,1,2}; const int DIRX2[4] = {1,1,-1,-1}; const int DIRY2[4] = {-1,1,-1,1}; int main() { int h,w,sx,sy,gx,gy; char hyo[510][510]; cin >> h >> w; for(int i = 0;i < h;i++){ for(int j = 0;j < w;j++){ cin >> hyo[j][i]; if(hyo[j][i] == 'S'){ sx = j; sy = i; }else if(hyo[j][i] == 'G'){ gx = j; gy = i; } } } //0はナイト int ans = INF; bool flag = true; bool used[510][510][2] = {}; queue > > > que; que.push(make_pair(0,make_pair(0,make_pair(sx,sy)))); while(que.size()){ bool type = que.front().first; int how = que.front().second.first,nx = que.front().second.second.first,ny = que.front().second.second.second; que.pop(); if(used[nx][ny][type]) continue; used[nx][ny][type] = true; if(nx == gx && ny == gy){ cout << how << endl; flag = false; break; } if(type){ for(int i = 0;i < 4;i++){ int tx = nx + DIRX2[i],ty = ny + DIRY2[i]; if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue; if(hyo[tx][ty] == 'R') que.push(make_pair(0,make_pair(how + 1,make_pair(tx,ty)))); else que.push(make_pair(1,make_pair(how + 1,make_pair(tx,ty)))); } }else{ for(int i = 0;i < 8;i++){ int tx = nx + DIRX1[i],ty = ny + DIRY1[i]; if(tx < 0 || tx >= w || ty < 0 || ty >= h) continue; if(hyo[tx][ty] == 'R') que.push(make_pair(1,make_pair(how + 1,make_pair(tx,ty)))); else que.push(make_pair(0,make_pair(how + 1,make_pair(tx,ty)))); } } } if(flag) cout << -1 << endl; return 0; }