• 迷宫求解


    刚看数据结构(严蔚敏),很多东西不会。。

    这次。。看着伪代码。。好不容易。。写出来了。。源代码。。

    恩恩。。我好样的。。偷笑

    保留在这算是个纪念吧。。。

    //mainstruct.c(主函数)

    #include "my.h"
    
    #define N 10
    
    
    
    int map[N][N]={
    		{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}
    };
    
    
    int main()
    {
    	int i,j;
    
    
    	SqStack s;
    	PosType curpos,start={1,1},end={8,8};
    	ElemType e;
    	InitStack(&s);	curpos = start;	
    	int curstep = 1;
    	do
    	{
    		if( Pass(map,curpos) )	//当前位置可以通过,即是未曾走到过的通道块
    		{
    			FootPrint(map,curpos);
    			//给e赋值
    			e.ord = curstep;
    			e.seat = curpos;
    			e.di = 1;
    
    			Push(&s,e);
    			if(curpos.x == end.x && curpos.y == end.y)	break;;
    			curpos = NextPos( curpos, 1 );
    			curstep++;
    		}//if
    		else	//当前位置不能通过
    		{
    			if( ! EmptyStack(&s) )
    			{
    				Pop(&s, &e);
    
    				while(e.di == 4 && !EmptyStack(&s))
    				{
    					FootPrint(map,e.seat);
    					Pop(&s,&e);
    				}
    				if(e.di < 4)
    				{
    					e.di++;	Push(&s,e);
    					curpos = NextPos(e.seat, e.di);
    				}
    			}
    		}
    
    
    	}while( !EmptyStack(&s) );
    
    	//将路径输出
    	ElemType *ps;
    	ps = s.base;
    	while(ps != s.top )			//将求解的路径的值设置为2
    	{
    		map[ps->seat.x][ps->seat.y] = 2;
    		ps++;
    	}
    	//显示
    	for(i=0; i<N; i++)	//2为走的路径
    	{
    		for(j=0; j<N; j++)
    			printf("%d ",map[i][j]);
    		putchar('\n');
    	}
    
    
    	return 0;
    
    }
    
    //my.h
    #ifndef MY_H
    #define MY_H
    //head file
    #include <stdio.h>
    #include <stdlib.h>
    
    //definition
    #define STACK_INIT_SIZE 100
    #define STACKINCREMENT 10
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    
    typedef struct
    {
    	int x;
    	int y;
    }PosType;
    
    
    typedef struct
    {
    	int ord;		//通道块在路径上的‘序号’
    	PosType seat;	//通道块在迷宫中的“坐标位置”
    	int di;			//栈的元素类型
    }SElemType;
    
    
    //type
    typedef SElemType ElemType;
    typedef int Status;
    
    
    //struct
    typedef struct
    {
    	ElemType *base;
    	ElemType *top;
    	int stacksize;
    }SqStack;
    
    
    Status InitStack(SqStack *S);
    Status GetTop(SqStack *S,ElemType *e);			
    Status Push(SqStack *S, ElemType e);
    Status Pop(SqStack *S, ElemType *e);
    Status EmptyStack(SqStack *S);
    
    //function
    Status FootPrint(int pmap[][10],PosType pos);		//留下足迹
    Status Pass(int pmap[][10],PosType e);				//当前位置是否可以通过
    PosType NextPos(PosType e,int n);					//下一方向
    Status ClearFoot(int pmap[][10], PosType pos);		//清除走过的痕迹
    
    
    #endif //MY_H
    

    //my.c
    #include "my.h"
    
    Status InitStack(SqStack * S)
    {
    	S->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    	if( !S->base ) return ERROR;
    	S->stacksize = STACK_INIT_SIZE;
    	S->top = S->base;
    	return OK;
    }
    
    Status GetTop(SqStack * S, ElemType *e)
    {
    	if( S->top == S->base )	return ERROR;
    	*e = *(S->top-1);
    	return OK;
    }
    
    Status Push(SqStack *S, ElemType e)
    {
    	if( S->top - S->base >= S->stacksize )				//没有问题?
    	{
    		S->base = (ElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT)*sizeof(ElemType));
    		if( !S->base ) return ERROR;
    		S->top = S->base + S->stacksize;
    		S->stacksize += STACKINCREMENT;
    	}
    	*S->top++ = e;
    	return OK;
    }
    
    Status Pop(SqStack *S, ElemType *e)
    {
    	if( S->base == S->top )
    		return ERROR;
    	*e = *--S->top;
    	return OK;
    }
    
    Status EmptyStack(SqStack *S)
    {
    	if( S->base == S->top )
    		return OK;
    	else
    		return ERROR;
    }
    
    
    Status FootPrint(int pmap[][10],PosType pos)
    {
    	pmap[pos.x][pos.y] = 1;
    	return OK;
    }
    Status Pass(int pmap[][10],PosType e)
    {
    	if(pmap[e.x][e.y] == 1 || pmap[e.x][e.y] == 2)
    		return FALSE;
    	else
    		return TRUE;
    }
    PosType NextPos(PosType e,int n)
    {
    	switch(n)
    	{
    	case 1:		//right
    		e.y++;
    		break;
    	case 2:		//down
    		e.x++;
    		break;
    	case 3:		//left
    		e.y--;
    		break;
    	case 4:		//up
    		e.x--;
    		break;
    	}
    	return e;
    }
    
    
    最后输出时。。会将找到的路径的值设置为2.。。便形成了一条通路


  • 相关阅读:
    UML系列图--用例图
    扩展方法
    POJ3254 Corn Fields(状压DP)
    POJ2836 Rectangular Covering(状压DP)
    POJ2817 WordStack(状压DP)
    POJ2441 Arrange the Bulls(状压DP)
    HDU2167 Pebbles(状压DP)
    HDU1561 The more, The Better(树形DP)
    POJ3659 Cell Phone Network(树上最小支配集:树型DP)
    UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2883524.html
Copyright © 2020-2023  润新知