迷宫求解问题c语言程序设计,ÓÃջʵÏÖÃÔ¹¬ÎÊÌâÇó½â

Ô´³ÌÐò£º

//base.h

#include

#include

#include

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define OVERFLOW -2

typedef int Status;

//stack.h

#include "base.h"

#define INIT_SIZE 100 //´æ´¢¿Õ¼ä³õʼ·ÖÅäÁ¿

#define INCREMENT 10//´æ´¢¿Õ¼ä·ÖÅäÔöÁ¿

typedef struct{//ÃÔ¹¬ÖÐrÐÐcÁеÄλÖÃ

int r;

int c;

}PostType;

typedef struct{

int ord;//µ±Ç°Î»ÖÃÔÚ·¾¶ÉϵÄÐòºÅ

PostType seat;//µ±Ç°×ø±ê

int di;//ÍùÏÂÒ»×ø±êµÄ·½Ïò

}SElemType;//Õ»ÔªËØÀàÐÍ

typedef struct{

SElemType* base;//Õ»»ùÖ·,¹¹ÔìÇ°Ïú»ÙºóΪ¿Õ

SElemType* top;//Õ»¶¥

int stackSize;//Õ»ÈÝÁ¿

}Stack;//Õ»ÀàÐÍ

Status InitStack(Stack &S){//¹¹Ôì¿ÕÕ»s

S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType));

if(!S.base)

exit(OVERFLOW);//´æ´¢·ÖÅäʧ°Ü

S.top=S.base;

S.stackSize=INIT_SIZE;

return OK;

}//InitStack

Status StackEmpty(Stack S){

//ÈôsΪ¿Õ·µ»ØTRUE,·ñÔò·µ»ØFALSE

if(S.top==S.base)

return TRUE;

return FALSE;

}//StackEmpty

Status Push(Stack &S,SElemType e){

//²åÈëÔªËØeΪеÄÕ»¶¥ÔªËØ

if(S.top-S.base >=S.stackSize){//Õ»Âú£¬¼Ó¿Õ¼ä

S.base=(SElemType *)realloc(S.base,(S.stackSize+INCREMENT)*sizeof(SElemType));

if(!S.base)

exit(OVERFLOW);//´æ´¢·ÖÅäʧ°Ü

S.top=S.base+S.stackSize;

S.stackSize+=INCREMENT;

}

*S.top++=e;

return OK;

}//push

Status Pop(Stack &S,SElemType &e){//ÈôÕ»²»¿Õɾ³ýÕ»//¶¥ÔªËØÓÃe·µ»Ø²¢·µ»ØOK£¬·ñÔò·µ»ØERROR

if(S.top==S.base)

return ERROR;

e=*--S.top;

return OK;

}//Pop

Status DestroyStack(Stack &S){//Ïú»ÙÕ»S,

free(S.base);

S.top=S.base;

return OK;

}//DestroyStack

//maze.cpp

#include "stack.h"

#define MAXLEN 10//ÃÔ¹¬°üÀ¨Íâǽ×î´óÐÐÁÐÊýÄ¿

typedef struct{

int r;

int c;

char adr[MAXLEN][MAXLEN];//¿ÉÈ¡' ''*' '@' '#'

}MazeType;//ÃÔ¹¬ÀàÐÍ

Status InitMaze(MazeType &maze){

//³õʼ»¯ÃÔ¹¬Èô³É¹¦·µ»ØTRUE,·ñÔò·µ»ØFALSE

int m,n,i,j;

printf("Enter row and column numbers: ");

scanf("%d%d",&maze.r,&maze.c); //ÃÔ¹¬ÐкÍÁÐÊý

for(i=0;i<=maze.c+1;i++){//ÃÔ¹¬ÐÐÍâǽ

maze.adr[0][i]='#';

maze.adr[maze.r+1][i]='#';

}//for

for(i=0;i<=maze.r+1;i++){//ÃÔ¹¬ÁÐÍâǽ

maze.adr[i][0]='#';

maze.adr[i][maze.c+1]='#';

}

for(i=1;i<=maze.r;i++)

for(j=1;j<=maze.c;j++)

maze.adr[i][j]=' ';//³õʼ»¯ÃÔ¹¬

printf("Enter block's coordinate((-1,-1) to end): ");

scanf("%d%d",&m,&n);//½ÓÊÕÕÏ°­µÄ×ø±ê

while(m!=-1){

if(m>maze.r || n>maze.c)//Ô½½ç

exit(ERROR);

maze.adr[m][n]='#';//ÃÔ¹¬ÕÏ°­ÓÃ'#'±ê¼Ç

printf("Enter block's coordinate((-1,-1) to end): ");

scanf("%d%d",&m,&n);

}//while

return OK;

}//InitMaze

StatusPass(MazeType maze,PostType curpos){

//µ±Ç°Î»ÖÿÉͨÔò·µ»ØTURE,·ñÔò·µ»ØFALSE

if(maze.adr[curpos.r][curpos.c]==' ')//¿Éͨ

return TRUE;

else

return FALSE;

}//Pass

Status FootPrint(MazeType &maze,PostType curpos){

//Èô×ß¹ý²¢ÇÒ¿Éͨ·µ»ØTRUE,·ñÔò·µ»ØFALSE

//ÔÚ·µ»Ø֮ǰÏú»ÙÕ»S

maze.adr[curpos.r][curpos.c]='*';//"*"±íʾ¿Éͨ

return OK;

}//FootPrint

