#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<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,1 },
{ 0,1,1,1,0,0 },
{ 0,1,0,1,1,0 },
{ 0,1,0,0,1,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;
}
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;
}
int Is_Stay_Wirh_Cirl(Maze* maze, point cur, point pre)
{
if (pre.row < 0 || pre.col < 0)
{
if (cur.row < 0 || cur.row > MAZE_ROW - 1
|| cur.col < 0 || cur.col > MAZE_COL - 1 && maze->map[cur.row][cur.col] == 0)
return 0;
else
return 1;
}
else
{
if (cur.row < 0 || cur.row > MAZE_ROW - 1
|| cur.col < 0 || cur.col > MAZE_COL - 1)
return 0;
if(maze->map[cur.row][cur.col] == 0)
return 0;
if (maze->map[cur.row][cur.col] == 1)
return 1;
if (maze->map[cur.row][cur.col] > maze->map[pre.row][pre.col] + 1)
{
return 1;
}
}
return 0;
}
void Mark_With_Cirl(Maze* maze, point cur,point pre)
{
if (pre.row < 0 || pre.col < 0)
{
maze->map[cur.row][cur.col] ++;
}
else
maze->map[cur.row][cur.col] = maze->map[pre.row][pre.col] + 1;
}
void _GetShort_pathWith_Cirl(Maze* maze, point cur, point pre, point entry, SeqStack* cur_path, SeqStack* short_path)
{
if (cur_path == NULL || short_path == NULL)
{
return;
}
if (!(Is_Stay_Wirh_Cirl(maze, cur, pre)))
{
return;
}
Mark_With_Cirl(maze, cur,pre);
SeqStackPush(cur_path, cur);
if (Is_Chukou(cur, entry))
{
if (SeqStackSize(cur_path) < SeqStackSize(short_path) || SeqStackSize(short_path) == 0)
{
SeqStackRplace(cur_path, short_path);
}
printf("找到一条路径:\n");
StackPrintf(cur_path);
printf("当前最短路径为:\n");
StackPrintf(short_path);
SeqStackPop(cur_path);
return;
}
point up = cur;
up.row -= 1;
_GetShort_pathWith_Cirl(maze, up, cur, entry, cur_path, short_path);
point right = cur;
right.col += 1;
_GetShort_pathWith_Cirl(maze, right, cur ,entry, cur_path, short_path);
point down = cur;
down.row += 1;
_GetShort_pathWith_Cirl(maze, down, cur, entry, cur_path, short_path);
point left = cur;
left.col -= 1;
_GetShort_pathWith_Cirl(maze, left, cur, entry, cur_path, short_path);
SeqStackPop(cur_path);
return;
}
void GetShort_pashWith_Cirl(Maze *maze, point entry)
{
if (maze == NULL)
{
return;
}
if (!Is_Stay(maze, entry))
{
return;
}
SeqStack short_path;
SeqStackInit(&short_path);
SeqStack cur_path;
SeqStackInit(&cur_path);
point cur = entry;
point pre = { -1,-1 };
_GetShort_pathWith_Cirl(maze, cur, pre, entry, &cur_path, &short_path);
printf("最终的最短路径为:\n");
StackPrintf(&short_path);
}
void TestInit()
{
Maze maze;
MazeInit(&maze);
MazePrint(&maze);
}
void Test()
{
Maze maze;
MazeInit(&maze);
MazePrint(&maze);
point pos = { 0,1 };
GetShort_pashWith_Cirl(&maze, pos);
MazePrint(&maze);
}
int main()
{
Test();
system("pause");
return 0;
}
#include<stdio.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,1 },
{ 0,1,1,1,0,0 },
{ 0,1,0,1,1,0 },
{ 0,1,0,0,1,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;
}
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;
}
int Is_Stay_Wirh_Cirl(Maze* maze, point cur, point pre)
{
if (pre.row < 0 || pre.col < 0)
{
if (cur.row < 0 || cur.row > MAZE_ROW - 1
|| cur.col < 0 || cur.col > MAZE_COL - 1 && maze->map[cur.row][cur.col] == 0)
return 0;
else
return 1;
}
else
{
if (cur.row < 0 || cur.row > MAZE_ROW - 1
|| cur.col < 0 || cur.col > MAZE_COL - 1)
return 0;
if (maze->map[cur.row][cur.col] == 0)
return 0;
if (maze->map[cur.row][cur.col] == 1)
return 1;
if (maze->map[cur.row][cur.col] > maze->map[pre.row][pre.col] + 1)
{
return 1;
}
}
return 0;
}
void Mark_With_Cirl(Maze* maze, point cur, point pre)
{
if (pre.row < 0 || pre.col < 0)
{
maze->map[cur.row][cur.col] ++;
}
else
maze->map[cur.row][cur.col] = maze->map[pre.row][pre.col] + 1;
}
void _GetShort_pathWith_Cirl(Maze* maze, point cur, point pre, point entry, SeqStack* cur_path, SeqStack* short_path)
{
if (cur_path == NULL || short_path == NULL)
{
return;
}
if (!(Is_Stay_Wirh_Cirl(maze, cur, pre)))
{
return;
}
SeqStack stack;
SeqStackInit(&stack);
Mark_With_Cirl(maze, cur, pre);
SeqStackPush(&stack, cur);
SeqStackPush(cur_path, cur);
while (SeqStackTop(&stack , &cur))
{
if ((Is_Chukou(cur, entry)))
{
if (SeqStackSize(cur_path) < SeqStackSize(short_path) || SeqStackSize(short_path) == 0)
{
SeqStackRplace(cur_path, short_path);
}
printf("找到一条路径:\n");
StackPrintf(cur_path);
printf("当前最短路径为:\n");
StackPrintf(short_path);
SeqStackPop(&stack);
SeqStackPop(cur_path);
continue;
}
point up = cur;
up.row -= 1;
if (Is_Stay_Wirh_Cirl(maze, up, cur))
{
Mark_With_Cirl(maze,up, cur);
SeqStackPush(&stack, up);
SeqStackPush(cur_path, up);
continue;
}
point right = cur;
right.col += 1;
if (Is_Stay_Wirh_Cirl(maze, right, cur))
{
Mark_With_Cirl(maze, right, cur);
SeqStackPush(&stack, right);
SeqStackPush(cur_path, right);
continue;
}
point down = cur;
down.row += 1;
if (Is_Stay_Wirh_Cirl(maze, down, cur))
{
Mark_With_Cirl(maze, down, cur);
SeqStackPush(&stack, down);
SeqStackPush(cur_path, down);
continue;
}
point left = cur;
left.row -= 1;
if (Is_Stay_Wirh_Cirl(maze, left, cur))
{
Mark_With_Cirl(maze, left , cur);
SeqStackPush(&stack, left);
SeqStackPush(cur_path, left);
continue;
}
SeqStackPop(&stack);
SeqStackPop(cur_path);
}
}
void GetShort_pashWith_Cirl(Maze *maze, point entry)
{
if (maze == NULL)
{
return;
}
if (!Is_Stay(maze, entry))
{
return;
}
SeqStack short_path;
SeqStackInit(&short_path);
SeqStack cur_path;
SeqStackInit(&cur_path);
point cur = entry;
point pre = { -1,-1 };
_GetShort_pathWith_Cirl(maze, cur, pre, entry, &cur_path, &short_path);
printf("最终的最短路径为:\n");
StackPrintf(&short_path);
}
void TestInit()
{
Maze maze;
MazeInit(&maze);
MazePrint(&maze);
}
void Test()
{
Maze maze;
MazeInit(&maze);
MazePrint(&maze);
point pos = { 0,1 };
GetShort_pashWith_Cirl(&maze, pos);
MazePrint(&maze);
}
int main()
{
Test();
system("pause");
return 0;
}