简单迷宫------一个出口

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stddef.h>

typedef struct point
{
    int row;
    int col;
}point;

#define SEQ_STACK_SIZE 100

typedef struct SeqStack
{
    point date[SEQ_STACK_SIZE];
    size_t size;
}SeqStack;

void SeqStackInit(SeqStack* stack);

void SeqStackDestory(SeqStack* stack);

void SeqStackPush(SeqStack* stack, point value);

void SeqStackPop(SeqStack* stack);

int SeqStackTop(SeqStack* stack, point* value);

void StackPrintf(SeqStack* stack);

int SeqStackSize(SeqStack* stack);

void SeqStackRplace(SeqStack* from, SeqStack* to);
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"

///////////////////////////////////
//        栈的初始化             //
///////////////////////////////////
void SeqStackInit(SeqStack* stack)
{
    if (stack == NULL)
    {
        return;
    }
    stack->size = 0;
}

///////////////////////////////////
//        栈的销毁               //
///////////////////////////////////
void SeqStackDestory(SeqStack* stack)
{
    if (stack == NULL)
    {
        return;
    }
    stack->size = 0;
}

///////////////////////////////////
//           压栈                //
///////////////////////////////////
void SeqStackPush(SeqStack* stack, point value)
{
    if (stack == NULL)
    {
        return;
    }
    if (stack->size > SEQ_STACK_SIZE - 1)
    {
        return;
    }
    stack->date[stack->size++] = value;
}


///////////////////////////////////
//           出栈                //
///////////////////////////////////
void SeqStackPop(SeqStack* stack)
{
    if (stack == NULL)
    {
        return;
    }
    if (stack->size == 0)
    {
        printf("栈为空\n");
        return;
    }
    stack->size = stack->size - 1;
}

///////////////////////////////////
//           取栈顶元素         //
///////////////////////////////////
int SeqStackTop(SeqStack* stack, point* value)
{
    if (stack == NULL)
    {
        return 0;
    }
    if (stack->size == 0)
    {
        printf("栈为空\n");
        return 0;
    }
    *value = stack->date[stack->size - 1];
    return 1;
}

///////////////////////////////////
//          栈的长度             //
///////////////////////////////////
int SeqStackSize(SeqStack* stack)
{
    if (stack == NULL)
    {
        return -1;
    }
    return stack->size;
}

///////////////////////////////////
//          替换栈               //
///////////////////////////////////
void SeqStackRplace(SeqStack* from, SeqStack* to)
{
    if (from == NULL || to == NULL)
    {
        return;
    }
    SeqStackDestory(to);
    size_t i = 0;
    for (i = 0; i < from->size; ++i)
    {
        to->date[to->size++] = from->date[i];
    }
}

///////////////////////////////////
//           打印栈              //
///////////////////////////////////
void StackPrintf(SeqStack* stack)
{
    printf("[栈底]");
    printf("\n");
    size_t i = 0;
    for (i = 0; i < (stack->size); ++i)
    {
        printf(" (%d, %d) ", stack->date[i].row,stack->date[i].col);
        printf("\n");
    }
    printf("[栈顶]\n");
}

//递归实现迷宫

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

#define MAZE_ROW 6
#define MAZE_COL 6

typedef struct Maze
{
    int map[MAZE_ROW][MAZE_COL];
}Maze;

typedef struct point
{
    int row;
    int col;
}point;

void MazeInit(Maze* maze)
{
    int map[MAZE_ROW][MAZE_COL] =
    {
        { 0,1,0,0,0,0 },
        { 0,1,1,1,0,0 },
        { 0,1,0,1,1,0 },
        { 0,1,1,0,0,0 },
        { 0,0,1,0,0,0 },
        { 0,0,1,0,0,0 }
    };
    int i = 0;
    int j = 0;
    for (i = 0; i<MAZE_ROW; ++i)
        for (j = 0; j<MAZE_COL; ++j)
        {
            maze->map[i][j] = map[i][j];
        }
}

void MazePrint(Maze *maze)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < MAZE_ROW; ++i)
    {
        for (j = 0; j < MAZE_COL; ++j)

        {
            printf("%2d ", maze->map[i][j]);
        }
        printf("\n");
    }
}

//判断是不是落脚点
int Is_Stay(Maze *maze, point pos)
{
    if (pos.row < 0 || pos.row > MAZE_ROW - 1
        || pos.col < 0 || pos.col > MAZE_COL - 1
        || maze->map[pos.row][pos.col] != 1)
        return 0;
    else
        return 1;
}

//标记走过的点
void MakePos(Maze* maze, point pos)
{
    maze->map[pos.row][pos.col] = 2;
}

int Is_Chukou(point pos, point entry)
{
    if ((pos.row == 0) || (pos.row == MAZE_ROW - 1)
        || (pos.col == 0) || (pos.col == MAZE_COL - 1))
    {
        if (pos.row == entry.row  && pos.col == entry.col)
            return 0;
        else
            return 1;
    }
    return 0;

}
void _Has_path(Maze *maze, point pos, point entry)
{
    //1.判断entry是不是落脚点,点在地图上,位置为1
    if (!Is_Stay(maze, pos))
    {
        return;
    }
    //2.标记当前点是走过的点
    MakePos(maze, pos);

    //3.判断当前点是不是出口,落在边缘上且不是入口
    if (Is_Chukou(pos, entry))
    {
        return;
    }
    //4.按照顺时针的方向探测周围的节点(递归调用该函数)
    //上
    point tmp_pos = pos;
    tmp_pos.row -= 1;
    _Has_path(maze, tmp_pos, entry);

    //右
    tmp_pos.row = pos.row;
    tmp_pos.col = pos.col;
    tmp_pos.col += 1;
    _Has_path(maze, tmp_pos, entry);

    //下
    tmp_pos.row = pos.row;
    tmp_pos.col = pos.col;
    tmp_pos = pos;
    tmp_pos.row += 1;
    _Has_path(maze, tmp_pos, entry);
    //左
    tmp_pos.row = pos.row;
    tmp_pos.col = pos.col;
    tmp_pos = pos;
    tmp_pos.col -= 1;
    _Has_path(maze, tmp_pos, entry);

    //5.如果四个方向都探测过了就直接返回    
    return;
}

