蓝桥杯——迷宫(调试方便版)

#include<iostream>
using namespace std;
char map[4][6];//记录迷宫
char a[2000] ;//最短序列
char b[2000];//临时序列
int dep = 100;//最短步数
void dayinxulie()//打印目前序列
{
	cout << "目前序列:";
	int k = 0;
	while (b[k] != 'Z')
	{
		cout << b[k]; k++;
	}
	cout << endl;
}
void ifchenggong(int x,int y,int deep)//判断是否抵达是否为最短序列
{
	if (x == 3 && y == 5)
	{
		cout << "----------------------------------------" << endl;
		if (deep<dep||(deep==dep&&string(a)>string(b)))
		{
			dep = deep;
			cout << "该序列为目前最短序列" << endl;
			for (int j = 0; j < 20; j++)
			{
				a[j] = b[j];
			}
		}

		cout << endl;
		cout << "检测到成功序列"<<endl;
		cout << "该成功序列为:" << endl;
		dayinxulie();//打印目前序列
		cout << endl << "该序列共用" << deep  << "步达成"<<endl;
		cout << "----------------------------------------" << endl;
		cout << endl;
		cout << endl;
		return;
	}
}
void baomu(int deep)//开始时说明第几步
{
	cout << endl;
	cout << "正在第" << deep << "步" << endl;
}
void xieru(int deep,char fangxiang)//将移动写入临时序列
{
	b[deep] = fangxiang;
	cout << "第" << deep << "位写入"<<fangxiang << endl;
}
void dayinmigong()//打印迷宫
{
	cout << "目前迷宫"<<endl;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 6; j++)
		{
			cout << map[i][j];
		}cout << endl;
	}
}

void move1(char map[4][6],int x, int y,int deep)//如果此函数命名为move会引发bug
{
	
	baomu( deep);//开始时说明第几步

	ifchenggong(x,y,deep);//判断是否抵达

	//判断移动
	{
		//判断能否向上移动-----------------------------------------------------
	    if (x - 1 >= 0 && map[x - 1][y] == '0')
			{
				xieru(deep, 'U');//将移动写入临时序列

				dayinxulie();//打印目前序列

				map[x - 1][y] = '2';//记录走过迷宫

				dayinmigong();//打印迷宫

                
				move1(map, x - 1, y, deep + 1);//进入下一步
				
				//恢复迷宫和临时序列
				{
					map[x - 1][y] = '0';
					b[deep] = 'Z';
				}

				//打印退回后序列
				{
					dayinxulie();
					cout << endl;
					cout << endl;
				}
			}
		

		//判断能否向下移动-----------------------------------------------------
		if (x + 1 < 4 && map[x + 1][y] == '0')
		{

			xieru(deep, 'D');//将移动写入临时序列

			dayinxulie();//打印目前序列

			map[x +1][y] = '2';//记录走过迷宫

			dayinmigong();//打印迷宫

			move1(map, x + 1, y, deep + 1);//进入下一步

			//恢复迷宫和临时序列
			{
				map[x + 1][y] = '0';
				b[deep] = 'Z';
			}

			//打印退回后序列
			{
				dayinxulie();
				cout << endl;
				cout << endl;
			}
		}


		//判断能否向右移动-----------------------------------------------------
		if (y + 1 < 6 && map[x][y + 1] == '0')
		{
			xieru(deep, 'R');//将移动写入临时序列

			dayinxulie();//打印目前序列

			map[x ][y+1] = '2';//记录走过迷宫

			dayinmigong();//打印迷宫

			move1(map, x, y + 1, deep + 1);//进入下一步

			//恢复迷宫和临时序列
			{
				map[x][y + 1] = '0';
				b[deep] = 'Z';
			}

			//打印退回后序列
			{
				dayinxulie();
				cout << endl;
				cout << endl;
			}
		}


		//判断能否向左移动-----------------------------------------------------
		if (y - 1 >= 0 && map[x][y - 1] == '0')
		{
			xieru(deep, 'L');//将移动写入临时序列

			dayinxulie();//打印目前序列

			map[x ][y-1] = '2';//记录走过迷宫

			dayinmigong();//打印迷宫

			move1(map, x, y - 1, deep + 1);//进入下一步

			//恢复迷宫和临时序列
			{
				map[x][y - 1] = '0';
				b[deep] = 'Z';
			}

			//打印退回后序列
			{
				dayinxulie();
				cout << endl;
				cout << endl;
			}
		}
	}
	//移动结束

	//打印退回信息
     if (deep != 0)
	  {
		cout << "退回第" << deep - 1 << "步" << endl;
		dayinmigong();
	   }
   
	
	return;
}

int main()
{

	//初始化两个序列
	{for (int i = 0; i < 50; i++)
	{
		a[i] = 'Z';
		b[i] = 'Z';
	}
	}

	//初始化迷宫
	{
		for (int i = 0; i < 4; i++)
		for (int j = 0; j < 6; j++) {
			cin >> map[i][j];
		}
	}

	move1(map, 0, 0,0);

	//打印a序列
	{
		cout << "最终结果"<<endl;
		int k = 0;
		while (a[k] != 'Z')
		{
			cout << a[k] << endl; k++;
		}
	}
}

因为一些很神奇的bug(并且闲的无聊),为了调试方便写成了这个样子,不会递归的人把输出看一遍说不定能学会递归和(高内聚低耦合)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值