迷宫解法之栈/队列的解法
2017-08-04 23:09
162 查看
以下是栈
#include<iostream>
#include<stack>
using namespace std;
int a[10][12] = { 1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1};
class Coordinate
{
public:
int x;
int y;
};
int main(void)
{
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
Coordinate start;
cout<<"输入起点";
cin>>start.x>>start.y;
Coordinate end;
cout<<"输入终点";
cin>>end.x>>end.y;
stack<Coordinate>s;
s.push(start);
bool flag=true;
while(!s.empty()&&flag)
{
a[start.x][start.y]=3;
if(a[start.x-1][start.y]==0)
{
start.x-=1;
s.push(start);
a[start.x][start.y]=-1;
}
else if(a[start.x][start.y-1]==0)
{
start.y-=1;
s.push(start);
a[start.x][start.y]=3;
}
else if(a[start.x][start.y+1]==0)
{
start.y+=1;
s.push(start);
a[start.x][start.y]=3;
}
else if(a[start.x+1][start.y]==0)
{
start.x+=1;
s.push(start);
a[start.x][start.y]=3;
}
else
{
a[start.x][start.y]=3;
start=s.top();
s.pop();
}
if(start.x==end.x&&start.y==end.y)
{
flag=false;
break;
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
以下是队列
#include<iostream>
#include<queue>
using namespace std;
int a[10][12] = { 1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1};
class Coordinate
{
public:
int x;
int y;
int pre;
};
int main(void)
{
int x,y;
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
int startx,starty,endx,endy;
int rear=-1;
int front=-1;
Coordinate s[100000];
rear++;
Coordinate start;
cout<<"输入起点";
cin>>startx>>starty;
Coordinate end;
cout<<"输入终点";
cin>>endx>>endy;
s[rear].x=startx;
s[rear].y=starty;
a[startx][starty]=-1;
s[rear].pre=-1;
bool flag=true;
while(front<=rear&&flag)
{
front++;
if(s[front].x==endx&&s[front].y==endy)
{
flag=false;
x=s[front].x;y=s[front].y;
a[x][y]=3;
int h=front;
while(s[h].pre!=-1)
{
int g=s[h].pre;
x=s[g].x;
y=s[g].y;
a[x][y]=3;
h=g;
}
}
for(int direction=0;direction<4;direction++)
{
switch(direction)
{
case 0:x=s[front].x;y=s[front].y-1;break;//上
case 1:x=s[front].x;y=s[front].y+1;break;//下
case 2:x=s[front].x-1;y=s[front].y;break;//左
case 3:x=s[front].x+1;y=s[front].y;break;//右
}
if(a[x][y]==0)
{
rear++;
s[rear].x=x;
s[rear].y=y;
s[rear].pre=front;
}
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
#include<iostream>
#include<stack>
using namespace std;
int a[10][12] = { 1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1};
class Coordinate
{
public:
int x;
int y;
};
int main(void)
{
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
Coordinate start;
cout<<"输入起点";
cin>>start.x>>start.y;
Coordinate end;
cout<<"输入终点";
cin>>end.x>>end.y;
stack<Coordinate>s;
s.push(start);
bool flag=true;
while(!s.empty()&&flag)
{
a[start.x][start.y]=3;
if(a[start.x-1][start.y]==0)
{
start.x-=1;
s.push(start);
a[start.x][start.y]=-1;
}
else if(a[start.x][start.y-1]==0)
{
start.y-=1;
s.push(start);
a[start.x][start.y]=3;
}
else if(a[start.x][start.y+1]==0)
{
start.y+=1;
s.push(start);
a[start.x][start.y]=3;
}
else if(a[start.x+1][start.y]==0)
{
start.x+=1;
s.push(start);
a[start.x][start.y]=3;
}
else
{
a[start.x][start.y]=3;
start=s.top();
s.pop();
}
if(start.x==end.x&&start.y==end.y)
{
flag=false;
break;
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
以下是队列
#include<iostream>
#include<queue>
using namespace std;
int a[10][12] = { 1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1};
class Coordinate
{
public:
int x;
int y;
int pre;
};
int main(void)
{
int x,y;
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
int startx,starty,endx,endy;
int rear=-1;
int front=-1;
Coordinate s[100000];
rear++;
Coordinate start;
cout<<"输入起点";
cin>>startx>>starty;
Coordinate end;
cout<<"输入终点";
cin>>endx>>endy;
s[rear].x=startx;
s[rear].y=starty;
a[startx][starty]=-1;
s[rear].pre=-1;
bool flag=true;
while(front<=rear&&flag)
{
front++;
if(s[front].x==endx&&s[front].y==endy)
{
flag=false;
x=s[front].x;y=s[front].y;
a[x][y]=3;
int h=front;
while(s[h].pre!=-1)
{
int g=s[h].pre;
x=s[g].x;
y=s[g].y;
a[x][y]=3;
h=g;
}
}
for(int direction=0;direction<4;direction++)
{
switch(direction)
{
case 0:x=s[front].x;y=s[front].y-1;break;//上
case 1:x=s[front].x;y=s[front].y+1;break;//下
case 2:x=s[front].x-1;y=s[front].y;break;//左
case 3:x=s[front].x+1;y=s[front].y;break;//右
}
if(a[x][y]==0)
{
rear++;
s[rear].x=x;
s[rear].y=y;
s[rear].pre=front;
}
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<12;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
相关文章推荐
- 队列应用2:求解迷宫问题,最短路径
- 第十二周项目5——迷宫问题之图深度优先遍历解法
- 第十二周项目5迷宫问题之图深度优先遍历解法
- 第十一周项目5—迷宫问题之图深度优先遍历解法
- 用队列解迷宫问题
- poj 3984 迷宫问题 【BFS + 优先队列 + stack路径记录】
- C++ 迷宫问题的回溯解法
- 迷宫问题 模拟队列 广度优先搜索
- 栈和队列的底层实现及迷宫问题
- C语言广度优先搜索之迷宫(队列)
- 第十二周--图--项目五--迷宫问题之图深度优先遍历解法
- 数据结构 迷宫 C++源代码 非递归解法
- 迷宫求解(队列算法)
- 杨辉三角的链队列解法(链队列操作头文件)
- 链式队列实现迷宫寻径
- 栈和队列_迷宫问题_栈和回朔法
- SDUT_2449_数据结构实验之栈与队列十:走迷宫
- 队列的应用——求解迷宫问题
- 队列求迷宫问题
- 数据结构:栈和队列-迷宫问题求解