Exemple #1
0
        static int BFS(int[,] maps)
        {
            Queue <coordi> q = new Queue <coordi>();

            visited[0, 0] = true;
            q.Enqueue(new coordi(0, 0, 1));

            while (q.Count != 0)
            {
                coordi cur = q.Dequeue();
                visited[cur.x, cur.y] = true;

                if (cur.x == maps.GetLength(0) - 1 && cur.y == maps.GetLength(1) - 1)
                {
                    return(cur.dist);
                }

                if (cur.x - 1 >= 0)
                {
                    if (!visited[cur.x - 1, cur.y] && maps[cur.x - 1, cur.y] == 1)
                    {
                        visited[cur.x - 1, cur.y] = true;
                        q.Enqueue(new coordi(cur.x - 1, cur.y, cur.dist + 1));
                    }
                }

                if (cur.x + 1 < maps.GetLength(0))
                {
                    if (!visited[cur.x + 1, cur.y] && maps[cur.x + 1, cur.y] == 1)
                    {
                        visited[cur.x + 1, cur.y] = true;
                        q.Enqueue(new coordi(cur.x + 1, cur.y, cur.dist + 1));
                    }
                }

                if (cur.y - 1 >= 0)
                {
                    if (!visited[cur.x, cur.y - 1] && maps[cur.x, cur.y - 1] == 1)
                    {
                        visited[cur.x, cur.y - 1] = true;
                        q.Enqueue(new coordi(cur.x, cur.y - 1, cur.dist + 1));
                    }
                }

                if (cur.y + 1 < maps.GetLength(1) && maps[cur.x, cur.y + 1] == 1)
                {
                    if (!visited[cur.x, cur.y + 1])
                    {
                        visited[cur.x, cur.y + 1] = true;
                        q.Enqueue(new coordi(cur.x, cur.y + 1, cur.dist + 1));
                    }
                }
            }

            return(-1);
        }
Exemple #2
0
        static int BFS(int x, int y)
        {
            Queue <coordi> q = new Queue <coordi>();

            q.Enqueue(new coordi(x, y, 0));

            visited[x, y] = true;

            while (q.Count != 0)
            {
                coordi temp = q.Dequeue();

                if (temp.x == goal_x && temp.y == goal_y)
                {
                    return(temp.dist);
                }

                if ((temp.x - 1) >= 0 && (temp.y - 2) >= 0 && visited[temp.x - 1, temp.y - 2] == false)
                {
                    visited[temp.x - 1, temp.y - 2] = true;
                    q.Enqueue(new coordi(temp.x - 1, temp.y - 2, temp.dist + 1));
                }

                if ((temp.x - 2) >= 0 && (temp.y - 1) >= 0 && visited[temp.x - 2, temp.y - 1] == false)
                {
                    visited[temp.x - 2, temp.y - 1] = true;
                    q.Enqueue(new coordi(temp.x - 2, temp.y - 1, temp.dist + 1));
                }

                if ((temp.x - 2) >= 0 && (temp.y + 1) < length && visited[temp.x - 2, temp.y + 1] == false)
                {
                    visited[temp.x - 2, temp.y + 1] = true;
                    q.Enqueue(new coordi(temp.x - 2, temp.y + 1, temp.dist + 1));
                }

                if ((temp.x - 1) >= 0 && (temp.y + 2) < length && visited[temp.x - 1, temp.y + 2] == false)
                {
                    visited[temp.x - 1, temp.y + 2] = true;
                    q.Enqueue(new coordi(temp.x - 1, temp.y + 2, temp.dist + 1));
                }

                if ((temp.x + 1) < length && (temp.y - 2) >= 0 && visited[temp.x + 1, temp.y - 2] == false)
                {
                    visited[temp.x + 1, temp.y - 2] = true;
                    q.Enqueue(new coordi(temp.x + 1, temp.y - 2, temp.dist + 1));
                }

                if ((temp.x + 2) < length && (temp.y - 1) >= 0 && visited[temp.x + 2, temp.y - 1] == false)
                {
                    visited[temp.x + 2, temp.y - 1] = true;
                    q.Enqueue(new coordi(temp.x + 2, temp.y - 1, temp.dist + 1));
                }

                if ((temp.x + 2) < length && (temp.y + 1) < length && visited[temp.x + 2, temp.y + 1] == false)
                {
                    visited[temp.x + 2, temp.y + 1] = true;
                    q.Enqueue(new coordi(temp.x + 2, temp.y + 1, temp.dist + 1));
                }

                if ((temp.x + 1) < length && (temp.y + 2) < length && visited[temp.x + 1, temp.y + 2] == false)
                {
                    visited[temp.x + 1, temp.y + 2] = true;
                    q.Enqueue(new coordi(temp.x + 1, temp.y + 2, temp.dist + 1));
                }
            }

            return(-1);
        }