迷宫求解
问题描述:
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, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 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表示墙,0表示通道,已知起点坐标(1,1),终点坐标(8,8)
求应该怎样走,才能到达? ( 默认迷宫是有解的 )
思想:
利用栈解决,把每次走过的点的坐标压入栈,如果发现当前点没有继续可以走的路,就开始往回走( 把走过的点的坐标弹出栈),为了避免再次走 走过的路,应该将走过的路进行标记(把表示通道的0改为其他数,本例中是8)
步骤:
初始条件,初始的位置为(1,1)
1. 当前位置(x,y),首先判断当前位置是不是出口(8,8), 如果是 步骤4 ;如果不是 , 跳到步骤2 .
2. 判断该点的上方(x-1,y)是否可以走,可以走,当前位置压进栈 ,进入步骤3; 如果不可以,
判断该点的右方(x,y-1)是否可以走,可以走,当前位置压进栈 ,进入步骤3;如果不可以,
判断该点的下方(x+1,y)是否可以走,可以走,当前位置压进栈,进入步骤3;如果不可以,
判断该点的左方(x,y+1)是否可以走,可以走,当前位置压进栈,进入步骤3;如果还不可以,那么怎么办呢? 前方的路全堵了(又不能飞),这时做个标记(标记为8)这个点不要再来了, 然后往回走了, 这时就要用栈了,把刚才走的路弹出来 , 根据弹出来的值 , 当前位置更新为这个弹出来的值,再次进入步骤2,进行判断.
3. 把要走的下一个位置更新为当前位置 , 回到步骤1
4.找到迷宫出口,回家咯
源代码
#include<iostream>
#include<stack>
#include <stdio.h>
#define UP 1
#define RIGHT 2
#define DOWN 3
#define LEFT 4
using namespace std;
struct node
{
int x;
int y;
};
int a[][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, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 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 display()
{
cout << "\n\t display the maze\n";
cout <<"\t---------------------"<<endl;
for(int i = 0 ; i < 10 ; i++ )
{
cout << '\t';
for( int j = 0 ; j < 10 ; j++ )
{
cout << a[i][j] << " ";
}
cout <<endl;
}
cout <<"\t---------------------"<<endl;
}
int main(int argc,char *argv[])
{
stack<node> maze;//遍历路径时使用的栈
stack<node> result;//最终的路径存放栈
int dest_x ,dest_y;//终点坐标,迷宫的出口
int ori_x,ori_y;//起点坐标,迷宫的入口
int run_x,run_y;//当前位置
ori_x = ori_y =1;//定义1,1为入口
dest_x = dest_y = 8;//定义8,8为出口
run_x = run_y =ori_x;
cout <<"\n\n";
cout << "\tEntrance " << ori_x << " " << ori_y<<endl;
cout << "\tExports " << dest_x << " " <<dest_y << endl;
node temp;
display();
while((run_x != dest_x) || (run_y != dest_y))
{
//向上走
if(a[run_x-1][run_y] == 0)//向上可以走
{
temp.x = run_x;//当前位置保存在 temp中
temp.y = run_y;
a[run_x][run_y] = 2;//标识当前位置我走了,脚印
run_x -= 1;//这步就是往上走了
maze.push(temp);//把你刚才的当前位置压到栈里面,做个记录
}
//向右走
else if(a[run_x][run_y+1] == 0)
{
temp.x = run_x;
temp.y = run_y;
a[run_x][run_y] = 2;
run_y += 1;
maze.push(temp);
}
//向下走
else if(a[run_x+1][run_y] == 0)
{
temp.x = run_x;
temp.y = run_y;
a[run_x][run_y] = 2;
run_x += 1;
maze.push(temp);
}
//向左走
else if(a[run_x][run_y-1]==0)
{
temp.x = run_x;
temp.y = run_y;
a[run_x][run_y] = 2;
run_y -= 1;
maze.push(temp);
}
else
{
temp = maze.top();
maze.pop();
a[temp.x][temp.y] = 0;//这个赋值主要便于查看每一步是怎么走的
a[run_x][run_y] = 8;
run_x = temp.x;
run_y = temp.y;
}
display();//这两行作用,运行程序,每次回车,显示当前位置,如果注释掉则直接显示最终结果
getchar();
}
while(!maze.empty())
{
result.push(maze.top());
temp = maze.top();
maze.pop();
}
cout <<"\tthe result: "<<endl;
display();
while(1)
{
temp = result.top();
cout <<"(" <<temp.x << "," << temp.y <<")";
result.pop();
if(result.empty())
{
break;
}
cout << "-->";
}
cout <<endl;
return 0;
}