示例#1
0
            public int QueryDistance(int x, int y)
            {
                if (x < 0 || x >= _width || y < 0 || y >= _height)
                {
                    return(-1);
                }
                BFSCell cell = _grid[x, y];

                return(cell.Distance);
            }
示例#2
0
        private static Point BFS(Point s, int[][] board)
        {
            Queue <BFSCell> q = new Queue <BFSCell>();

            int[] dr = new int[] { 1, 0, -1, 0 };
            int[] dc = new int[] { 0, 1, 0, -1 };
            for (int i = 0; i < 4; i++)
            {
                int nr = s.X + dr[i];
                int nc = s.Y + dc[i];
                if (nr >= 0 && nc >= 0 && nr < Rows && nc < Cols && board[nr][nc] == 0)
                {
                    q.Enqueue(new BFSCell {
                        r = nr, c = nc, d = i, l = 0
                    });
                }
            }
            while (q.Count > 0)
            {
                BFSCell p = q.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    if (Math.Abs(p.d - i) == 2)
                    {
                        continue;
                    }
                    int nr = p.r + dr[i];
                    int nc = p.c + dc[i];
                    if (nr >= 0 && nc >= 0 && nr < Rows && nc < Cols)
                    {
                        int nl = p.l;
                        if (p.d != i)
                        {
                            nl++;
                        }
                        if (nl > 2)
                        {
                            continue;
                        }
                        if (board[nr][nc] == 0)
                        {
                            BFSCell cell = new BFSCell {
                                r = nr, c = nc, d = i, l = nl
                            };
                            q.Enqueue(cell);
                        }
                        else if (board[nr][nc] == board[s.X][s.Y])
                        {
                            return(new Point(nr, nc));
                        }
                    }
                }
            }
            return(new Point(-1, -1));
        }
示例#3
0
 public void Reset()
 {
     for (int j = 0; j < _height; j++)
     {
         for (int i = 0; i < _width; i++)
         {
             BFSCell cell = _grid[i, j];
             cell.Visited  = false;
             cell.CameFrom = null;
             cell.Distance = -1;
         }
     }
 }
示例#4
0
            public void SearchCharGrid(char[,] grid, char empty, int x, int y)
            {
                Reset();
                Queue <BFSCell> frontier = new Queue <BFSCell>();

                ProcessCell(grid, empty, frontier, null, x, y);
                while (frontier.Any())
                {
                    BFSCell current = frontier.Dequeue();
                    ProcessCell(grid, empty, frontier, current, current.X + 1, current.Y);
                    ProcessCell(grid, empty, frontier, current, current.X - 1, current.Y);
                    ProcessCell(grid, empty, frontier, current, current.X, current.Y + 1);
                    ProcessCell(grid, empty, frontier, current, current.X, current.Y - 1);
                }
            }
示例#5
0
 public BreadthFirstSearchGrid(int width, int height)
 {
     _grid   = new BFSCell[width, height];
     _width  = width;
     _height = height;
     for (int j = 0; j < _height; j++)
     {
         for (int i = 0; i < _width; i++)
         {
             _grid[i, j] = new BFSCell {
                 X = i, Y = j,
             };
         }
     }
 }
示例#6
0
 private void ProcessCell(char[,] grid, char empty, Queue <BFSCell> frontier, BFSCell current, int nextX, int nextY)
 {
     if (nextX < 0 || nextX >= _width || nextY < 0 || nextY >= _height)
     {
         return;
     }
     if (grid[nextX, nextY] == empty || current == null)
     {
         BFSCell cell = _grid[nextX, nextY];
         if (cell.Visited == false)
         {
             frontier.Enqueue(cell);
             cell.Visited  = true;
             cell.Distance = (current?.Distance ?? -1) + 1;
             cell.CameFrom = current;
         }
     }
 }