java图的深度优先遍历实现随机生成迷宫

2025-05-29 0 67

最近经常在机房看同学在玩一个走迷宫的游戏,比较有趣,自己也用java写一个实现随机生成迷宫的算法,其实就是一个图的深度优先遍历算法.基本思想就是,迷宫中的每个点都有四面墙,然后呢。

1、从任意一点开始访问(我的算法中固定是从(0,0)点开始),往四个方向中的随机一个访问(每访问到一个可访问的点,就去掉该点的那个方向的墙),被访问点继续以这种方识向下进行访问。
2、对每个被访问的点都被标识为已访问,当一个点对某个方向进行访问时我们首先会判断被访问点是否已被访问,或者触到边界.如果该点四个方向皆已访问或已无法访问,就退回上一个点。上一个点继续这个过程。

这样一次遍历下来,可以确定每个点都被访问过,而且由于每次访问的方向都是随机,这就会形成许多不同遍历情况,同时每两个点之间的路径是唯一,也就形成不同的迷宫,且是起点到终点只有唯一路径,这是由于图的深度遍历算法的特点所决定的。算法的实现上,主要是利用栈,第一次,先把第一个点压进栈里,每访问到一个点,就把该点压进栈里,我们再对栈顶的点进行四个方向的随机访问,访问到新点,又把新点压进去,一旦这个点四个方向都无法访问了,就让该点退栈,再对栈顶的点的四个方向进行访问,以此类推,直到栈里的点都全部退出了,我们的遍历就成功了,这是一个递归的过程,这个算法自然可以用递归的方法来实现,不过这里我这样做,而是手工用一个数组作为栈来实现,呵呵~~说了这么多,也不知道自己要表达的有没表达出来。不过我感觉我的具体代码设计还是写的不好,还有很多地方缺乏完善和优化,权当是算法练习,以下是两个关键类的核心代码,至于表示层的代码就不贴出来了,因为那些都很琐碎。

下面是效果图:

java图的深度优先遍历实现随机生成迷宫

迷宫的类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124
//作者:zhongzw

//邮箱:zhong317@126.com

package cn.zhongzw.model;

import java.util.arraylist;

import java.util.random;

public class mazemodel {

private int width = 0;

private int height = 0;

private random rnd = new random();

public mazemodel() {

this.width = 50; //迷宫宽度

this.height = 50; //迷宫高度

}

public int getwidth() {

return width;

}

public void setwidth(int width) {

this.width = width;

}

public int getheight() {

return height;

}

public void setheight(int height) {

this.height = height;

}

public mazemodel(int width, int height) {

super();

this.width = width;

this.height = height;

}

public arraylist < mazepoint > getmaze() {

arraylist < mazepoint > maze = new arraylist < mazepoint > ();

for (int h = 0; h < height; h++) {

for (int w = 0; w < width; w++) {

mazepoint point = new mazepoint(w, h);

maze.add(point);

}

}

return createmaze(maze);

}

private arraylist < mazepoint > createmaze(arraylist < mazepoint > maze) {

int top = 0;

int x = 0;

int y = 0;

arraylist < mazepoint > team = new arraylist < mazepoint > ();

team.add(maze.get(x + y * width));

while (top >= 0) {

int[] val = new int[] {

-1, -1, -1, -1

};

int times = 0;

boolean flag = false;

mazepoint pt = (mazepoint) team.get(top);

x = pt.getx();

y = pt.gety();

pt.visted = true;

ro1: while (times < 4) {

int dir = rnd.nextint(4);

if (val[dir] == dir)

continue;

else

val[dir] = dir;

switch (dir) {

case 0: // 左边

if ((x - 1) >= 0 && maze.get(x - 1 + y * width).visted == false) {

maze.get(x + y * width).setleft();

maze.get(x - 1 + y * width).setright();

team.add(maze.get(x - 1 + y * width));

top++;

flag = true;

break ro1;

}

break;

case 1: // 右边

if ((x + 1) < width && maze.get(x + 1 + y * width).visted == false) {

maze.get(x + y * width).setright();

maze.get(x + 1 + y * width).setleft();

team.add(maze.get(x + 1 + y * width));

top++;

flag = true;

break ro1;

}

break;

case 2: // 上边

if ((y - 1) >= 0 && maze.get(x + (y - 1) * width).visted == false) {

maze.get(x + y * width).setup();

maze.get(x + (y - 1) * width).setdown();

team.add(maze.get(x + (y - 1) * width));

top++;

flag = true;

break ro1;

}

break;

case 3: // 下边

if ((y + 1) < height && maze.get(x + (y + 1) * width).visted == false) {

maze.get(x + y * width).setdown();

maze.get(x + (y + 1) * width).setup();

team.add(maze.get(x + (y + 1) * width));

top++;

flag = true;

break ro1;

}

break;

}

times += 1;

}

if (!flag) {

team.remove(top);

top -= 1;

}

}

return maze;

}

}

迷宫

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59
//作者:zhongzw

//邮箱:zhong317@126.com

package cn.zhongzw.model;

import java.util.*;

import java.lang.*;

public class mazepoint {

private int left = 0;

private int right = 0;

private int up = 0;

private int down = 0;

private int x;

private int y;

public boolean visted;

public mazepoint(int x, int y) {

this.x = x;

this.y = y;

}

public int getleft() {

return left;

}

public void setleft() {

this.left = 1;

}

public int getright() {

return right;

}

public void setright() {

this.right = 1;

}

public int getup() {

return up;

}

public void setup() {

this.up = 1;

}

public int getdown() {

return down;

}

public void setdown() {

this.down = 1;

}

public int getx() {

return x;

}

public void setx(int x) {

this.x = x;

}

public int gety() {

return y;

}

public void sety(int y) {

this.y = y;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/woshizoe/article/details/27345201

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java图的深度优先遍历实现随机生成迷宫 https://www.kuaiidc.com/112588.html

相关文章

发表评论
暂无评论