DFS迷宫问题如何找到邻居

时间:2017-04-13 12:20:54

标签: c# multidimensional-array graph-algorithm maze

我完全了解图表的工作原理(DFS)。我已经阅读了许多关于如何使用DFS构建迷宫路径查找解算器的教程,并且有一部分我没有得到。我如何知道一个顶点的邻居是谁? Forinstens我有这个迷宫: maze

我已将所有字符串放入名为“names”的二维数组中。所以如果我写信:

names[0,0]  // it contains the string    +

如果我写:

names[0,1] // it contains the string     -

如果我写:

names[1,0]  //it contains the string     B

等。但名称[1,0]如何知道它的邻居是名字[0,0],名字[2,0]和名字[1,1]?

2 个答案:

答案 0 :(得分:0)

我启动了代码并从您提供的链接中加载了第一行迷宫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;



namespace ConsoleApplication49
{
    class Program
    {

        static void Main(string[] args)
        {
            Cell maze = new Cell();
            maze.Load();

        }

    }
    public class Cell
    {
        public static Cell[,] maze = new Cell[10, 10];

        public static KeyValuePair<int, int> start = new KeyValuePair<int, int>();
        public static KeyValuePair<int, int> end = new KeyValuePair<int, int>();

        public bool topBlock { get; set; }
        public bool rightBlock { get; set; }
        public bool bottomBlock { get; set; }
        public bool leftBlock { get; set; }

        public void Load()
        {
            start = new KeyValuePair<int, int>(0, 0);
            end = new KeyValuePair<int, int>(9, 9);

            maze[0, 0] = new Cell() { topBlock = true, rightBlock = true, bottomBlock = false, leftBlock = false };
            maze[0, 1] = new Cell() { topBlock = true, rightBlock = false, bottomBlock = false, leftBlock = true };
            maze[0, 2] = new Cell() { topBlock = true, rightBlock = true, bottomBlock = false, leftBlock = false };
            maze[0, 3] = new Cell() { topBlock = true, rightBlock = true, bottomBlock = false, leftBlock = true };
            maze[0, 4] = new Cell() { topBlock = true, rightBlock = false, bottomBlock = false, leftBlock = true };
            maze[0, 5] = new Cell() { topBlock = true, rightBlock = false, bottomBlock = true, leftBlock = false };
            maze[0, 6] = new Cell() { topBlock = true, rightBlock = true, bottomBlock = false, leftBlock = false };
            maze[0, 7] = new Cell() { topBlock = true, rightBlock = false, bottomBlock = false, leftBlock = true };
            maze[0, 8] = new Cell() { topBlock = true, rightBlock = false, bottomBlock = true, leftBlock = false };
            maze[0, 9] = new Cell() { topBlock = true, rightBlock = true, bottomBlock = false, leftBlock = false };
        }
    }

}

答案 1 :(得分:0)

你必须有一个定义迷宫的方法。它可以是输入文件而不是硬编码。使用一些智能包装方案,如顶线= 1,右线= 2,底线= 4,左线= 8.整个迷宫可以这样定义:

            int[,] maze = {
                          {0x39,0x3B,0x95,0x39,0x53},
                          {0xC6,0xC4,0x6B,0xAB,0x32},
                          {0x95,0x3D,0x3A,0x83,0xAA},
                          {0xAB,0x85,0x6C,0x68,0x6A},
                          {0xAA,0xC5,0x39,0x3A,0x96},
                          {0xC6,0x93,0xC6,0xC2,0xAB},
                          {0xD5,0x2C,0x17,0x92,0xC6},
                          {0x95,0x6D,0x45,0x6C,0x3B},
                          {0xC5,0x55,0x39,0x57,0x86},
                          {0xD5,0x55,0x6C,0x55,0x45}
                          };