简体   繁体   English

脱节集和迷宫创建

[英]Disjoint set and maze creation

I am attempting to create a maze using a premade Disjoint set class. 我正在尝试使用预制的不相交集合类创建迷宫。 I have created a Cell class that contains a boolean variable for each of the four walls. 我创建了一个Cell类,其中包含四个壁中每个壁的布尔变量。 The problem is, how do I create a disjoint set of cell objects? 问题是,如何创建不相交的单元格对象集? That way I can union the cells and change the boolean variables accordingly. 这样,我可以合并单元格并相应地更改布尔变量。

http://users.cis.fiu.edu/~weiss/dsaajava3/code/DisjSets.java http://users.cis.fiu.edu/~weiss/dsaajava3/code/DisjSets.java

That is the code for the disjoint set 那是不相交集的代码

A disjoint set data-structure is not suited for this particular problem. 不相交的集合数据结构不适合此特定问题。

Attempting to force your solution to use it is just going to give you poor code and a bad design. 试图强迫您的解决方案使用它只会给您糟糕的代码和糟糕的设计。

Not to mention that there is a much simpler, more elegant solution. 更不用说有一个更简单,更优雅的解决方案。

In order to generate a perfect (one unique path between any two points) maze: 为了生成一个完美的(任意两点之间的唯一路径)迷宫:

  1. create an array of N by M cells, keeping track of each wall in each cell 创建一个由N个单元格组成的N个数组,跟踪每个单元格中的每个墙
  2. choose a cell at random 随机选择一个单元
  3. from your current cell (A), select a non-visited neighbour (B), if there are no such neighbours, go back to your previously visited cell and start from step (3) 从您当前的单元格(A)中选择一个未访问的邻居(B),如果没有这样的邻居,请返回您以前访问的单元格,然后从步骤(3)开始
  4. break down the wall between A and B 打破A和B之间的墙
  5. mark B as the current cell (or push it in a list or similar datastructure) 将B标记为当前单元格(或将其推送到列表或类似的数据结构中)
  6. if all cells have been visited, end. 如果已经访问了所有单元,则结束。 else go back to step (3) 否则回到步骤(3)

All you need is a grid, and a list. 您只需要一个网格和一个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM