fork download
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4.  
  5. namespace ConsoleApplicationTest
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var lines = File.ReadAllLines(@"c:\map.txt");
  12. var m = (from l in lines select l.Replace('S','0').ToCharArray()).ToArray();
  13. int gx = -1, gy = -1, p = -1;
  14. for (int n = 0; ; n++)
  15. {
  16. bool loop = false;
  17. for (int y = 0; y < m.Length && p==-1; y++)
  18. for (int x = 0; x < m[0].Length; x++)
  19. if (m[y][x] == n + '0')
  20. {
  21. if (m[y][x - 1] == 'G') { loop = false; gx = x - 1; gy = y; p = m[y][x]; break; }
  22. if (m[y][x + 1] == 'G') { loop = false; gx = x + 1; gy = y; p = m[y][x]; break; }
  23. if (m[y - 1][x] == 'G') { loop = false; gx = x; gy = y - 1; p = m[y][x]; break; }
  24. if (m[y + 1][x] == 'G') { loop = false; gx = x; gy = y + 1; p = m[y][x]; break; }
  25. char c = (char)(m[y][x] + 1);
  26. if (m[y][x - 1] == '.' || m[y][x - 1] > c) { loop = true; m[y][x - 1] = c; };
  27. if (m[y][x + 1] == '.' || m[y][x + 1] > c) { loop = true; m[y][x + 1] = c; };
  28. if (m[y - 1][x] == '.' || m[y - 1][x] > c) { loop = true; m[y - 1][x] = c; };
  29. if (m[y + 1][x] == '.' || m[y + 1][x] > c) { loop = true; m[y + 1][x] = c; };
  30. }
  31. if (!loop) break;
  32. }
  33. for (int y = 0; y < m.Length; y++){
  34. for (int x = 0; x < m[0].Length; x++)
  35. Console.Write(m[y][x]);
  36. Console.Write(Environment.NewLine);
  37. }
  38. if (p == -1 ) { Console.WriteLine("no route"); return; }
  39. string ret ="";
  40. while (m[gy][gx] != '0')
  41. {
  42. if (m[gy][gx - 1] == p) { gx = gx - 1; ret = 'r' + ret; p--; continue; }
  43. if (m[gy][gx + 1] == p) { gx = gx + 1; ret = 'l' + ret; p--; continue; }
  44. if (m[gy - 1][gx] == p) { gy = gy - 1; ret = 'd' + ret; p--; continue; }
  45. if (m[gy + 1][gx] == p) { gy = gy + 1; ret = 'u' + ret; p--; continue; }
  46. }
  47. Console.WriteLine(ret);
  48. Console.ReadLine();
  49. }
  50. }
  51. }
  52.  
Runtime error #stdin #stdout 0.04s 40816KB
stdin
Standard input is empty
stdout
Standard output is empty