#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");
}
//递归实现迷宫
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);
if (!Is_Stay(maze, entry))
{
return;
}
MakePos(maze, entry);
SeqStackPush(&stack, entry);
while (stack.size > 0)
{
point pos;
SeqStackTop(&stack, &pos);
if (Is_Chukou(pos, entry))
{
printf("找到路了!\n");
printf("路径为:\n");
StackPrintf(&stack);
return;
}
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;
}
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()
{
Test();
system("pause");
return 0;
}