PostType NextPos(PostType &curpos,int i){

//ָʾ²¢·µ»ØÏÂһλÖõÄ×ø±ê

PostType cpos;

cpos=curpos;

switch(i){//1.2.3.4·Ö±ð±íʾ¶«,ÄÏ,Î÷,±±·½Ïò

case 1 : cpos.c+=1; break;

case 2 : cpos.r+=1; break;

case 3 : cpos.c-=1; break;

case 4 : cpos.r-=1; break;

default: exit(ERROR);

}

return cpos;

}//Nextpos

Status MarkPrint(MazeType &maze,PostType curpos){

//Ôø×ß¹ýµ«²»ÊÇͨ·±ê¼Ç²¢·µ»ØOK

maze.adr[curpos.r][curpos.c]='@';//"@"±íʾÔø×ß¹ýµ«²»Í¨

return OK;

}//MarkPrint

Status MazePath(MazeType &maze,PostType start,PostType end){

//ÈôÃÔ¹¬maze´æÔÚ´ÓÈë¿Ústartµ½endµÄͨµÀÔòÇóµÃÒ»Ìõ´æ·ÅÔÚÕ»ÖÐ

//²¢·µ»ØTRUE,·ñÔò·µ»ØFALSE

Stack S;

PostType curpos;

int curstep;//µ±Ç°ÐòºÅ,1.2.3.4·Ö±ð±íʾ¶«,ÄÏ,Î÷,±±·½Ïò

SElemType e;

InitStack(S);

curpos=start; //ÉèÖÃ"µ±Ç°Î»ÖÃ"Ϊ"Èë¿ÚλÖÃ"

curstep=1;//̽Ë÷µÚÒ»²½

do{

if(Pass(maze,curpos)){//µ±Ç°Î»ÖÿÉÒÔͨ¹ý,

//¼´ÊÇδÔø×ßµ½¹ýµÄͨµÀ

FootPrint(maze,curpos);//ÁôÏÂ×ã¼£

e.ord=curstep;

e.seat=curpos;

e.di=1;

Push(S,e);//¼ÓÈë·¾¶

if(curpos.r==end.r&& curpos.c==end.c)

if(!DestroyStack(S))//Ïú»Ùʧ°Ü

exit(OVERFLOW);

else

return TRUE; //µ½´ï³ö¿Ú

else{

curpos=NextPos(curpos,1);

//ÏÂһλÖÃÊǵ±Ç°Î»ÖõĶ«ÁÚ

curstep++;//̽Ë÷ÏÂÒ»²½

}//else

}//if

else{//µ±Ç°Î»Öò»Í¨

if(!StackEmpty(S)){

Pop(S,e);

while(e.di==4

&& !StackEmpty(S)){

MarkPrint(maze,e.seat);

Pop(S,e);

//Áôϲ»ÄÜͨ¹ýµÄ±ê¼Ç,²¢ÍËÒ»²½

}//while

if(e.di < 4){

e.di++;//»»ÏÂÒ»¸ö·½Ïò̽Ë÷

Push(S,e);

curpos=NextPos(e.seat,e.di);//É趨µ±Ç°Î»ÖÃÊǸÃ

//з½ÏòÉϵÄÏàÁÚ

}//if

}//if

}//else

}while(!StackEmpty(S));

if(!DestroyStack(S))//Ïú»Ùʧ°Ü

exit(OVERFLOW);

else

return FALSE;

}//MazePath

void PrintMaze(MazeType &maze){

//½«±ê¼Ç·¾¶ÐÅÏ¢µÄÃÔ¹¬Êä³öµ½ÖÕ¶Ë(°üÀ¨Íâǽ)

int i,j;

printf("\nShow maze path(*---pathway):\n\n");

printf("");

for(i=0;i<=maze.r+1;i++)//´òÓ¡ÁÐÊýÃû

printf("%4d",i);

printf("\n\n");

for(i=0;i<=maze.r+1;i++){

printf("%2d",i);//´òÓ¡ÐÐÃû

for(j=0;j<=maze.c+1;j++)

printf("%4c",maze.adr[i][j]);//Êä³öÃÔ¹¬//µ±Ç°Î»Öõıê¼Ç

printf("\n\n");

}

}//PrintMaze

void main(){//Ö÷º¯Êý

MazeType maze;

PostType start,end;

char cmd;

do{

printf("-------FOUND A MAZEPATH--------\n");

if(!InitMaze(maze)){ //³õʼ»¯²¢´´½¨ÃÔ¹¬

printf("\nInitialization errors!!!\n");

exit(OVERFLOW);//³õʼ»¯´íÎó

}

do{//ÊäÈëÃÔ¹¬Èë¿Ú×ø±ê

printf("\nEnter entrance coordinate of the maze: ");

scanf("%d%d",&start.r,&start.c);

if(start.r>maze.r || start.c>maze.c){

printf("\nBeyond the maze!!!\n");

continue;

}

}while(start.r>maze.r || start.c>maze.c);

do{//ÊäÈëÃÔ¹¬³ö¿Ú×ø±ê

printf("\nEnter exit coordinate of the maze: ");

scanf("%d%d",&end.r,&end.c);

if(end.r>maze.r || end.c>maze.c){

printf("\nBeyond the maze!!!\n");

continue;

}

}while(end.r>maze.r || end.c>maze.c);

if(!MazePath(maze,start,end))//ÃÔ¹¬Çó½â

printf("\nNo path from entrance to exit!\n");

else

PrintMaze(maze);//´òӡ·¾¶

printf("\nContinue?(y/n): ");

scanf("%s",&cmd);

}while(cmd=='y' || cmd=='Y');

}//main

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值