#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(并且闲的无聊),为了调试方便写成了这个样子,不会递归的人把输出看一遍说不定能学会递归和(高内聚低耦合)。