static void Main() { var(h, w) = Read2(); var z = Read(); var sv = (z[0], z[1]); var ev = (z[2], z[3]); var s = GraphConsole.ReadEnclosedGrid(ref h, ref w); var r1 = ShortestPath.ForGrid(h, w) .ForUnweightedMap(v => { var nvs = (v.i + v.j) % 2 == 0 ? Nexts1(v) : Nexts2(v); return(Array.FindAll(nvs, nv => s.GetValue(nv) != '#')); }) .Bfs(sv, ev); var r2 = ShortestPath.ForGrid(h, w) .ForUnweightedMap(v => { var nvs = (v.i + v.j) % 2 != 0 ? Nexts1(v) : Nexts2(v); return(Array.FindAll(nvs, nv => s.GetValue(nv) != '#')); }) .Bfs(sv, ev); var m = Math.Min(r1[ev], r2[ev]); Console.WriteLine(m == long.MaxValue ? -1 : m); Point[] Nexts1(Point v) => new[] { new Point(v.i - 1, v.j), new Point(v.i + 1, v.j) }; Point[] Nexts2(Point v) => new[] { new Point(v.i, v.j - 1), new Point(v.i, v.j + 1) }; }
static void Main() { var(h, w) = Read2(); var s = GraphConsole.ReadEnclosedGrid(ref h, ref w); var sv = (h - 2, w - 1); var ev = (1, 1); var r = ShortestPath.ForGrid(h, w) .ForWeightedMap(v => { var nes = new List <Edge <Point> >(); var uv = v - new Point(1, 0); var lv = v - new Point(0, 1); if (s.GetValue(uv) != '#') { nes.Add(new Edge <Point>(v, uv, s.GetValue(uv) == 'S' ? 0 : 1)); } if (s.GetValue(lv) != '#') { nes.Add(new Edge <Point>(v, lv, s.GetValue(lv) == 'E' ? 0 : 1)); } return(nes.ToArray()); }) .BfsMod(2, sv, ev); Console.WriteLine(r[ev]); }
static object Solve() { var(h, w) = Read2(); var sp = GraphConsole.ReadPoint(); var ep = GraphConsole.ReadPoint(); var s = GraphConsole.ReadEnclosedGrid(ref h, ref w); int ToId(Point p) => p.i * w + p.j; int ToId2(int i, int j) => i * w + j; Point FromId(int id) => new Point(id / w, id % w); var u_tate = new bool[h, w]; var u_yoko = new bool[h, w]; var r = ShortestPathCore.Bfs(h * w, pid => { var(pi, pj) = FromId(pid); var nexts = new List <int>(); if (!u_tate[pi, pj]) { u_tate[pi, pj] = true; for (int i = pi - 1; i >= 0 && s[i][pj] == '.'; i--) { u_tate[i, pj] = true; nexts.Add(ToId2(i, pj)); } for (int i = pi + 1; i < h && s[i][pj] == '.'; i++) { u_tate[i, pj] = true; nexts.Add(ToId2(i, pj)); } } if (!u_yoko[pi, pj]) { u_yoko[pi, pj] = true; for (int j = pj - 1; j >= 0 && s[pi][j] == '.'; j--) { u_yoko[pi, j] = true; nexts.Add(ToId2(pi, j)); } for (int j = pj + 1; j < w && s[pi][j] == '.'; j++) { u_yoko[pi, j] = true; nexts.Add(ToId2(pi, j)); } } return(nexts.ToArray()); }, ToId(sp), ToId(ep)); return(r[ToId(ep)] - 1); }
static void Main() { var(h, w) = Read2(); var s = GraphConsole.ReadEnclosedGrid(ref h, ref w); var sv = (h - 2, w - 1); var ev = (1, 1); var grid = ShortestPath.ForGrid(h, w); var dp = grid.CreateMap(int.MaxValue); dp[sv] = 0; var r = grid.ForUnweightedMap(v => { if (v.i == 0 || v.j == 0) { return(new Point[0]); } var lv = v - (0, 1); var uv = v - (1, 0); if (v.j == w - 1) { var nlv = dp[v] + (s.GetValue(lv) == 'E' ? 0 : 1); dp[lv] = Math.Min(dp[lv], nlv); return(new[] { lv }); } else { var nlv = dp[v] + (s.GetValue(lv) == 'E' ? 0 : 1); dp[lv] = Math.Min(dp[lv], nlv); var nuv = dp[v] + (s.GetValue(uv) == 'S' ? 0 : 1); dp[uv] = Math.Min(dp[uv], nuv); return(new[] { lv, uv }); } }).Bfs(sv, (-1, -1)); Console.WriteLine(dp[ev]); }
static void Main() { var(h, w) = Read2(); Point sv = Read2(); var s = GraphConsole.ReadEnclosedGrid(ref h, ref w); const string arrows = "v^><"; var spp = ShortestPath.ForGrid(h, w) .ForUnweightedMap(v => { var nvs = new List <Point>(); var nva = v.Nexts(); for (int k = 0; k < 4; k++) { var(i, j) = nva[k]; if (s[i][j] == '.' || s[i][j] == arrows[k]) { nvs.Add(nva[k]); } } return(nvs.ToArray()); }) .Bfs(sv, (-1, -1)); Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); for (int i = 1; i < h - 1; i++) { for (int j = 1; j < w - 1; j++) { Console.Write(s[i][j] == '#' ? '#' : spp.IsConnected((i, j)) ? 'o' : 'x'); } Console.WriteLine(); } Console.Out.Flush(); }