C++ 自定义栈实现迷宫求解

2025-05-27 0 23

C++ 自定义实现迷宫求解

一:迷宫求解

是一个锻炼我们的算法求解能力的问题,它的实现方法有很多;今天我们就介绍其中的用求解的方法。

二:什么是

大家应该都有往袋子里装东西的经历,在往袋子里装满东西之后,当我们去取的时候,总是先从最后放进去的东西的地方去取。也就是后进先出(FILO)。虽然的单向性用起来会没有链表那样可以在任意位置对数据进行操作,但是正因为如此也带来了很大的方便。

三:迷宫求解

现在我们要在下面的迷宫寻找一条可行的路径

?

1

2

3

4

5

6

7

8

9

10
1 1 1 1 1 1 1 1 1 1

1 0 0 1 1 0 0 0 0 1

1 0 0 0 1 0 1 0 0 1

1 0 1 0 0 0 1 1 0 1

1 0 1 0 0 0 1 1 1 1

1 0 1 1 1 0 1 1 1 1

1 0 1 1 1 0 1 1 1 1

1 1 1 1 1 0 0 0 1 1

1 1 1 1 1 1 1 0 0 1

1 1 1 1 1 1 1 1 1 1

首先我们需要在程序中表示上面的迷宫,该问题可以用数组实现

1:的定义

?

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
/************************************************************************/

/*自定义栈 */

/*通过自定义的简单栈以满足迷宫求解 */

/*功能:push() 将元素加入栈中 */

/* pop() 退栈;topValue() 获得栈顶元素 */

/* clear() 清除栈 length() 获得栈中元素个数*/

/************************************************************************/

#include <stack>

#include <iostream>

using namespace std;

template<typename Elem> class PathStack: public stack<Elem>

{

private:

int size;

int top;

Elem* listArray;

public:

PathStack(int sz = DefaultListSize){

size = sz;

top = 0;

listArray = new Elem[sz];

}

~PathStack(){ delete []listArray; }

void clear(){ top = 0; }

/****向栈中加入元素****/

bool push(const Elem& item);

/***********退栈**********/

Elem pop();

/********获得栈顶元素*******/

Elem topValue() const;

int length() const { return top; }

};

template<typename Elem>

bool PathStack<typename Elem>::push(const Elem& item){

if(top == size) return false;

listArray[top++] = item;

return true;

}

template<typename Elem>

Elem PathStack<typename Elem>::pop(){

Elem it;

if(top == 0) return it;

it = listArray[--top];

return it;

}

template<typename Elem>

Elem PathStack<typename Elem>::topValue() const{

Elem it;

if(top == 0) return it;

it = listArray[top - 1];

return it;

}

2:如何实现路径的寻找

1:设定寻找的方向,可以使用一个判断语句;判断起始位置周围哪个地方有路就将该位置的坐标加入到中,并将该位置标记(将改位置值改为2,既将走过的位置标记为2)

2:判断该位置周围是否还有路,若没有则退即退回到上一个位置;并将该位置做下另一个标记(将该位置值改为3,既将退位置值用3标记)

3:重复1,2步骤直到达到出口

路径寻找的类:

?

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
//迷宫求解的方法类

//功能:通过findPath() 方法实现对路径的查找

// 通过printPath()方法将路径打印出来

#include "PathStack.h"

#include <iostream>

using namespace std;

class MazeSolveMethod

{

private:

static int maze[10][10];//存放迷宫数据

PathStack<int> pathStack;//定义栈

public:

MazeSolveMethod():pathStack(100){

}

~MazeSolveMethod(){ }

void findPath(int startX,int startY);

void printPath() const;

};

int MazeSolveMethod::maze[10][10] = {

{1,1,1,1,1,1,1,1,1,1},

{1,0,0,1,1,0,0,0,0,1},

{1,0,0,0,1,0,1,0,0,1},

{1,0,1,0,0,0,1,1,0,1},

{1,0,1,0,0,0,1,1,1,1},

{1,0,1,1,1,0,1,1,1,1},

{1,0,1,1,1,0,1,1,1,1},

{1,1,1,1,1,0,0,0,1,1},

{1,1,1,1,1,1,1,0,0,1},

{1,1,1,1,1,1,1,1,1,1},

};

void MazeSolveMethod::findPath(int startX,int startY){

int x = startX;

int y = startY;

pathStack.push(x);

pathStack.push(y);

maze[x][y] = 2;

cout<<"进入方法!"<<endl;

while(true){

if(maze[x-1][y] == 0){

pathStack.push(--x);

pathStack.push(y);

maze[x][y] = 2;

}else if(maze[x][y-1] == 0){

pathStack.push(x);

pathStack.push(--y);

maze[x][y] = 2;

}else if(maze[x][y+1] == 0){

pathStack.push(x);

pathStack.push(++y);

maze[x][y] = 2;

}else if(maze[x+1][y] == 0){

pathStack.push(++x);

pathStack.push(y);

maze[x][y] = 2;

}

if(maze[x-1][y] != 0 && maze[x][y+1] != 0 && maze[x+1][y] != 0 && maze[x][y-1] != 0){

if(x >= 8 && y >= 8){

break;

}else{

maze[x][y] = 3;

y = pathStack.pop();

x = pathStack.pop();

}

y = pathStack.topValue();

int temp = pathStack.pop();

x = pathStack.topValue();

pathStack.push(temp);

}

}

}

void MazeSolveMethod::printPath() const{

cout<<"printPath"<<endl;

for(int i=0; i<10; i++){

for(int j=0; j<10; j++){

if(maze[i][j] == 2)

cout<<'*'<<" ";

else if(maze[i][j] == 3)

cout<<0<<" ";

else

cout<<1<<" ";

}

cout<<endl;

}

}

主函数类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
/************************************************************************/

/*迷宫求解----栈方法实现*/

//功能:通过对栈实现迷宫算法求解

//Author:Andrew

//Date :2012-10-20

/************************************************************************/

#include "MazeSolveMethod.h"

#include <iostream>

using std::cout;

using std::endl;

int main(){

MazeSolveMethod solve;

solve.findPath(1,1);

solve.printPath();

system("pause");

return 0;

}

将上面的代码运行后结果截图如下:

其中* 为路径

C++ 自定义栈实现迷宫求解

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 C++ 自定义栈实现迷宫求解 https://www.kuaiidc.com/74259.html

相关文章

发表评论
暂无评论