栈求迷宫问题

个人摸索写的代码,不保证严谨和安全性,仅供参考而已。

 

stack.h

/*
 * stack.h
 *
 *  Created on: 2012年8月20日
 *      Author: h
 */

#ifndef STACK_H_
#define STACK_H_

#define STACK_INIT_SIZE  10
#define STACK_INCREME_SIZE  10

//typedef int ElemType;

typedef struct
{
	ElemType *top;
	ElemType *base;
	int size;
} STACK;

STACK * initStack();

int push(STACK *s, ElemType *e);

int pop(STACK *s, ElemType *e);

int isEmpty(STACK *s);

void destoryStack(STACK *s);
#endif /* STACK_H_ */

 stack

/*
 * stack.c
 *
 *  Created on: 2012年8月20日
 *      Author: h
 */

#include "maze.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

STACK * initStack()
{
	STACK *s = (STACK *)malloc(sizeof(STACK));
	if (s == NULL)
		exit(0);

	s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
	if (s->base == NULL)
		exit(0);

	//s->base = s->top;
	s->top=s->base;
	s->size = STACK_INIT_SIZE;
	return s;
}

void destoryStack(STACK *s)
{
	free(s->base);
	free(s);
}

int push(STACK *s, ElemType *e)
{
	if (s == NULL || e == NULL)
		return 0;

	if ((s->top - s->base) >= s->size)
	{
		s->base = (ElemType *)realloc(s->base,
				(s->size + STACK_INCREME_SIZE) * sizeof(ElemType));
		if (s == NULL)
			return 0;
		s->top = s->base + s->size;
		s->size = s->size + STACK_INCREME_SIZE;
	}
	*s->top = *e;
	s->top++;
	//s->top++ = *e
	return 1;
}

int pop(STACK *s, ElemType *e)
{
	if (s == NULL || e == NULL)
		return 0;

	if (s->top == s->base)
		return 0;
	s->top--;
	*e = *s->top;
	//*e = *--s->top
	return 1;
}

int isEmpty(STACK *s)
{
	return s->base == s->top ? 1 : 0;
}

 

 maze.h

/*
 * maze.h
 *
 *  Created on: 2012年8月21日
 *      Author: h
 */

#ifndef MAZE_H_
#define MAZE_H_

typedef struct
{
	int y, x;
} POS;
typedef struct
{
	int di;
	int sno;
	POS seat;
} ElemType;

int isPass(POS p, int item[][10]);

POS getNext(POS curr, int di);

void printMaze(POS curr, int item[][10]);
#endif /* MAZE_H_ */

 

maze.c

/*
 * maze.c
 *
 *  Created on: 2012年8月21日
 *      Author: h
 */
#include "maze.h"
#include <stdlib.h>
#include <stdio.h>
int isPass(POS p, int item[][10])
{
	return item[p.y][p.x] == 0 ? 1 : 0;
}

POS getNext(POS curr, int di)
{
	POS p = curr;
	switch (di)
	{
	case 0:
		p.x--;
		break;
	case 1:
		p.y++;
		break;
	case 2:
		p.x++;
		break;
	case 3:
		p.y--;
		break;
	}
	return p;
}

void printMaze(POS curr, int item[][10])
{
	int i, j;
	system("cls");
	printf("\n");
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (i == curr.y && j == curr.x)
			{
				printf("@");
				continue;
			}
			if (item[i][j] != 0)
				printf("%d",item[i][j]);
			else
				printf(" ");
		}
		printf("\n");
	}
}

 

main.c

/*
 * main.c
 *
 *  Created on: 2012年8月20日
 *      Author: h
 */

#include "maze.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

static int item[10][10] =
{
{ 1, 1, 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, 1, 1 } };
static const POS inPos =
{ 1, 1 }, outPOS =
{ 8, 8 };

//求迷宫出口
int main(int argc, char **argv)
{
	ElemType e;
	POS currPos = inPos;
	int step = 1;
	STACK *s = initStack();
	do
	{
		if (isPass(currPos, item))
		{
			e.sno = step;
			e.seat = currPos;
			e.di = 0;
			push(s, &e);
			item[currPos.y][currPos.x] = 2;
			if (currPos.x == outPOS.x && currPos.y == outPOS.y)
			{
				printf("ok!\n");
				getch();
				break;
			}
			step++;
			printMaze(currPos, item);
			currPos = getNext(currPos, 0);
			getch();
		} else
		{
			pop(s, &e);
			if (e.di == 4 && !isEmpty(s))
			{
				item[currPos.y][currPos.x] = 3;
				pop(s, &e);
				currPos=e.seat;
			}
			if (e.di <= 3)
			{
				e.di++;
				push(s, &e);
				currPos = getNext(e.seat, e.di);
			}
		}
	} while (!isEmpty(s));
	return 1;
}

/*
 int main(int argc, char **argv)
 {

 int num = 1348, temp;

 STACK *s = initStack();
 while (num)
 {
 temp = num % 8;
 push(s, &temp);
 num /= 8;
 }
 printf("the result is \n");
 while (!isEmpty(s))
 {
 pop(s, &temp);
 printf("%d", temp);
 }
 printf("\n");
 destoryStack(s);
 return 1;
 }*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值