using System; using System.Collections.Generic; namespace No367_1{ public class Program{ public static void Main(string[] args){ var move = new[]{ new[]{ new{X = -2, Y = -1}, new{X = -2, Y = +1}, new{X = -1, Y = -2}, new{X = -1, Y = +2}, new{X = +1, Y = -2}, new{X = +1, Y = +2}, new{X = +2, Y = -1}, new{X = +2, Y = +1} }, new[]{ new{X = -1, Y = -1}, new{X = -1, Y = +1}, new{X = +1, Y = -1}, new{X = +1, Y = +1} } }; var input = Console.ReadLine().Split(' '); var w = int.Parse(input[1]); var h = int.Parse(input[0]); var start = new Point(0, 0); var goal = new Point(0, 0); var map = new char[h][]; for(var i = 0; i < h; i++){ map[i] = Console.ReadLine().ToCharArray(); int x; if((x = Array.IndexOf(map[i], 'S')) != -1){ start = new Point(x, i); } if((x = Array.IndexOf(map[i], 'G')) != -1){ goal = new Point(x, i); } } var result = new int[w, h, 2]; result[start.X, start.Y, 0] = 1; var que = new Queue(); que.Enqueue(new Stat(start.X, start.Y, 0)); while(que.Count != 0){ var now = que.Dequeue(); foreach(var m in move[now.Type]){ if(0 <= now.X + m.X && now.X + m.X < w && 0 <= now.Y + m.Y && now.Y + m.Y < h){ var next = new Stat( now.X + m.X, now.Y + m.Y, map[now.Y + m.Y][now.X + m.X] == 'R' ? 1 - now.Type : now.Type ); if(result[next.X, next.Y, next.Type] == 0 || result[next.X, next.Y, next.Type] > result[now.X, now.Y, now.Type] + 1){ que.Enqueue(next); result[next.X, next.Y, next.Type] = result[now.X, now.Y, now.Type] + 1; } } } } if((result[goal.X, goal.Y, 0] == result[goal.X, goal.Y, 1]) && result[goal.X, goal.Y, 0] == 0){ Console.WriteLine(-1); } else{ if(result[goal.X, goal.Y, 0] == 0) result[goal.X, goal.Y, 0] = int.MaxValue; if(result[goal.X, goal.Y, 1] == 0) result[goal.X, goal.Y, 1] = int.MaxValue; Console.WriteLine(Math.Min(result[goal.X, goal.Y, 0], result[goal.X, goal.Y, 1]) - 1); } Console.ReadLine(); } public class Stat{ public Stat(int x, int y, int type){ X = x; Y = y; Type = type; } public int X { get; } public int Y { get; } public int Type { get; } } public class Point{ public Point(int x, int y){ X = x; Y = y; } public int X { get; } public int Y { get; } } } }