using System; using System.Linq; using System.IO; namespace ConsoleApplicationTest { class Program { static void Main(string[] args) { var lines = File.ReadAllLines(@"c:\map.txt"); var m = (from l in lines select l.Replace('S','0').ToCharArray()).ToArray(); int gx = -1, gy = -1, p = -1; for (int n = 0; ; n++) { bool loop = false; for (int y = 0; y < m.Length && p==-1; y++) for (int x = 0; x < m[0].Length; x++) if (m[y][x] == n + '0') { if (m[y][x - 1] == 'G') { loop = false; gx = x - 1; gy = y; p = m[y][x]; break; } if (m[y][x + 1] == 'G') { loop = false; gx = x + 1; gy = y; p = m[y][x]; break; } if (m[y - 1][x] == 'G') { loop = false; gx = x; gy = y - 1; p = m[y][x]; break; } if (m[y + 1][x] == 'G') { loop = false; gx = x; gy = y + 1; p = m[y][x]; break; } char c = (char)(m[y][x] + 1); if (m[y][x - 1] == '.' || m[y][x - 1] > c) { loop = true; m[y][x - 1] = c; }; if (m[y][x + 1] == '.' || m[y][x + 1] > c) { loop = true; m[y][x + 1] = c; }; if (m[y - 1][x] == '.' || m[y - 1][x] > c) { loop = true; m[y - 1][x] = c; }; if (m[y + 1][x] == '.' || m[y + 1][x] > c) { loop = true; m[y + 1][x] = c; }; } if (!loop) break; } for (int y = 0; y < m.Length; y++){ for (int x = 0; x < m[0].Length; x++) Console.Write(m[y][x]); Console.Write(Environment.NewLine); } if (p == -1 ) { Console.WriteLine("no route"); return; } string ret =""; while (m[gy][gx] != '0') { if (m[gy][gx - 1] == p) { gx = gx - 1; ret = 'r' + ret; p--; continue; } if (m[gy][gx + 1] == p) { gx = gx + 1; ret = 'l' + ret; p--; continue; } if (m[gy - 1][gx] == p) { gy = gy - 1; ret = 'd' + ret; p--; continue; } if (m[gy + 1][gx] == p) { gy = gy + 1; ret = 'u' + ret; p--; continue; } } Console.WriteLine(ret); Console.ReadLine(); } } }