Beispiel #1
0
        public int bfs(pair <int, int> p1, pair <int, int> p2, pair <int, int> p3)
        {
            int N = p3.ff;
            int M = p3.ss;
            Queue <pair <pair <int, int>, int> > Que;
            Dictionary <pair <int, int>, bool>   Vis;

            Que.Enqueue(mpa(p1, 0));

            while (Que.Count != 0)
            {
                pair <pair <int, int>, int> temp = Que.front();
                Que.pop();

                if (temp.ff.ff == p2.ff && temp.ff.ss == p2.ss)
                {
                    return(temp.ss);
                }
                int x   = temp.ff.ff;
                int y   = temp.ff.ss;
                int dis = temp.ss + 1;


                if (Vis.count(mpa(x, y)))
                {
                    continue;
                }
                Vis[mpa(x, y)] = true;

                for (int i = 0; i < 8; ++i)
                {
                    int x1 = x + dx[i];
                    int y1 = y + dy[i];
                    if (valid(x1, y1, N, M))
                    {
                        Que.push(mpa(mpa(x1, y1), dis));
                    }
                }
            }

            return(-1);
        }