//stack.h
#define MAX_STACK_SIZE 100
typedef struct{
    int row;
    int col;
}element;
element stack[MAX_STACK_SIZE]; 

int top = -1;

void push(element item)
{
  if(top >= MAX_STACK_SIZE - 1) {
    printf("stack_full()n");
    return;
  }
  stack[++top] = item;
}                   

element pop() {
 element tmp;
    if(top == -1)
    {
        printf("stack_empty()n"); exit(0) ;
    }
 tmp = stack[top];
 top--;
 return tmp;
}

int isempty()
{ if( top == -1 ) return(1); else return(0); }

int isfull()
{ if ( top >= MAX_STACK_SIZE -1 ) return(1); else return(0); }

void viewstack(){
 element tmp;
 int i;
 for(i=0;i<top;i++)
  printf("%d %dn",stack[i].row,stack[i].col);

}


//maze.h
#define MAZE_WALL  "■"
#define MAZE_SPACE "□"
#define MAX_row 10   
#define MAX_col 10
#define STARTX 0 //시작점
#define STARTY 1 //시작점
#define ENDX  8  //끝점
#define ENDY  9     //끝점

char maze[MAX_row][MAX_col] = {
    1,1,1,1,1,1,1,1,1,1,
    0,0,1,1,1,1,1,1,1,1,
    1,0,1,1,0,0,0,1,0,1,
    1,0,0,0,0,1,0,1,0,1,
    1,0,1,1,1,1,0,1,0,1,
    1,0,1,0,0,0,0,1,0,1,
    1,0,1,0,1,1,1,1,0,1,
    1,1,1,0,0,0,0,0,0,1,
    1,0,0,0,1,1,1,1,0,1,
    1,1,1,1,1,1,1,1,0,1
};
void printMaze();

void printMaze(){
    int i,j;
 gotoxy(0,0);
    for(i=0;i<MAX_row;i++){
        for(j=0;j<MAX_col;j++){
            if(maze[i][j]==1)
                printf(MAZE_WALL);
            else
                printf(MAZE_SPACE);
        }
        printf("n");
    }

}



//main.c

//미로찾기
#include <stdio.h>
#include <conio.h>
#include "TURBOC.H"
#include "stack.h"
#include "maze.h"

element now;
int isInStack(int row,int col){
 int i;
 for(i=0;i<top;i++){
  if(stack[i].col == col && stack[i].row == row){
   return 1;
  }
 }
 return 0;
}

int main()

 int isForward=1,i;
 printMaze();
    now.col = STARTX;
 now.row = STARTY;
 while(!(ENDX==now.col && ENDY==now.row) ){
  gotoxy(now.col*2,now.row);
  if(isForward){
   printf("X");
   push(now);
  }
  if( maze[now.row -1][now.col] == 0 &&
    !(isInStack(now.row -1,now.col)) ){
   // 위로
    now.row--;
    isForward = 1;
  }else if(maze[now.row ][now.col+1] == 0 &&
   !(isInStack(now.row ,now.col+1))  ){
   //오른쪽
   now.col++;
   isForward = 1;
  }else if(maze[now.row + 1 ][now.col] == 0 &&
   !(isInStack(now.row +1,now.col)) ){
   //아래
   now.row++;
   isForward = 1;
  }else if(maze[now.row ][now.col-1] == 0 &&
   !(isInStack(now.row ,now.col-1)) ){
   //왼쪽
   now.col--;
   isForward = 1;
  }else{
   //printf("더 이상 갈때 없음...!n");
   gotoxy(now.col*2,now.row);
   printf(MAZE_SPACE);
   maze[now.row][now.col]=1;
   now=pop();
   isForward = 0;
  }
 // getch();
  
 }
 gotoxy(now.col*2,now.row);
 printf("★");
 gotoxy(MAX_col,MAX_row);
 printf("n");
 viewstack();
 

}