1. 程式人生 > >#複雜迷宮求解(2)

#複雜迷宮求解(2)

#標頭檔案

#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define ROW 6 
#define COL 6
typedef struct Maze
{
	int _map[ROW][COL];
}Maze;
typedef struct Position
{
	int _x;
	int _y;
}Position;
//Stack.h


#define MAX 100
typedef Position DataType;
typedef struct Stack { int top; DataType stack[MAX]; }Stack; void StackInit(Stack* s); void StackPush(Stack* s, DataType data); void StackPop(Stack* s); DataType GetStackTop(Stack*s); int GetStackSize(Stack*s); int StackEmpty(Stack *s); // 初始化迷宮 void InitMaze(Maze* m, int map[ROW][COL]); // 列印迷宮
void PrintMaze(Maze* m); // 檢測是否為有效的入口 int IsValidEnter(Maze* m, Position enter); // 檢測pos是否在出口的位置 int IsExit(Position pos, Position enter); // 檢測cur的下一步next是否為通路: // next位置為1 || next位置儲存資料 大於 cur位置儲存資料 int IsNextPass(Maze* m, Position next, Position cur); // 儲存最短的路徑 void SaveShortPath(Stack* path, Stack*
shortPath); // 走迷宮 void _GetMazeShortPath(Maze* m, Position cur, Position enter, Stack* path, Stack* shortPath); void PassMaze(Maze* m, Position enter, Stack* shortPath);

#操作函式

//Stack.c
#include"HardMaze.h"
void StackInit(Stack* s)
{
	s->top = 0;
}
void StackPush(Stack* s, DataType data)
{
	if (s->top == MAX)
	{
		printf("棧已滿!無法入棧元素\n");
		return;
	}

	(s->top)++;
	s->stack[s->top] = data;
}
void StackPop(Stack *s)
{
	if (s->top == 0)
	{
		printf("棧為空,無法出棧元素\n");
		return;
	}
	(s->top)--;
}
DataType GetStackTop(Stack*s)
{
	if (s->top == 0)
	{
		printf("棧為空,無法獲取棧頂\n");
	}
	return s->stack[s->top];
}
int GetStackSize(Stack* s)
{
	return s->top;
}
int StackEmpty(Stack *s)
{
	if (s->top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//Maze.c
#include"HardMaze.h"
void InitMaze(Maze * m, int map[ROW][COL])
{
	int i = 0;
	int j = 0;
	assert(m);
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			m->_map[i][j] = map[i][j];
		}
	}
}
void PrintMaze(Maze* m)
{
	assert(m);
	int i = 0;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			printf(" %d ", m->_map[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
int IsValidEnter(Maze* m, Position enter)
{
	assert(m);
	if (enter._x == ROW - 1 || enter._x == 0 || enter._y == COL - 1 || enter._y == 0)
	{
		return 1;
	}
	else
		return 0;
}
// pos 必須在地圖中 pos位置如果是1 
int IsPass(Maze* m, Position pos)
{
	assert(m);
	if (pos._x >= ROW || pos._x < 0 || pos._y >= COL || pos._y >= COL)
	{
		return 0;
	}
	else if (m->_map[pos._x][pos._y] != 1)
	{
		return 0;
	}
	else
		return 1;
}
// 檢測是否在出口的位置 
int IsExit(Position pos, Position enter)
{
	if ((pos._x == ROW - 1 || pos._x == 0 || pos._y == COL - 1 || pos._y == 0) && ((pos._x != enter._x) || (pos._y) != enter._y))
	{
		return 1;
	}
	return 0;
}

int IsNextPass(Maze* m, Position next, Position cur)
{
	if (m->_map[next._x][next._y] == 1)
	{
		return 1;
	}
	else if (m->_map[next._x][next._y] > m->_map[cur._x][cur._y])
	{
		return 1;
	}
	return 0;
}
// 儲存最短的路徑 

void SaveShortPath(Stack* path, Stack* shortPath)
{
	int i = 0;
	if (StackEmpty(shortPath))
	{
		for (i = 0; i < GetStackSize(path); i++)
		{
			StackPush(shortPath, path->stack[i]);
		}
	}
	else if (GetStackSize(path) < GetStackSize(shortPath))
	{
		shortPath->top = 0;
		for (i = 0; i < GetStackSize(path); i++)
		{
			StackPush(shortPath, path->stack[i]);
		}
	}
}

void PassMaze(Maze* m, Position enter, Stack* shortPath)
{
	Stack path;
	if (!IsValidEnter(m, enter))
	{
		printf("迷宮的入口異常!!!\n");
		return;
	}

	StackInit(&path);
	StackInit(shortPath);
	_GetMazeShortPath(m, enter, enter, &path, shortPath);
}

void _GetMazeShortPath(Maze* m, Position cur, Position enter, Stack* path, Stack* shortPath)
{

	PrintMaze(m);

	m->_map[enter._x][enter._y] = 2;
	if (IsExit(cur, enter))
	{
		SaveShortPath(path, shortPath);
	}
	StackPush(path, cur);
	Position next = cur;
	next._x -= 1;
	if (IsNextPass(m, next, cur))
	{
	      m->_map[next._x][next._y]	= m->_map[cur._x][cur._y] + 1;
		  _GetMazeShortPath(m, next, enter, path, shortPath);
	}
	next = cur;
	next._y -= 1;
	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);
	}
	next = cur;
	next._y += 1;
	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);

	}
	next = cur;
	next._x += 1;

	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);
	}
	StackPop(path);
}
#include"HardMaze.h"
int main()

{
	int map[ROW][COL] = {
		{ 0,0,0,0,0,0 },
		{ 0,1,1,1,0,0 },
		{ 0,1,0,1,0,0 },
		{ 0,1,0,1,0,0 },
		{ 0,1,1,1,1,1 },
		{ 0,1,0,0,0,0 }
	};
	Maze m;
	Stack shortPath;
	InitMaze(&m, map);
	Position enter;
	enter._x = 5;
	enter._y = 1;
	PrintMaze(&m);
	PassMaze(&m, enter, &shortPath);
	PrintMaze(&m);
	system("pause");
	return 0;
}

這裡寫圖片描述