2020-11-18

博客围绕数据结构(C语言版)“栈与队列”章节的迷宫求解问题展开。作者先给出代码,指出将特定通道块赋值为零程序才有解,若通道通会报异常,猜测是弹栈问题。后经调试解决错误,原因是将指示方向的di默认指定为 -1,每次循环运行了5次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构(C语言版)”栈与队列“章节迷宫求解问题

先将代码给出

#include"iostream"
using namespace std;




#define ROW 10
#define COL 10


int maze[ROW][COL] = {
	{0,0,0,0,0,0,0,0,0,0},
	{0,1,1,0,1,1,1,0,0,0},
	{0,1,1,0,1,1,1,0,1,0},
	{0,1,1,0,1,0,0,1,1,0},//将这行第四个数赋为零,这个程序才可能有一个解
	{0,1,0,0,0,1,1,1,1,0},
	{0,1,1,1,0,1,1,1,1,0},
	{0,1,0,1,1,1,0,1,1,0},
	{0,1,0,0,0,1,0,0,1,0},
	{0,0,1,1,1,1,1,1,1,0},
	{0,0,0,0,0,0,0,0,0,0}
};



typedef struct {

	int x;
	int y;
}Direction;//点集合

typedef struct {
	int x, y;//当前行和列
	int di;//方向
}Box;

typedef struct {
	Box* base;
	Box* top;
	int size;
}SElemType;


int InitStack(SElemType &S)
{
	S.base = (Box*)malloc(100 * sizeof(SElemType));
	if (!S.base)
		return 0;
	S.top = S.base;
	S.size = 100;
	return 1;
}

int isEmpty(const SElemType &s)
{
	if (s.base == s.top)
		return 1;
	else
		return 0;
}

int LengthStack(SElemType s)
{
	return s.size;
}

int Push(SElemType &S, Box temp)
{
	if (S.top - S.base >= S.size)
	{
		S.base = (Box*)realloc(S.base, (S.size + 10) * sizeof(Box));
		if (S.base)
			return 0;
		S.top = S.base + S.size;
		S.size += 10;
	}
	*S.top++ = temp;
	return 1;
}

int Pop(SElemType &s, Box &temp)
{
	if (s.top == s.base)
		return 0;
	temp = *--s.top;
}


void print(SElemType &s);
int findMaze(SElemType &s, Direction start, Direction end)
{
	Direction direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
	InitStack(s);
	Box temp;
	temp.x = start.x;
	temp.y = start.y;
	temp.di = -1;
	int line, col, di, flag;
	maze[start.x][start.y] = -1;
	Push(s, temp);
	while (!isEmpty(s))
	{
		flag = 0;
		di = temp.di;
		if (temp.x == end.x&&temp.y == end.y)//到达终点,打印
		{
			print(s);
			return 1;
		}
		while (di < 4 && flag == 0)//方向没有走完 并且 某个方向的下一个通道块没有墙
		{
			di++;
			col = temp.y + direct[di].y;
			line = temp.x + direct[di].x;
			if (maze[line][col] == 1)
			{
				flag++;
			}
			temp.di = di;
		}
		if (1 == flag)//说明有路可走,将当前的点方向压栈
		{
			temp.x = line;
			temp.y = col;
			temp.di = -1;
			maze[temp.x][temp.y] = -1;
			Push(s, temp);//更改temp
		}
		else
		{
			maze[temp.x][temp.y] = 0;//已经没有路可走了,擦掉自己留下的痕迹。
			Pop(s, temp);//存在一个问题,已经走过的路,可能再走一遍
		}	
	}
	return 0;

}


void print(SElemType &s)
{
	int i = 0;
	Box temp;
	while (!isEmpty(s))
	{
		Pop(s, temp);
		cout << " <" << temp.x << "," << temp.y << "> ";
		if (i % 5 == 0)
			cout << endl;
		i++;
	}
}

void test()
{
	SElemType s;
	Direction start = { 1,1};
	Direction end = { 8,8 };
	int rs = findMaze(s,start,end);
	if (rs)
		cout << "found it\n";
	else
		cout << "don't found it\n";
	print(s);
}


int main(void)
{

	//print();
	test();
	//test1();

	
	return 0;
}


