#include 'maze.h'

int map[row][col] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1}, {1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1}, {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; // 迷宫地图,1代表墙壁,0代表路

int enterx = 1, entery = 1; // 入口坐标 int exitx = 6, exity = 9; // 出口坐标

Path path[(row - 1) * (col - 1)]; // 探索迷宫的路径 int pathLength = 0; // 路径长度

int main(int argc, char* argv[]) { LinkStack L; L.count = 0; CreateLink(&L);

bool flag = Maze(&L);
	
if (flag == true) {
	PrintPath();
} else {
	printf('Can not find path');
}

return 0;

}

// 判断是否是走过的迷宫块 bool Panduan(int x, int y) { if (map[x][y] == 0) { return true; }

return false;

}

// 探索迷宫可通行的路径 bool Maze(LinkStack* L) { bool flag = false;

LinkStackPtr p;
p = (LinkStackPtr)malloc(sizeof(struct StackNode));
p->position.x = enterx;
p->position.y = entery;
p->direct.top = 1;
p->direct.right = 0;
p->direct.left = 0;
p->direct.bottom = 0;
map[p->position.x][p->position.y] = 2;

// 将入口结点入栈
Push(L, p);

while (!IsEmpty(L)) {
	// 获取栈顶结点
	LinkStackPtr current = GetTop(L);
	
	// 如果当前结点是出口,设置flag为true,结束循环
	if (current->position.x == exitx && current->position.y == exity) {
		flag = true;
		break;
	}
	
	// 判断是否可以向上移动
	if (current->direct.top == 1 && Panduan(current->position.x - 1, current->position.y)) {
		LinkStackPtr next = (LinkStackPtr)malloc(sizeof(struct StackNode));
		next->position.x = current->position.x - 1;
		next->position.y = current->position.y;
		next->direct.top = 1;
		next->direct.right = 0;
		next->direct.left = 0;
		next->direct.bottom = 0;
		map[next->position.x][next->position.y] = 2;
		
		// 将下一个结点入栈
		Push(L, next);
		
		// 记录探索路径
		RecordExplorationPath(next);
		
		continue;
	}
	
	// 判断是否可以向右移动
	if (current->direct.right == 1 && Panduan(current->position.x, current->position.y + 1)) {
		LinkStackPtr next = (LinkStackPtr)malloc(sizeof(struct StackNode));
		next->position.x = current->position.x;
		next->position.y = current->position.y + 1;
		next->direct.top = 0;
		next->direct.right = 1;
		next->direct.left = 0;
		next->direct.bottom = 0;
		map[next->position.x][next->position.y] = 2;
		
		// 将下一个结点入栈
		Push(L, next);
		
		// 记录探索路径
		RecordExplorationPath(next);
		
		continue;
	}
	
	// 判断是否可以向左移动
	if (current->direct.left == 1 && Panduan(current->position.x, current->position.y - 1)) {
		LinkStackPtr next = (LinkStackPtr)malloc(sizeof(struct StackNode));
		next->position.x = current->position.x;
		next->position.y = current->position.y - 1;
		next->direct.top = 0;
		next->direct.right = 0;
		next->direct.left = 1;
		next->direct.bottom = 0;
		map[next->position.x][next->position.y] = 2;
		
		// 将下一个结点入栈
		Push(L, next);
		
		// 记录探索路径
		RecordExplorationPath(next);
		
		continue;
	}
	
	// 判断是否可以向下移动
	if (current->direct.bottom == 1 && Panduan(current->position.x + 1, current->position.y)) {
		LinkStackPtr next = (LinkStackPtr)malloc(sizeof(struct StackNode));
		next->position.x = current->position.x + 1;
		next->position.y = current->position.y;
		next->direct.top = 0;
		next->direct.right = 0;
		next->direct.left = 0;
		next->direct.bottom = 1;
		map[next->position.x][next->position.y] = 2;
		
		// 将下一个结点入栈
		Push(L, next);
		
		// 记录探索路径
		RecordExplorationPath(next);
		
		continue;
	}
	
	// 如果四个方向都无法移动,则将当前结点出栈
	Pop(L);
}

if (flag == true) {
	PrintPath();
} else {
	printf('Can not find path');
}

return flag;

}

// 判断链栈是否为空 bool IsEmpty(LinkStack *L) { if (L->count == 1) return true; return false; }

// 创建链栈 void CreateLink(LinkStack *L) { L->top = (LinkStackPtr)malloc(sizeof(struct StackNode)); if (!L->top) { exit(1); } L->top = NULL; L->count++; }

// 获取栈顶 LinkStackPtr GetTop(LinkStack *L) { return L->top; }

// 进栈 void Push(LinkStack *L, LinkStackPtr s) { s->next = L->top; L->top = s; L->count++; }

// 出栈 void Pop(LinkStack *L) { LinkStackPtr p; if (IsEmpty(L)) { return; } p = L->top; L->top = L->top->next; free(p); L->count--; }

// 记录探索路径 void RecordExplorationPath(LinkStackPtr m){ path[pathLength].x = m->position.x; path[pathLength].y = m->position.y; path[pathLength].seq = pathLength + 1; pathLength += 1; }

// 打印迷宫可通行的路径 void PrintPath(){ for (int i = 0; i < pathLength; i++) { printf('%d:(%d, %d)\n',path[i].seq, path[i].x, path[i].y); } return;

C语言迷宫求解算法实现 - 寻找最短路径

原文地址: https://www.cveoy.top/t/topic/peMX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录