C# BFS求助
  • 板块学术版
  • 楼主UT_MC_wuming
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/2 20:54
  • 上次更新2023/10/27 09:08:56
查看原帖
C# BFS求助
383781
UT_MC_wuming楼主2022/10/2 20:54
namespace BFS
{
    public class Program
    {
        //const int[][] d = new int[4][](0,1,0,-1,1,0,-1,0);
        readonly static int[,] d = new int[4, 2] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
        //public static int[][] _map = new int[114][];


        public static void BFS(int n)
        {
            Queue<int> x = new();
            Queue<int> y = new();
            x.Enqueue(1);
            y.Enqueue(1);
            _Map[1][1].step = 0;
            _Map[1][1].vis = true;
            while (x.Count != 0 || y.Count != 0)
            {
                int fx = x.Peek();
                int fy = y.Peek();
                for (int i = 0; i < 4; i++)
                {
                    int nx = fx + d[i, 0];
                    int ny = fy + d[i, 1];
                    if (1 <= nx && nx <= n && 1 <= ny && ny <= n && _Map[nx][ny].vis == false)
                    {
                        x.Enqueue(nx);
                        y.Enqueue(ny);
                        _Map[nx][ny].step = _Map[fx][fy].step + 1;
                        _Map[nx][ny].vis = true;
                    }
                }
                x.Dequeue();
                y.Dequeue();
            }
        }
        public static MAP[][] _Map = new MAP[114][];
        private static void Main(string[] args)
        {

            int n;
            n = Convert.ToInt32(Console.ReadLine());
            
            //Console.Read();
            //Console.ReadKey();
            //var program = new Program();
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    //map[i][j] = Convert.ToInt32(Console.Read());
                    int temp;
                    var m = new MAP();
                    temp = Console.Read();
                    m.C = (char)temp;
                    _Map[i][j] = m;
                }
            }
            BFS(n);
            Console.WriteLine(_Map[n][n].step);
        }
        public class MAP
        {
            public bool vis;
            public int step;
            public char c;
            public char C
            {
                get { return c; }
                set
                {
                    //C = '.';
                    if (value == '.') vis = false;
                    else if (value == '#') vis = true;
                    c = value;
                }
            }
        }
    }
}

VS总是报错System.NullReferenceException:“Object reference not set to an instance of an object.”

求大佬怎么修改

2022/10/2 20:54
加载中...