main.cpp
~cpp
#include <iostream.h>
#include "stack.h"
Offset move[8]= {
{1,0}, {1,1}, {0,1}, {-1,1},
{-1,0}, {-1,-1}, {0,-1}, {1,-1}};
short maze[M+2][P+2] = {
{ 1, 1,1,1,1,1,1,1, 1 },
{ 1, 0,1,0,0,1,1,1, 1 },
{ 1, 0,1,0,1,0,1,1, 1 },
{ 1, 0,1,0,1,1,0,1, 1 },
{ 1, 0,1,0,1,1,1,0, 1 },
{ 1, 0,1,0,1,1,1,0, 1 },
{ 1, 0,1,0,1,1,1,0, 1 },
{ 1, 1,0,1,0,0,1,0, 1 },
{ 1, 1,1,1,1,1,1,1, 1 }
};
void main()
{
Element item;
item.col = item.row = 1;
item.direction = 0;
push(&top, item);
int row, col;
while ( (row != M || col != P) && top >= 0){
row = path[top].row + move[path[top].direction].vtc;
col = path[top].col + move[path[top].direction].hrz;
item.col = col;
item.row = row;
path[top].direction++;
push(&top, item);
if ( maze[row][col] != 0 )
pop(&top);
if ( path[top-1].direction > 7){
pop(&top);
maze[path[top].row][path[top].col] = -1;
pop(&top);
}
}
for ( int i = 0 ; i <= top ; i++ )
cout << "(" << path[i].row
<< ", " << path[i].col << ")" << endl;
}