c#迷宫求解器图形

本文关键字:图形 迷宫 | 更新日期: 2023-09-27 18:08:06

我的迷宫求解器有问题,当我运行求解器时,它在图形变量

处给出错误
 private bool solveMaze(int xPos, int yPos, bool[,] alreadySearched)
        {
            bool correctPath = false;
            bool shouldCheck = true;
            Bitmap map = (Bitmap)Mazebox.Image;
            Graphics gfx = null;
            gfx = Graphics.FromImage(map);
            Brush b = new SolidBrush(Color.CornflowerBlue);
            //out of index check
            if (xPos >= XTILES || xPos < 0 || yPos >= YTILES || yPos < 0)
                shouldCheck = false;
            if (map.GetPixel(xPos , yPos) == Color.Green)
            {
                correctPath = true;
            }
            //Search the Tile
            if (shouldCheck)
            {
                //mark tile as searched
                alreadySearched[xPos, yPos] = true;
                //Check right tile
                correctPath = correctPath || solveMaze(xPos + 1, yPos, alreadySearched);
                //Check down tile
                correctPath = correctPath || solveMaze(xPos, yPos + 1, alreadySearched);
                //check left tile
                correctPath = correctPath || solveMaze(xPos - 1, yPos, alreadySearched);
                //check up tile
                correctPath = correctPath || solveMaze(xPos, yPos - 1, alreadySearched);
            }
            //make correct path gray
            if (correctPath)
            {
                gfx.FillRectangle(b, xPos, yPos, 10, 10);
                Mazebox.Image = map;
            }
            return correctPath;
        }

我认为问题是他经常打开它,然后它崩溃了(无限)有人能帮我解决这个问题吗?

c#迷宫求解器图形

您从未使用您的alreadySearched字段。

你可以通过插入

 shouldCheck = shouldCheck && !alreadySearched[xPos, yPos];