#include #include #include #include const int INF = 10000000; using namespace std; typedef pair P; int ary[550][550]; int nxmove[8] = {1,-1,1,-1,2,-2,2,-2}; int nymove[8] = {2,2,-2,-2,1,1,-1,-1}; int bxmove[4] = {1,-1,1,-1}; int bymove[4] = {1,1,-1,-1}; int sx ,sy,gx,gy; int h,w; string str[1010]; bool koma = 0; int bfs() { queue

que; for(int i=0; i= w || my >= h) continue; if(INF == ary[my][mx]) que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; continue; } } int mx = x + bxmove[i]; int my = y + bymove[i]; koma = 0; if(mx < 0 || my < 0 || mx >= w || my >= h) continue; if(INF == ary[my][mx]) que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; } } for(int i=0; i<8; i++) { int mx = x + nxmove[i]; int my = y + nymove[i]; koma = 0; if(mx < 0 || my < 0 || mx >= w || my >= h) continue; if(INF == ary[my][mx]) que.push(P(mx,my)); ary[my][mx] = ary[y][x] + 1; } } return ary[gy][gx]; } int main() { cin >> h >> w; for(int i = 0; i < h; i++) { cin >> str[i]; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++) { if(str[i][j] == 'S') { sx = j; sy = i; } else if(str[i][j] == 'G') { gx = j; gy = i; } } } } int ans = bfs(); if(ans != INF) cout << ans << endl; else cout << -1 << endl; return 0; }