2012-05-19 61 views
1

我想将迷宫数据结构转换成图形。迷宫就像一个网格和细胞之间的一些墙壁。如何将迷宫转换为图形?

maze[8][8][4] is how the maze is represented. 
If maze[i][j][0] = 1 it means you can't go up from (i,j) 
if maze[i][j][1] = 1 it means you can't go down from (i,j) 
// and so on 

我想将这个迷宫转换为图形我该怎么做?从最初的矩阵

1,创建一个邻接矩阵:

回答

3

你可以做到这两种方式。邻接矩阵的形式为:

h[i][j] = 0, if there is no direct link from i to j 
(i and j are not neighbors in the maze) 
h[i][j] = 1, if there is a direct link from i to j 
(i and j are neighbors in the maze) 

2.创建每个节点的邻居列表:j是在i邻居列表,如果有ij之间的直接联系。

+0

请记住,如果h [i] [j] == H [J] [I],你只需要存储/计算矩阵的一半。 – Gnosophilon

+0

查看[this](http://stackoverflow.com/questions/2218322/what-is-better-adjacency-lists-or-adjacency-matrix-for-graph-problems-in-c/5419933#5419933)在考虑内存效率时选择什么信息 – keyser

1

对于每个邻居单元格,如果它们之间没有隔离墙,则使它们在图形中连接。

2

将输入数据视为邻接矩阵。将迷宫想象成一条路径,每个连接段创建顶点。每个角落都是一个节点(包括开始和结束)。如果连接存在,矩阵中有一个值。如果不是,你可以使用INF或-1来告诉没有路由。无论如何,你可以使用Dijkstra的最短数学算法来解决这个问题。网上有很多关于它的信息。

http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/

+0

当前域不是所有者,所以链接指向无处。 – Evil