栈的应用---迷宫

根据数据结构书本上的为代码实现的

 

 

 

 

View Code
#include<stdio.h>
#include <windows.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
#define EXIT_ROW 9
#define EXIT_COL 9


int maze[10][10] = {
        {1,0,1,1,1,1,1,1,1,1},
        {1,0,0,1,0,0,0,1,0,1},
        {1,0,0,1,0,0,0,1,0,1},
        {1,0,0,0,0,1,1,0,0,1},
        {1,0,1,1,1,0,0,0,0,1},
        {1,0,0,0,1,0,0,0,0,1},
        {1,0,1,0,0,0,1,0,0,1},
        {1,0,1,1,1,1,1,1,0,1},
        {1,1,0,0,0,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,0,1}};/*初始化迷宫*/

int mark[10][10] = {0};/*初始化标志位,0代表没走过,1代表走过*/

bool mars[10][10] = {0};


/*方向*/
typedef struct{
    short int vert;
    short int horiz;
}offsets;

offsets move[4] = {{0,1},{1,0},{0,-1},{-1,0}};/*北,东,南,西*/


/*迷宫类型*/
typedef struct {
    short int row;
    short int col;
    short int dir;
}element;

element stack[MAX_STACK_SIZE];


void path(void);
element Delete(int* top);
void add(int* top,element item);


element Delete(int* top)/*出栈,top指向栈顶元素*/
{
    if(*top == -1)
    {
        printf("stack is empty!\n");
    }
    return stack[(*top)--];
}


void add(int* top,element item)/*入栈*/
{
    if(*top >= MAX_STACK_SIZE - 1)
    {
        printf("stack is full!\n");
        return;
    }
    stack[++*top] = item;
}

void PrintMap()
{
   // printf("显示地图,1表示墙,0表示能通过的路径\n");
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            if(mars[i][j] == 1)
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED);
            printf("%d ",maze[i][j]);
             if(mars[i][j] == 1)
                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
        }
        printf("\n");
    }
}



void path(void)/*迷宫函数*/
{
    element position;
    int i,row,col,next_row,next_col,dir,top;
    bool found = 0;
    mark[1][1] = 1,top = 0;/*初始化标志数组元素以及栈*/
    stack[0].row = 1,stack[0].col = 1,stack[0].dir = 0; //入口的位置
    while(top > -1 && !found)
    {
        position = Delete(&top);    /*将栈顶元素取出,*/
        row = position.row;            /*利用中间变量row,col,dir等候判断*/
        col = position.col;
        dir = position.dir;
        while(dir < 4 && !found)
        {
            next_row = row + move[dir].vert;
            next_col = col + move[dir].horiz;
            if(next_row == EXIT_ROW && next_col == EXIT_COL)
                found = 1;
            else if(!maze[next_row][next_col] && !mark[next_row][next_col])/*判断下一步可走并且没走过,则入栈*/
            {
                mark[next_row][next_col] = 1;
                position.row = row;
                position.col = col;
                position.dir = ++dir;
                add(&top,position);/*合理则入栈*/
                row = next_row;/*继续向下走*/
                col = next_col;dir = 0;
            }
            else
                dir++;/*走不动了就换一个方向,dir<4时,改变方向*/
        }
        if(found)/*判断是否有出口*/
        {
            for(i = 0;i <= top;++i)
                 mars[stack[i].row][stack[i].col] = 1;
            mars[row][col] = 1;
            mars[0][1] = 1;
           // mars[EXIT_ROW][EXIT_COL] = 1;
           // system("color 07");
           printf("\n\n找到出口了,红色代表路径\n");
            PrintMap();
        }
    }
    if(!found)
    {
        printf("找不到出口\n");
    }
}


int main(void)
{
    PrintMap();
    path();
    return 0;
}
posted on 2012-03-26 00:21  genslow  阅读(198)  评论(0编辑  收藏  举报