Beispiel #1
0
    static bool Solve()
    {
        var(h, w) = Read2();
        var s = GridShortestPath.ReadEnclosedGrid(ref h, ref w);

        var sv = GridShortestPath.FindChar(s, 's');
        var ev = GridShortestPath.FindChar(s, 'g');
        var r  = GridShortestPath.UndirectedBfs(h, w, s, sv, ev);

        return(r.GetByP(ev) < int.MaxValue);
    }
Beispiel #2
0
    static void Main()
    {
        var(h, w) = Read2();
        var sv = Read2() - new P(1, 1);
        var ev = Read2() - new P(1, 1);
        var s  = Array.ConvertAll(new bool[h], _ => Console.ReadLine());

        var r = GridShortestPath.UndirectedBfs(h, w, s, sv, ev);

        Console.WriteLine(r.GetByP(ev));
    }
Beispiel #3
0
    static void Main()
    {
        var(h, w) = Read2();
        var s = Array.ConvertAll(new bool[h], _ => Console.ReadLine());

        var sv = GridShortestPath.FindChar(h, w, s, 'S');
        var gv = GridShortestPath.FindChar(h, w, s, 'G');

        var map = new Dictionary <char, List <P> >();

        for (char c = 'a'; c <= 'z'; c++)
        {
            map[c] = new List <P>();
        }

        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                var c = s[i][j];
                if ('a' <= c && c <= 'z')
                {
                    map[c].Add(new P(i, j));
                }
            }
        }

        var r = GridShortestPath.Bfs(h, w, v =>
        {
            var c = s[v.i][v.j];
            if (!map.ContainsKey(c))
            {
                return(v.Nexts());
            }

            var nvs = v.Nexts().Concat(map[c]).ToArray();
            map.Remove(c);
            return(nvs);
        },
                                     sv, gv, v => s[v.i][v.j] == '#').GetByP(gv);

        Console.WriteLine(r == int.MaxValue ? -1 : r);
    }