经典数据结构之栈的应用-迷宫问题
栈是一种常用的数据结构,有着广泛的应用,例如走迷宫,八皇后问题,树的前序遍历,数学公式计算等等。以下是走迷宫问题代码
// Queen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
#define VISITED 10 //如果走过,则标记为VISITED
#define UNVISITED 9//初始都为UNVISITED
//4个行进方向
typedef struct direction
{
int x;
int y;
}DIR;
//坐标点定义
typedef struct pos
{
int pos_x;
int pos_y;
}POSITION;
//路径
typedef struct Path
{
POSITION point;
int dir;
}PATH;
//stack
class Stack
{
private:
int top;
POSITION status[64];
int m_maxNode;
public:
Stack()
{
top=-1;
m_maxNode=64;
for(int i=0;i<64;i++)
status[i].pos_x=status[i].pos_y=-1;
}
int Push(POSITION point)
{
if(top>=m_maxNode-1)
return 1;//over flow!
else
status[++top]=point;
return 0;
}
int Pop(POSITION *out)
{
if(top<0)
return 1;
out->pos_x=status[top].pos_x;
out->pos_y=status[top--].pos_y;
return 0;
}
int IsEmpty()
{
if(top==-1) return 1;
else
return 0;
}
};
class Queen
{
private:
DIR dir[4];
int m_MazeLength;
int Maze[9][9];
int m_mark[9][9];
int m_Path[20];
int m_index;
public:
Queen()
{
dir[LEFT].x=-1;
dir[LEFT].y=0;
dir[RIGHT].x=1;
dir[RIGHT].y=0;
dir[UP].x=0;
dir[UP].y=-1;
dir[DOWN].x=0;
dir[DOWN].y=1;
m_MazeLength=8;
int tempMaze[9][9]={
{1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1},
{1,0,0,0,0,1,0,0,1},
{1,0,1,0,0,0,0,1,1},
{1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1}
};
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
Maze[i][j]=tempMaze[i][j];
m_mark[i][j]=UNVISITED;
}
}
int GetPath();
POSITION GetPosition(POSITION current,int dire)
{
POSITION temp;
temp.pos_x=current.pos_x + dir[dire].x;
temp.pos_y=current.pos_y+ dir[dire].y;
return temp;
}
};
int Queen::GetPath()
{
Stack mazeStack;
POSITION pos;
pos.pos_x=1;
pos.pos_y=1;
mazeStack.Push(pos);
m_mark[pos.pos_y][pos.pos_x]=VISITED;
//迷宫栈不为空,则一直继续
while(!mazeStack.IsEmpty())
{
POSITION currentpos;
mazeStack.Pop(¤tpos);
if(currentpos.pos_x==7 &¤tpos.pos_y==7)
{
return 0;
}
//向上通路
POSITION pos_Up,pos_Down,pos_Left,pos_Right;
pos_Up=GetPosition(currentpos,UP);
pos_Down=GetPosition(currentpos,DOWN);
pos_Left=GetPosition(currentpos,LEFT);
pos_Right=GetPosition(currentpos,RIGHT);
//up direction
if(Maze[pos_Up.pos_y][pos_Up.pos_x]==0 && m_mark[pos_Up.pos_y][pos_Up.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Up);
m_mark[pos_Up.pos_y][pos_Up.pos_x]=VISITED;
}
//left direction
if(Maze[pos_Left.pos_y][pos_Left.pos_x]==0 && m_mark[pos_Left.pos_y][pos_Left.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Left);
m_mark[pos_Left.pos_y][pos_Left.pos_x]=VISITED;
}
//down direction
if(Maze[pos_Down.pos_y][pos_Down.pos_x]==0 && m_mark[pos_Down.pos_y][pos_Down.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Down);
m_mark[pos_Down.pos_y][pos_Down.pos_x]=VISITED;
}
//right direction
if(Maze[pos_Right.pos_y][pos_Right.pos_x]==0 && m_mark[pos_Right.pos_y][pos_Right.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Right);
m_mark[pos_Right.pos_y][pos_Right.pos_x]=VISITED;
}
}
return 1;
}
int main(int argc, char* argv[])
{
Queen q;
if(!q.GetPath())
printf("you have Walked out from the maze successfully\n");
else
printf("No Path to walk out from the maze\n");
return 0;
}
//
#include "stdafx.h"
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
#define VISITED 10 //如果走过,则标记为VISITED
#define UNVISITED 9//初始都为UNVISITED
//4个行进方向
typedef struct direction
{
int x;
int y;
}DIR;
//坐标点定义
typedef struct pos
{
int pos_x;
int pos_y;
}POSITION;
//路径
typedef struct Path
{
POSITION point;
int dir;
}PATH;
//stack
class Stack
{
private:
int top;
POSITION status[64];
int m_maxNode;
public:
Stack()
{
top=-1;
m_maxNode=64;
for(int i=0;i<64;i++)
status[i].pos_x=status[i].pos_y=-1;
}
int Push(POSITION point)
{
if(top>=m_maxNode-1)
return 1;//over flow!
else
status[++top]=point;
return 0;
}
int Pop(POSITION *out)
{
if(top<0)
return 1;
out->pos_x=status[top].pos_x;
out->pos_y=status[top--].pos_y;
return 0;
}
int IsEmpty()
{
if(top==-1) return 1;
else
return 0;
}
};
class Queen
{
private:
DIR dir[4];
int m_MazeLength;
int Maze[9][9];
int m_mark[9][9];
int m_Path[20];
int m_index;
public:
Queen()
{
dir[LEFT].x=-1;
dir[LEFT].y=0;
dir[RIGHT].x=1;
dir[RIGHT].y=0;
dir[UP].x=0;
dir[UP].y=-1;
dir[DOWN].x=0;
dir[DOWN].y=1;
m_MazeLength=8;
int tempMaze[9][9]={
{1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1},
{1,0,0,0,0,1,0,0,1},
{1,0,1,0,0,0,0,1,1},
{1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1}
};
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
Maze[i][j]=tempMaze[i][j];
m_mark[i][j]=UNVISITED;
}
}
int GetPath();
POSITION GetPosition(POSITION current,int dire)
{
POSITION temp;
temp.pos_x=current.pos_x + dir[dire].x;
temp.pos_y=current.pos_y+ dir[dire].y;
return temp;
}
};
int Queen::GetPath()
{
Stack mazeStack;
POSITION pos;
pos.pos_x=1;
pos.pos_y=1;
mazeStack.Push(pos);
m_mark[pos.pos_y][pos.pos_x]=VISITED;
//迷宫栈不为空,则一直继续
while(!mazeStack.IsEmpty())
{
POSITION currentpos;
mazeStack.Pop(¤tpos);
if(currentpos.pos_x==7 &¤tpos.pos_y==7)
{
return 0;
}
//向上通路
POSITION pos_Up,pos_Down,pos_Left,pos_Right;
pos_Up=GetPosition(currentpos,UP);
pos_Down=GetPosition(currentpos,DOWN);
pos_Left=GetPosition(currentpos,LEFT);
pos_Right=GetPosition(currentpos,RIGHT);
//up direction
if(Maze[pos_Up.pos_y][pos_Up.pos_x]==0 && m_mark[pos_Up.pos_y][pos_Up.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Up);
m_mark[pos_Up.pos_y][pos_Up.pos_x]=VISITED;
}
//left direction
if(Maze[pos_Left.pos_y][pos_Left.pos_x]==0 && m_mark[pos_Left.pos_y][pos_Left.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Left);
m_mark[pos_Left.pos_y][pos_Left.pos_x]=VISITED;
}
//down direction
if(Maze[pos_Down.pos_y][pos_Down.pos_x]==0 && m_mark[pos_Down.pos_y][pos_Down.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Down);
m_mark[pos_Down.pos_y][pos_Down.pos_x]=VISITED;
}
//right direction
if(Maze[pos_Right.pos_y][pos_Right.pos_x]==0 && m_mark[pos_Right.pos_y][pos_Right.pos_x]==UNVISITED)
{
mazeStack.Push(pos_Right);
m_mark[pos_Right.pos_y][pos_Right.pos_x]=VISITED;
}
}
return 1;
}
int main(int argc, char* argv[])
{
Queen q;
if(!q.GetPath())
printf("you have Walked out from the maze successfully\n");
else
printf("No Path to walk out from the maze\n");
return 0;
}