深度与广度优先搜索迷宫问题c语言,迷宫问题的求解方式:应用深度优先和广度优先的搜索...

用堆栈实现迷宫问题,二维数组表示迷宫:1表示墙壁,0表示可以走的路,只能横着走或竖着走

不能斜着走,要求编程实现找到从左上角到右下角的路线//深度优先:有解就退出搜索(不一定是最优解)

#include

#include

using namespace std;

#define ROW 5

#define COL 5

typedef struct point

{

int _row;

int _col;

}Point;

Point stack[512];

int top= 0;

void Push(Point& p)

{

stack[top++] = p;

}

const Point& Pop()

{

return stack[--top];

}

int IsEmpty()

{

return top==0;

}

int maze[ROW][COL] = {

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

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

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

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

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

};

void PrintMaze()

{

for (int i = 0; i 

{

for (int j = 0; j 

{

cout <

}

cout <

}

cout <

}

Point Prev[ROW][COL] = {

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

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

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

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

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

};

void Visit(int row, int col,Point& p)

{

Point tmp = { row, col};

maze[row][col] = 2;

Prev[row][col] = p;

Push(tmp);

}

void Test1()

{

Point p = { 0, 0};

maze[p._row][p._col] = 2;

Push(p);

while (!IsEmpty())

{

p = Pop();

if (p._row == ROW - 1 && p._col == COL - 1)

break;

//up

if (p._row - 1 >= 0 && maze[p._row - 1][p._col] == 0)

Visit(p._row - 1, p._col,p);

//down

if (p._row + 1 

Visit(p._row + 1, p._col,p);

//left

if (p._col - 1 >= 0 && maze[p._row][p._col - 1] == 0)

Visit(p._row, p._col - 1,p);

//right

if (p._col + 1 

Visit(p._row, p._col + 1,p);

}

if (p._row == ROW - 1 && p._col == COL - 1)

{

printf("(%d,%d)\n", p._row, p._col);

while ((Prev[p._row][p._col])._row != -1)

{

p = Prev[p._row][p._col];

printf("(%d,%d)\n", p._row, p._col);

}

}

else

cout <

}

f72b47ca70452daad09830dda5850c5d.png//广度求得最优路径,找到后停止搜索

#include

using namespace std;

#define ROW 5

#define COL 5

typedef struct point

{

int _row;

int _col;

int _prev;

}Point;

Point queue[512];

int head = 0;

int tail = 0;

void Enqueue(Point& p)

{

queue[tail++] = p;

}

const Point& Dequeue()

{

return queue[head++];

}

int IsEmpty()

{

return head == tail;

}

int maze[ROW][COL] = {

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

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

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

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

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

};

void PrintMaze()

{

for (int i = 0; i 

{

for (int j = 0; j 

{

cout <

}

cout <

}

cout <

}

void Visit(int row, int col)

{

Point tmp = { row, col, head - 1 };

maze[row][col] = 2;

Enqueue(tmp);

}

void Test1()

{

Point p = { 0, 0, -1 };

maze[p._row][p._col] = 2;

Enqueue(p);

while (!IsEmpty())

{

p = Dequeue();

if (p._row == ROW - 1 && p._col == COL - 1)

break;

//up

if (p._row - 1 >= 0 && maze[p._row - 1][p._col] == 0)

Visit(p._row - 1, p._col);

//down

if (p._row + 1 

Visit(p._row + 1, p._col);

//left

if (p._col - 1 >= 0 && maze[p._row][p._col - 1] == 0)

Visit(p._row, p._col - 1);

//right

if (p._col + 1 

Visit(p._row, p._col + 1);

}

if (p._row == ROW - 1 && p._col == COL - 1)

{

printf("(%d,%d)\n", p._row, p._col);

while (p._prev != -1)

{

p = queue[p._prev];

printf("(%d,%d)\n", p._row, p._col);

}

}

else

cout <

}

1ae92772420aa073d0bad004116df2b1.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值