-1

当我运行解算器时,我的迷宫解算器出现问题,它在图形变量处出现错误

 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;
        }

我认为问题是他打开了很多然后它崩溃(无限)有人可以帮我解决这个问题吗?

4

1 回答 1

0

你永远不会使用你的alreadySearched领域。

你可以通过插入来做到这一点

 shouldCheck = shouldCheck && !alreadySearched[xPos, yPos];
于 2013-04-07T12:17:14.083 回答