我的问题,将这4行第四个数赋为零(下面叫通道块),这个程序才可能有一个解,不清楚为什么如果这个通道块如果通,就会报异常。我猜测是弹栈的时候,可能又有数从走了,具体原因,不清楚。感觉对迷宫算法不是特别理解。希望大佬指教

问题我已经解决了,通过慢慢的调试。错误在于我将 di 指示方向的默认指定为-1,而每次循环时,实际上运行了5次。

修改后的代码

#include"iostream"
using namespace std;




#define ROW 10
#define COL 10



int maze[ROW][COL] = {
	{0,0,0,0,0,0,0,0,0,0},
	{0,1,1,0,1,1,1,0,0,0},
	{0,1,1,0,1,1,1,0,1,0},
	{0,1,1,1,1,0,0,1,1,0},
	{0,1,0,0,0,1,1,1,1,0},
	{0,1,1,1,0,1,1,1,1,0},
	{0,1,0,1,1,1,0,1,1,0},
	{0,1,0,0,0,1,0,0,1,0},
	{0,0,1,1,1,1,1,1,1,0},
	{0,0,0,0,0,0,0,0,0,0}
};



typedef struct {

	int x;
	int y;
}Direction;

typedef struct {
	int x, y;//当前行和列
	int di;//方向
}Box;

typedef struct {
	Box* base;
	Box* top;
	int size;
}SElemType;


int InitStack(SElemType &S)
{
	S.base = (Box*)malloc(100 * sizeof(SElemType));
	if (!S.base)
		return 0;
	S.top = S.base;
	S.size = 100;
	return 1;
}

int isEmpty(const SElemType &s)
{
	if (s.base == s.top)
		return 1;
	else
		return 0;
}

int LengthStack(SElemType s)
{
	return s.size;
}

int Push(SElemType &S, Box temp)
{
	if (S.top - S.base >= S.size)
	{
		S.base = (Box*)realloc(S.base, (S.size + 10) * sizeof(Box));
		if (S.base)
			return 0;
		S.top = S.base + S.size;
		S.size += 10;
	}
	*S.top++ = temp;
	return 1;
}

int Pop(SElemType &s, Box &temp)
{
	if (s.top == s.base)
		return 0;
	temp = *--s.top;
}

int Pass(Direction curpos)
{
	return maze[curpos.x][curpos.y];
}

void print(SElemType &s);
int findMaze(SElemType &s, Direction start, Direction end)
{
	Direction direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };
	InitStack(s);
	Box temp;
	int curstep = 1;
	temp.x = start.x;
	temp.y = start.y;
	temp.di = 0;
	int line, col, di, flag;
	maze[start.x][start.y] = -1;
	Push(s, temp);
	while (!isEmpty(s))
	{
		flag = 0;
		di = temp.di;
		if (temp.x == end.x&&temp.y == end.y)//到达终点,打印
		{
			print(s);
			return 1;
		}
		while (di < 4 && flag == 0)//方向没有走完 并且 某个方向的下一个通道块没有墙
		{
			
			col = temp.y + direct[di].y;
			line = temp.x + direct[di].x;
			if (maze[line][col] == 1)
			{
				flag++;
			}
			temp.di = di;
			di++;
		}
		if (1 == flag)//说明有路可走,将当前的点方向压栈
		{
			temp.x = line;
			temp.y = col;
			temp.di = 0;
			maze[temp.x][temp.y] = -1;
			Push(s, temp);//更改temp
		}
		else
		{
			maze[temp.x][temp.y] = 0;//已经没有路可走了,擦掉自己留下的痕迹。
			Pop(s, temp);//存在一个问题,已经走过的路,可能再走一遍
		}
	}
	return 0;

}


void print(SElemType &s)
{
	int i = 0;
	Box temp;
	while (!isEmpty(s))
	{
		Pop(s, temp);
		cout << " <" << temp.x << "," << temp.y << "> ";
		if (i % 5 == 0)
			cout << endl;
		i++;
	}
}

void print1()
{
	cout << endl;
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			cout << maze[i][j] << "\t";
		}
		cout << endl;
	}
}
void test()
{
	SElemType s;
	Direction start = { 1,1 };
	Direction end = { 8,8 };
	int rs = findMaze(s, start, end);
	if (rs)
		cout << "found it\n";
	else
		cout << "don't found it\n";
	print1();
}




int main(void)
{
	test();
	return 0;
}


结果打印,-1为走的路径

有错误请让我知道,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值