C# 如何用bfs求路径矩阵

C# 如何用bfs求路径矩阵,c#,breadth-first-search,C#,Breadth First Search,假设我有一个矩阵,如: 其中0表示空白,1表示第一项,2表示第二项,3表示第三项 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 3 我从(0,0)开始,我想参观1,2,3,因此在I,j方面,它从(0,0)-(3,1)-(1,4)-(4,4) 所以要创造一条我一直在思考的道路 从起始位置(0,0)运行BFS至1位置(3,1) 现在运行BFS开始位置为(3,1)到2(1,4) 现在运行BFS开始位置为(1,4)到3(4,4) 要使用BFS,我们需

假设我有一个矩阵,如:

其中0表示空白,1表示第一项,2表示第二项,3表示第三项

0 0 0 0 0 
0 0 0 0 2
0 0 0 0 0
0 1 0 0 0
0 0 0 0 3
我从
(0,0)
开始,我想参观
1,2,3
,因此在
I,j
方面,它从
(0,0)-(3,1)-(1,4)-(4,4)

所以要创造一条我一直在思考的道路

  • 从起始位置(0,0)运行BFS至1位置(3,1)
  • 现在运行BFS开始位置为(3,1)到2(1,4)
  • 现在运行BFS开始位置为(1,4)到3(4,4)
  • 要使用BFS,我们需要节点定义及其子节点,但如何做到这一点? 到目前为止,我已经:

    using System;   
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    
    
    namespace ejercicio2
    {
    
        class Program
        {
            struct position
            {
                public int row;
                public int col;
            };
    
            const int NOT_VISITED = 0;
            static int[,] map = {
                                    {0, 0, 0, 0, 0}, 
                                    {0, 0, 0, 0, 2},
                                    {0, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 0},
                                    {0, 0, 0, 0, 3}
                                };
    
            static void Main()
            {
                int who = 1;
                position start;
                start.row = 0;
                start.col = 0;
                //find 1
                bfs(start, map, who);
                start.row = 3;
                start.col = 1;
                //find 2
                who = 2;
                bfs(start, map,who);
                start.row = 1;
                start.col = 4;
                //find 3
                 who = 3;
                bfs(start, map,who);
    
            }
    
            static void bfs(position present, int[,] map, int lookingfor)
            {
               //how to look for looking for??
    
    
            }
            private bool visited(int[,] board, position cell)
            {
                return board[cell.row, cell.col] < 1;
            }
        }
    }
    
    使用系统;
    使用System.IO;
    使用System.Collections.Generic;
    使用系统文本;
    使用系统集合;
    命名空间ejercicio2
    {
    班级计划
    {
    结构位置
    {
    公共int row;
    公共int col;
    };
    const int NOT_visted=0;
    静态int[,]映射={
    {0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0},
    {0, 0, 0, 0, 3}
    };
    静态void Main()
    {
    int who=1;
    位置启动;
    start.row=0;
    start.col=0;
    //查找1
    bfs(start、map、who);
    start.row=3;
    start.col=1;
    //查找2
    who=2;
    bfs(start、map、who);
    start.row=1;
    start.col=4;
    //查找3
    who=3;
    bfs(start、map、who);
    }
    静态无效bfs(位置存在,int[,]映射,int查找)
    {
    //怎么找找找??
    }
    已访问的专用布尔值(int[,]板,位置单元格)
    {
    返回板[cell.row,cell.col]<1;
    }
    }
    }
    
    使用System.IO;
    使用制度;
    使用System.Collections.Generic;
    班级计划
    {
    结构方向
    {
    公共int x;
    公共智力;
    };
    静态方向[]方向={
    新方向(){x=1,y=0},
    新方向(){x=0,y=1},
    新方向(){x=-1,y=0},
    新方向(){x=0,y=-1}
    };
    静态int[,]visitedmap=resetVisitedMap();
    结构位置
    {
    公共int row;
    公共int col;
    };
    const int NOT_visted=0;
    静态int[,]映射={
    {0, 4, 0, 0, 0}, 
    {0, 0, 0, 0, 2},
    {0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0},
    {0, 0, 0, 0, 3}
    };
    静态void Main()
    {
    int[]who=新int[]{1,2,3,4};
    位置启动;
    start.row=0;
    start.col=0;
    Console.Write(“+start.row+”,“+start.col+”);
    //查找1
    foreach(int查找who){
    开始=bfs(开始、映射、查找);
    Console.Write(“->”(“+start.row+”,“+start.col+”);
    }
    }
    静态位置bfs(位置存在,整数[,]映射,整数查找)
    {
    位置[]姿势=新位置[]{present};
    bool-found=false;
    而(!found){
    foreach(姿势中的位置){
    if(映射[pos.row,pos.col]==查找){
    //Console.Write(“---->成功地点:(“+pos.row+”,“+pos.col+”);
    visitedmap=重置visitedmap();
    返回pos;
    }
    }
    姿势=单元距离(姿势);
    }
    归还礼物;
    }
    静态位置[]单元距离(位置位置){
    //Console.WriteLine(“查找(“+pos.row+”,“+pos.col+”)附近的单元格”);
    列表结果=新列表();
    foreach(方向中的方向){
    int ROWCOMPUTED=位置行+方向x;
    int colcomputed=pos.col+dir.y;
    如果(行计算>=0&&
    colcomputed>=0&&
    行计算
    只需取消记录行的注释,即可查看如何执行搜索。。。。
    如果您有任何疑问,请随时提问。

    这里有完整路径的代码

    using System.IO;
    using System;
    using System.Collections.Generic;
    
    class Program
    {
    
    
           struct direction
                {
                    public int x;
                    public int y;
                };
    
                static direction[] directions = {
                                           new direction(){x = 1, y = 0},
                                           new direction(){x = 0, y = 1},
                                           new direction(){x = -1, y = 0},
                                           new direction(){x = 0, y = -1}
        };
    
    
    
         static int[,] visitedmap = resetVisitedMap();
    
    
    
             class Position
            {
                public int row;
                public int col;
                public Position  parent;
            };
    
            const int NOT_VISITED = 0;
            static int[,] map = {
                                    {0, 4, 0, 0, 0}, 
                                    {0, 0, 0, 0, 2},
                                    {0, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 0},
                                    {0, 0, 0, 0, 3}
                                };
    
            static void Main()
            {
                int[] who = new int[]{1,2,3,4};
                Position start = new Position();
                start.row = 0;
                start.col = 0;
                start.parent = null;
                Console.Write("("+start.row+","+start.col+")");
                //find 1
                foreach(int lookingfor in who){
                        start = bfs(start, map, lookingfor); 
                        Console.Write("->("+start.row+","+start.col+")");
                }
    
                Console.Write("\n Full path :\n");
                string path = "";
                while (start != null){
                    path = "->("+ start.row+","+start.col+")"+path;
                    start = start.parent;
                }
                Console.Write(path);
            }
    
            static Position bfs(Position present, int[,] map, int lookingfor)
            {
                Position[] poses = new Position[]{present};
                bool found = false;
    
               while (!found){
                   foreach (Position pos in poses){
                       if (map[pos.row, pos.col] == lookingfor){
                          // Console.Write("------->Success at : ("+pos.row+","+pos.col+")");
                            visitedmap = resetVisitedMap();
                           return pos;
                       }
                   }
                poses = cellsNear(poses);
               }
    
            return present;
            }
    
            static Position[] cellsNear(Position pos){
                //Console.WriteLine("Looking for cells near (" + pos.row +","+pos.col+")" );
                List<Position> result = new List<Position>();
                foreach (direction dir in directions){
                    int rowCalculated = pos.row + dir.x;
                    int colCalculated = pos.col + dir.y;
                    if (rowCalculated >= 0 &&
                            colCalculated >= 0 &&
                            rowCalculated < map.GetLength(0) && 
                            colCalculated < map.GetLength(1) &&
                            !visited(rowCalculated,colCalculated)){
                        visitedmap[rowCalculated,colCalculated] = 1;
                        Position posPath = new Position();
                        posPath.col = colCalculated;
                        posPath.row = rowCalculated;
                        posPath.parent = pos;
                        result.Add(posPath);
                        //Console.WriteLine("found children cell : ("+rowCalculated+","+colCalculated+")" );
                    }
                }
                //Console.WriteLine("\n");
                return result.ToArray();
            }
    
            static Position[] cellsNear(Position[] poses){
                List<Position> result = new List<Position>();
                foreach (Position pos in poses){
                    result.AddRange(cellsNear(pos));
                }
                return result.ToArray();
            }
    
            static bool visited(int row, int col)
                {
                    return visitedmap[row, col] == 1;
                }
        static int[,] resetVisitedMap(){
            return  new int[,]{
                                        {0, 0, 0, 0, 0}, 
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0}
                                    };
        }
    }
    
    <代码
    using System.IO;
    using System;
    using System.Collections.Generic;
    
    class Program
    {
    
    
           struct direction
                {
                    public int x;
                    public int y;
                };
    
                static direction[] directions = {
                                           new direction(){x = 1, y = 0},
                                           new direction(){x = 0, y = 1},
                                           new direction(){x = -1, y = 0},
                                           new direction(){x = 0, y = -1}
        };
    
    
    
         static int[,] visitedmap = resetVisitedMap();
    
    
    
             class Position
            {
                public int row;
                public int col;
                public Position  parent;
            };
    
            const int NOT_VISITED = 0;
            static int[,] map = {
                                    {0, 4, 0, 0, 0}, 
                                    {0, 0, 0, 0, 2},
                                    {0, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 0},
                                    {0, 0, 0, 0, 3}
                                };
    
            static void Main()
            {
                int[] who = new int[]{1,2,3,4};
                Position start = new Position();
                start.row = 0;
                start.col = 0;
                start.parent = null;
                Console.Write("("+start.row+","+start.col+")");
                //find 1
                foreach(int lookingfor in who){
                        start = bfs(start, map, lookingfor); 
                        Console.Write("->("+start.row+","+start.col+")");
                }
    
                Console.Write("\n Full path :\n");
                string path = "";
                while (start != null){
                    path = "->("+ start.row+","+start.col+")"+path;
                    start = start.parent;
                }
                Console.Write(path);
            }
    
            static Position bfs(Position present, int[,] map, int lookingfor)
            {
                Position[] poses = new Position[]{present};
                bool found = false;
    
               while (!found){
                   foreach (Position pos in poses){
                       if (map[pos.row, pos.col] == lookingfor){
                          // Console.Write("------->Success at : ("+pos.row+","+pos.col+")");
                            visitedmap = resetVisitedMap();
                           return pos;
                       }
                   }
                poses = cellsNear(poses);
               }
    
            return present;
            }
    
            static Position[] cellsNear(Position pos){
                //Console.WriteLine("Looking for cells near (" + pos.row +","+pos.col+")" );
                List<Position> result = new List<Position>();
                foreach (direction dir in directions){
                    int rowCalculated = pos.row + dir.x;
                    int colCalculated = pos.col + dir.y;
                    if (rowCalculated >= 0 &&
                            colCalculated >= 0 &&
                            rowCalculated < map.GetLength(0) && 
                            colCalculated < map.GetLength(1) &&
                            !visited(rowCalculated,colCalculated)){
                        visitedmap[rowCalculated,colCalculated] = 1;
                        Position posPath = new Position();
                        posPath.col = colCalculated;
                        posPath.row = rowCalculated;
                        posPath.parent = pos;
                        result.Add(posPath);
                        //Console.WriteLine("found children cell : ("+rowCalculated+","+colCalculated+")" );
                    }
                }
                //Console.WriteLine("\n");
                return result.ToArray();
            }
    
            static Position[] cellsNear(Position[] poses){
                List<Position> result = new List<Position>();
                foreach (Position pos in poses){
                    result.AddRange(cellsNear(pos));
                }
                return result.ToArray();
            }
    
            static bool visited(int row, int col)
                {
                    return visitedmap[row, col] == 1;
                }
        static int[,] resetVisitedMap(){
            return  new int[,]{
                                        {0, 0, 0, 0, 0}, 
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0},
                                        {0, 0, 0, 0, 0}
                                    };
        }
    }