/*This is solve to maze problem,if you want auto get the maze use rand() to get
*---------------------------running------------------------------------*/
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
/*------a template link list stack for this problem ---------*/
template< class T >
struct Snode {
T data;
Snode* next;
};
template< class T >
class mStack {
Snode<T>* end;
Snode<T>* p;
public:
int size;
mStack();
bool isEmpty();
bool GetTop( T& d );
void Push( const T& d );
bool Pop();
~mStack() {
delete p;
delete end;
}
};
template< class T >
mStack<T>::mStack() {
size = 0;
end = ( Snode<T>* )malloc( sizeof( Snode<T> ) );
p = end;
}
template< class T >
bool mStack<T>::isEmpty() {
if( size > 0 )
return false;
return true;
}
template< class T >
void mStack<T>::Push( const T& d ) {
Snode<T>* temp;
temp = ( Snode<T>* )malloc( sizeof( Snode<T> ) );
temp->data = d;
temp->next = p;
p = temp;
size++;
}
template< class T >
bool mStack<T>::GetTop( T& d ) {
if( !isEmpty() )
{
d = p->data;
return true;
}
return false;
}
template< class T >
bool mStack<T>::Pop() {
Snode<T>* f;
if( !isEmpty() )
{
f = p;
p = p->next;
free( f );
size--;
return true;
}
return false;
}
/*---------link list stack end------------------------*/
struct Position {
int x;
int y;
};
enum Direction { Right, Down, Left, Up };
struct ElemType {
int step;
Position c_pos; //current position
Direction di; //next direction
};
mStack< ElemType > mstack;
// map information
vector< vector<int> > map;
int m_rows;
int m_cols;
// first enter into maze position
int b_x = 0;
int b_y = 0;
// exit the maze position
int e_x = 0;
int e_y = 0;
void InitMaze( const int rows, const int cols ) {
int i = 0;
int j = 0;
map.resize( rows+2, vector<int>(cols+2) );
for( i = 0; i < rows+1; i++ ) // set wall
{
map[i][0] = 5;
map[i][cols+1] = 5;
}
for( i = 0; i < cols+2; i++ ) //set wall
{
map[0][i] = 5;
map[rows+1][i] = 5;
}
for( i = 1; i < rows+1; i++ ) //get input
{
for( j = 1; j < cols+1; j++ )
{
cin >> map[i][j];
}
}
}
bool is_out( int x, int y ) { //if walked out the map
if( ( e_x == x )&&( e_y == y ) )
return true;
return false;
}
Position get_next_step( const ElemType& p ) {
Position temp;
switch( p.di ) {
case Left: temp.x = p.c_pos.x; temp.y = p.c_pos.y-1; break;
case Right: temp.x = p.c_pos.x; temp.y = p.c_pos.y+1; break;
case Up: temp.x = p.c_pos.x-1; temp.y = p.c_pos.y; break;
case Down: temp.x = p.c_pos.x+1; temp.y = p.c_pos.y; break;
default: cerr << "Unknow dirction" << endl;
}
return temp;
}
void walk( ElemType& cur, int x, int y ) {
map[x][y] = 3;
cur.c_pos.x = x;
cur.c_pos.y = y;
cur.step++;
}
bool MazePath( int b_x, int b_y ) {
ElemType t;
Position pos;
Position temp_pos;
t.step = 0;