如何正确检查迷宫上的可用路径

时间:2017-08-11 09:18:46

标签: c# algorithm console-application maze

我正在学习c#和编程。我有这项任务要找到一条走出迷宫的道路。

我有一个2D矩阵,其中:

  • 5 - 是终点。
  • 3 - 是我的起点。
  • 2 - 是步行路径。
  • 1 - 是墙。

我写了这段代码:

public static bool PathFinder(MapFile map)
    {
        for (int y = 0; y < map.height; y++)
        {
            for (int x = 0; x < map.width; x++)
            {
                if (map.Matrix[x, y] == 3)
                {
                    map.Matrix[x, y] = 11;
                    map.start = true;
                    return map.end;
                }
                if (map.Matrix[x, y] == 2)
                {
                    map.Matrix[x, y] = 9;
                    return map.end;
                }
                if (map.Matrix[x, y] == 1)
                {
                    return map.end;
                }
                if (map.Matrix[x, y] == 5)
                {
                    map.Matrix[x, y] = 11;
                    map.end = true;
                    return map.end;
                }
            }
        }

问题是我不明白如何检查矩阵上的点是否是有效路径,然后走到下一点。

我应该考虑哪些方面以及尝试寻找解决方案的方向?

OBS。:

map.start是一个布尔值,如果找到迷宫的起点,则返回true。

map.end是一个布尔值,如果找到迷宫的末尾则返回true。

值9和11只是我用来改变控制台上该点颜色的参考。

0 个答案:

没有答案
相关问题