高级农民
- 积分
- 1038
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-9-7
- 最后登录
- 1970-1-1
|
本帖最后由 topnessman 于 2016-7-9 12:11 编辑
交作业啦,好开心! 还有最后一个homework 和project.
这是part2的答案:
First we create a graph of cells, in which every cell has directed edges from itself to all its neighbors.
Then we randomly choose a initial cell, eg (0,0) cell, call DFS() on that node. The DFS() here is the same as the lecture's version, except a difference: if we encounter a cell v that is visited already from current node u, then we remove the edge from u to v.
Finally, after the depth-first search finishes, the correct maze will be created successfully.
1) How would your algorithm ensure that there is a path between every pair of cells, but no more than one path between any pair of cells (i.e., no cycles)?
Always has path: In the beginnnig, all the cells are completely connected. So every cell can be visited from the initial cell.
No cycles: if we encounter a visited cell u from cell v, it means that there is already a path from u to v(order doesn't matter). But now we encounter another edge from v to u, which will make a cycle. Therefore, this is the reason why we need to remove the directed edge from v to u. If u finds its neighbor v also visited, directed edge from u to v will also be removed. Then there will be no edges between u and v, then it means that the wall between u and v exists. By removing the directed edge from a node to an already visited neighbor node, we can make sure that there won't be cycles.
2) How does your algorithm use random numbers to generate a different maze each time? Specifically, what decision should be made by random numbers at each recursive invocation of the depth-first search method?
We can generate a random number between 0 to (# of neighbors - 1), and choose (random # + 1)th neighbor of current cell, and recursively call DFS() on that neighbor.
|
|