void Has_pash(Maze *maze, point entry)
{
    assert(maze);
    _Has_path(maze, entry, entry);
}

void TestInit()
{
    Maze maze;
    MazeInit(&maze);
    MazePrint(&maze);
}
void Test()
{
    Maze maze;
    MazeInit(&maze);
    MazePrint(&maze);
    point pos = { 0,1 };
    Has_pash(&maze, pos);
    MazePrint(&maze);
}

int main()
{
    //TestInit();
    Test();
    system("pause");
    return 0;
}
//用循环是实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"stack.h"

#define MAZE_ROW 6
#define MAZE_COL 6

typedef struct Maze
{
    int map[MAZE_ROW][MAZE_COL];
}Maze;


void MazeInit(Maze* maze)
{
    int map[MAZE_ROW][MAZE_COL] =
    {
        { 0,1,0,0,0,0 },
        { 0,1,1,1,0,0 },
        { 0,1,0,1,1,0 },
        { 0,1,1,0,0,0 },
        { 0,0,1,0,0,0 },
        { 0,0,1,0,0,0 }
    };
    int i = 0;
    int j = 0;
    for (i = 0; i<MAZE_ROW; ++i)
        for (j = 0; j<MAZE_COL; ++j)
        {
            maze->map[i][j] = map[i][j];
        }
}

void MazePrint(Maze *maze)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < MAZE_ROW; ++i)
    {
        for (j = 0; j < MAZE_COL; ++j)

        {
            printf("%2d ", maze->map[i][j]);
        }
        printf("\n");
    }
}

//判断是不是落脚点
int Is_Stay(Maze *maze, point pos)
{
    if (pos.row < 0 || pos.row > MAZE_ROW - 1
        || pos.col < 0 || pos.col > MAZE_COL - 1
        || maze->map[pos.row][pos.col] != 1)
        return 0;
    else
        return 1;
}

//标记走过的点
void MakePos(Maze* maze, point pos)
{
    maze->map[pos.row][pos.col] = 2;
}

int Is_Chukou(point pos, point entry)
{
    if ((pos.row == 0) || (pos.row == MAZE_ROW - 1)
        || (pos.col == 0) || (pos.col == MAZE_COL - 1))
    {
        if (pos.row == entry.row  && pos.col == entry.col)
            return 0;
        else
            return 1;
    }
    return 0;

}

void Has_pash(Maze *maze, point entry)
{
    assert(maze);
    SeqStack stack;
    SeqStackInit(&stack);
    //1.判断入口点是否能入脚,如果不能,表示非法输入
    if (!Is_Stay(maze, entry))
    {
        return;
    }
    //2.标记入口点走过了,把入口点入栈
    MakePos(maze, entry);
    SeqStackPush(&stack, entry);


    while (stack.size > 0)
    {
        //3.进入循环,取栈顶元素为当前点
        point pos;
        SeqStackTop(&stack, &pos);
        //4.判断当前点是不是出口(如果是出口,说明找到路了)
        if (Is_Chukou(pos, entry))
        {
            printf("找到路了!\n");
            printf("路径为:\n");
            StackPrintf(&stack);
            return;
        }
        //5.按照一定的顺序判定当前点的四个领节点是否能落脚
        //6.如果某个领节点能够落脚,标记这个领节点,并且把这个领节点入栈,并且直接进入下一次循环

            //上
        point up = pos;
        up.row -= 1;
        if (Is_Stay(maze, up))
        {
            MakePos(maze, up);
            SeqStackPush(&stack, up);
            continue;
        }

          //右
        point right  = pos;
        right.col += 1;
        if (Is_Stay(maze, right))
        {
            MakePos(maze, right);
            SeqStackPush(&stack, right);
            continue;
        }

            //下
        point down = pos;
        down.row += 1;
        if (Is_Stay(maze, down))
        {
            MakePos(maze, down);
            SeqStackPush(&stack, down);
            continue;
        }

            //左
        point left = pos;
        left.col -= 1;
        if (Is_Stay(maze, left))
        {
            MakePos(maze, left);
            SeqStackPush(&stack, left);
            continue;
        }

    //6.四个方向都探测完毕,把当前点出栈
        SeqStackPop(&stack);
    }

}

void TestInit()
{
    Maze maze;
    MazeInit(&maze);
    MazePrint(&maze);
}
void Test()
{
    Maze maze;
    MazeInit(&maze);
    MazePrint(&maze);
    point pos = { 0,1 };
    Has_pash(&maze, pos);
    MazePrint(&maze);

}

int main()
{
    //TestInit();
    Test();
    system("pause");
    return 0;
}



猜你喜欢

转载自blog.csdn.net/jsbgo201506010102/article/details/80629277