严蔚敏迷宫程序

看了一晚上才都弄懂。。。水平好次。。。   T^T

solve函数是关键。

// maze.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR -1
#define MAXSIZE 10
#define TRUE 1
#define FALSE 0

typedef enum direction{right,down,left,up};
typedef enum markflag{yes,no};

typedef struct Position
{
	int x;
	int y;
}position;

typedef struct
{
	int order;
	position seat;
	direction di;
}selemtype;
typedef struct
{
	int top;
	selemtype* elem;
}stack;
char maze[MAXSIZE][MAXSIZE] = {
	{ '0', '0', '1', '1', '1', '1', '1', '1', '1', '1' },

	{ '1', '0', '0', '1', '0', '0', '0', '1', '0', '1' },

	{ '1', '0', '0', '1', '0', '0', '0', '1', '0', '1' },

	{ '1', '0', '0', '0', '0', '1', '1', '0', '0', '1' },

	{ '1', '0', '1', '1', '1', '0', '0', '0', '0', '1' },

	{ '1', '0', '0', '0', '1', '0', '0', '0', '0', '1' },

	{ '1', '0', '1', '0', '0', '0', '1', '0', '0', '1' },

	{ '1', '0', '1', '1', '1', '0', '1', '1', '0', '1' },

	{ '1', '1', '0', '0', '0', '0', '0', '0', '0', '1' },

	{ '1', '1', '1', '1', '1', '1', '1', '1', '0', '1' }
};
//-------------------------------------------------------------------------
int init_stack(stack* s);
int push(stack* s, selemtype e);
int pop(stack* s, selemtype* e);
int empty(stack* s);
int creata_maze(position* startpos, position* endpos);
int can_pass(position pos);
int mark_pos(position curpos, markflag flag);
position next_pos(position curpos, direction dir);
direction next_dir(direction dir);
int solve(stack* s, position start, position end);
//------------------------------------------------------------------------ -
int init_stack(stack* s)
{
	s->elem = (selemtype*)malloc(MAXSIZE*MAXSIZE*sizeof(selemtype));
	s->top = 0;
	if (!s->elem)
		return ERROR;
	return OK;
}
int push(stack* s, selemtype e)
{
	if (s->top>MAXSIZE*MAXSIZE)
		return ERROR;
	s->top++;
	s->elem[s->top] = e;
	return OK;
}
int pop(stack* s, selemtype* e)
{
	if (s->top <= 0)
		return ERROR;
	*e = s->elem[s->top];
	s->top--;
	return OK;
}
int empty(stack* s)
{
	if (s->top == 0)
		return TRUE;
	return FALSE;
}
int creata_maze(position* startpos, position* endpos)
{
	position start, end;
	printf("请输入迷宫入口:");
	scanf("%d %d", &start.x, &start.y);
	printf("请输入迷宫出口:");
	scanf("%d %d", &end.x, &end.y);

	*startpos = start;
	*endpos = end;
	return OK;
}
int can_pass(position pos)
{
	if (maze[pos.x][pos.y] == '0')
		return TRUE;
	return FALSE;
}
int mark_pos(position curpos, markflag flag)
{
	switch (flag)
	{
	case yes:
		maze[curpos.x][curpos.y] = '.';
		break;
	case no:
		maze[curpos.x][curpos.y] = '#';
		break;
	}
	return OK;
}
position next_pos(position curpos, direction dir)
{
	switch (dir)
	{
	case right:
		curpos.y++;
		break;
	case down:
		curpos.x++;
		break;
	case left:
		curpos.y--;
		break;
	case up:
		curpos.x--;
		break;
	}
	return curpos;
}
direction next_dir(direction dir)
{
	switch (dir)
	{
	case right:
		return down;
	case down:
		return left;
	case left:
		return up;
	}
}
int solve(stack* s, position start, position end)
{
	position curpos;
	selemtype e;
	int curstep = 1;
	if (init_stack(s) == ERROR)
		return FALSE;
	curpos = start;
	do
	{
		if (can_pass(curpos))
		{
			mark_pos(curpos,yes);
			e.di = right;
			e.seat = curpos;
			e.order = curstep;
			push(s, e);
			if (curpos.x == end.x&&curpos.y == end.y)
				return TRUE;
			curpos = next_pos(curpos, right);
			curstep++;
		}
		else
		{
			if (!empty(s))
	         //出栈   获得上一个位置
				pop(s, &e);
			if (e.di != up)
			{
				//上一个位置换方向走
				e.di = next_dir(e.di);
				push(s, e);
				curpos = next_pos(e.seat, e.di);
				//不是next_pos(curpos, e.di)    
			}

			while (e.di == up&&!empty(s))
			{
				//上一个位置四个方向都不行
				//令上一位置等于当前位置   回溯到有方向可走的pos 或是startpos
				curpos = e.seat;
				mark_pos(curpos, no);
				if (pop(s, &e) == ERROR)
					return FALSE;
			}
		}
	} while (!empty(s));
	return FALSE;
}
int _tmain(int argc, _TCHAR* argv[])
{
	position startpos, endpos;
	stack path;
	selemtype e;
	int i, j;
	if (creata_maze(&startpos, &endpos) == ERROR)
		exit(1);
	solve(&path, startpos, endpos);
	while (!empty(&path))
	{
		pop(&path, &e);
		printf("%d%d", e.seat.x, e.seat.y);
		printf(" ");
	}
	printf("\n");
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
			printf("%c", maze[i][j]); 
		printf("\n");
	}
	system("pause");
	return 0;
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值