走迷宫

#ifndef __MAZE_H__
#define __MAZE_H__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>
/*=========================*/
void printMaze();
/*=========================*/

enum{MAZE_ROW = 20, MAZE_COL = 20};    //迷宫的长和宽
char maze[MAZE_ROW][MAZE_COL] = {0};
struct point
{
    char x;
    char y;
};
struct stack_fp
{
    struct point *base;     //存储路径的栈的基准指针
    struct point *cursor;   //存储路径的栈的游标
    int len;                //当前路径栈的路径长度
    void Push(struct point pt)    //压栈
    {
        if(len>MAZE_COL*MAZE_ROW)
        {
            printf("stack full!\n");
            return;
        }
        len++;
        (*cursor).x = (pt).x;
        (*cursor).y = (pt).y;
        cursor++;
    }
    const struct point Pop()   //出栈
    {
        if(len == 0)
        {
            printf("stack empty!!\n");
            return (struct point){-1, -1};
        }
        len--;
        return *(--cursor);
    }
}fp;
void createMazeRandom()      //随机生成迷宫
{
    /*init the seed*/
    srand(time(NULL));
    /*create the wall of the maze*/
    for(int i=0; i<MAZE_ROW; i++)
        maze[i][0] = maze[i][MAZE_COL-1] = 1;    //生成迷宫的边界
    for(int j=0; j<MAZE_COL; j++)
        maze[0][j] = maze[MAZE_ROW-1][j] = 1;     //生成迷宫的边界

    /*create the maze*/
    for(int i=1; i<MAZE_ROW-1; i++)
        for(int j=1; j<MAZE_COL-1; j++)
        {
            int random = rand()%3;
            if(!random)    /*2:1*/
                maze[i][j] = 1;    //为1表示墙,墙和空的比例为1:2
        }
    /*create the entrance and the exit*/
    maze[1][0] = maze[MAZE_ROW-2][MAZE_COL-1] = maze[1][1] = maze[MAZE_ROW-2][MAZE_COL-2] = 0;   

    /*init stack*/
    fp.base = (struct point *)malloc(sizeof(struct point)*MAZE_ROW*MAZE_COL);
    fp.cursor = fp.base;
    fp.len = 0;
    /*walk maze*/
    void walkMaze();
    walkMaze();
    /*print maze*/
    printMaze();
    /*print the path*/
    //int len = fp.len;
    //struct point *cursor = fp.cursor;
    //while(len--)
    //{
    //    printf("(x:%d, y:%d)\n", (*cursor).x, (*cursor).y);
    //    cursor--;
    //}
}
//递归走迷宫
void walkMaze()
{
    struct point pt = {1, 0};   //entrance
    int nextStep(struct point);
    nextStep(pt);
}

int checkFootPrint(struct point pt)   //检测当前位置是否已经在栈中,以检测是否已经走过该位置
{
    int len = fp.len;
    struct point *cursor = fp.cursor;
    while(len--)
    {
        if((*cursor).x == pt.x && (*cursor).y == pt.y)
            return 1;
        else
            cursor--;
    }
    return 0;
}

int nextStep(struct point pt)
{
    if((pt.x == MAZE_ROW-2) && (pt.y == MAZE_COL-1))     //是否到出口了
    {
        maze[pt.x][pt.y] = 3;    //为3表示该路径
        return 1;
    }
    if(maze[pt.x+1][pt.y] == 0)      //下
    {
        fp.Push(pt);
        maze[pt.x][pt.y] = 3;    //为3表示该路径
        if(!checkFootPrint((struct point){pt.x+1, pt.y}))   //检测是否已经走过该位置
            if(nextStep((struct point){pt.x+1, pt.y}) == 1)
                return 1;
    }
    if(maze[pt.x][pt.y+1] == 0)       //右
    {
        fp.Push(pt);
        maze[pt.x][pt.y] = 3;
        if(!checkFootPrint((struct point){pt.x, pt.y+1}))
            if(nextStep((struct point){pt.x, pt.y+1}) == 1)
                return 1;
    }
    if(maze[pt.x-1][pt.y] == 0)       //上
    {
        fp.Push(pt);
        maze[pt.x][pt.y] = 3;
        if(!checkFootPrint((struct point){pt.x-1, pt.y}))
            if(nextStep((struct point){pt.x-1, pt.y}) == 1)
                return 1;
    }
    if(maze[pt.x][pt.y-1] == 0)       //左
    {
        fp.Push(pt);
        maze[pt.x][pt.y] = 3;
        if(!checkFootPrint((struct point){pt.x, pt.y-1}))
            if(nextStep((struct point){pt.x, pt.y-1}) == 1)
                return 1;
    }
    fp.Pop();
    maze[pt.x][pt.y] = 7;
    return 0;
}
void printMaze()
{
    for(int i=0; i<MAZE_ROW; i++)
    {
        for(int j=0; j<MAZE_COL; j++)
        {
            if(maze[i][j] == 1)
                printf("¡ö");   /*unicode*/
            else if(maze[i][j] == 3)
                printf("¡ð");
            else if(maze[i][j] == 7)
                printf("¡Á");
            else
                printf("  ");  /*2 space here, ascii*/
        }
        printf("\n");
    }
}
#endif // __MAZE_H__


主函数:

#include <stdio.h>
#include "maze.h"

int main()
{
    //printf("¡ö");printf("¡ð");printf("¡Á");
    createMazeRandom();

    getchar();
    return 0;
}


迷宫走通:



迷宫未走通:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值