c语言,迷宫最短路径

 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 30//迷宫最大30*30
#define ENDS -2//端点位置标记为-2
#define ROUT -1//入队的路径标记为-1
#define SHORTESTPATH 2//将最终的最短路径标记为2
//队列结构,保存进行广度优先搜索时的路径
typedef struct QueueNode
{
    int a[2];//记录当前对应的位置
    int source;//记录新节点是由哪个节点探测的
    int self;//记录新节点在队列中的位置
    struct QueueNode *parent,*next;//双亲域
}Queue;
Queue *front;//队头指针
Queue *rear;//队尾指针
//栈结构,保存已经探测好的路径
typedef struct Path
{
    int a[2];
    struct Path *next;
}PathStack;
int MapRand(int i,int j,int maze[MAX][MAX])//随机迷宫生成函数,百分之六十是墙,百分之四十是通路
{
    float scale=0.4;//墙的比例,百分之六十
    int count=0;//计数器,在对迷宫比例进行局部调整的时候使用
    time_t t;//设定时间种子
    srand((unsigned)time(&t));
    int m,n;//控制循环变量
    for(m=0;m<i;m++)
    for(n=0;n<j;n++)
    maze[m][n]=rand()%10;
    for(m=0;m<i;m++)//0代表墙,1代表路
    for(n=0;n<j;n++)
    if(maze[m][n]<5)
    maze[m][n]=0;
    else
    maze[m][n]=1;
    //控制墙和路的比例
    for(n=0;n<j;n++)
    {
        maze[0][n]=0;
        maze[i-1][n]=0;
    }
    for(m=1;m<i-1;m++)
    {
        maze[m][0]=0;
        maze[m][j-1]=0;
    }
    //将外围置为墙,防止出界
    for(m=1;m<i-1;m++)
    for(n=1;n<j-1;n++)
    if(maze[m][n]==0)
    count++;
    //统计墙的个数
    int total;//墙的总数
    total=(int)(i-2)*(j-2)*scale;
    while(count!=total)
    if(count<total)
    {
        m=rand()%(i-2)+1;
        n=rand()%(j-2)+1;
        if(maze[m][n]==1)
        {
            maze[m][n]=0;
            count++;
        }
    }
    else
    {
        m=rand()%(i-2)+1;
        n=rand()%(j-2)+1;
        if(maze[m][n]==0)
        {
            maze[m][n]=1;
            count--;
        }
    }
    //对迷宫进行局部随机调整,使墙和路的比例合乎规定
    return 0;
}
int PrintMap(int i,int j,int maze[MAX][MAX])//打印迷宫
{
    int m,n;
    printf(" ");
    for(n=0;n<j;n++)
    printf("%d",n%10);
    printf("\n");
    for(m=0;m<i;m++)
    {
        printf("%d",m%10);
        for(n=0;n<j;n++)
        {
            if(maze[m][n]==0)
            printf("%c",219);
            //墙
            if(maze[m][n]==1||maze[m][n]==ROUT)
            printf(" ");
            if(maze[m][n]==SHORTESTPATH)
            printf("*");
            //路径
            if(maze[m][n]==ENDS)
            printf("%c",1);//笑脸
            //开始位置和终点位置
        }
        printf("\n");
    }
    printf("\n");
    return 0;
}
int InitQueue(int maze[MAX][MAX])//构造一个队列,并把起始位置加入队列
{
    int m,n;//起始位置坐标
    printf("Please enter the starting location(i,j):");
    scanf("%d%d",&m,&n);
    getchar();
    while(maze[m][n]==0)
    {
        printf("This seat is unacceptable,please reenter the starting location(i,j):");
        scanf("%d%d",&m,&n);
        getchar();
    }
    maze[m][n]=ENDS;//开始位置
    Queue *q;
    q=(Queue *)malloc(sizeof(Queue));
    if(q==NULL)
    {
        printf("Error1!");
        exit(1);
    }
    q->a[0]=m;
    q->a[1]=n;
    q->parent=q->next=NULL;
    q->source=0;
    q->self=0;
    front=rear=q;
    return 0;
}
int EnQueue(int m,int n)//入队函数
{
    Queue *q;
    q=(Queue *)malloc(sizeof(Queue));
    if(q==NULL)
    {
        printf("Error2!");
        exit(1);
    }
    q->a[0]=m;
    q->a[1]=n;
    q->next=NULL;
    q->self=rear->self+1;
    q->source=front->self;
    rear->next=q;
    q->parent=rear;
    rear=q;
    return 0;
}
PathStack *pushstack(PathStack *top,int m,int n)//入栈函数
{
    PathStack *q;
    q=(PathStack *)malloc(sizeof(PathStack));
    if(q==NULL)
    {
        printf("Error3!");
        exit(1);
    }
    q->a[0]=m;
    q->a[1]=n;
    q->next=top;
    top=q;
    return top;
}
int ShortestPath_BFS(int maze[MAX][MAX])//广度优先搜索,寻找最短路径
{
    int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//四个方向,依次是右,下,左,上
    int i;//控制循环变量,实现四个方向的转换
    int m,n;//记录坐标位置,用此位置来探测下一位置
    int end1,end2;//终点坐标位置
    printf("please enter the end position(i,j):");
    scanf("%d%d",&end1,&end2);
    getchar();
    while(maze[end1][end2]==0)
    {
        printf("This seat is unacceptable,please reenter the end position(i,j):");
        scanf("%d%d",&end1,&end2);
        getchar();
    }
    while(front)
    {
        m=front->a[0];
        n=front->a[1];
        for(i=0;i<4;i++)
        {
            int x,y;
            x=m+move[i][0];
            y=n+move[i][1];
            if(maze[x][y]==1)
            {
                maze[x][y]=ROUT;//将入队的位置标记为-1
                EnQueue(x,y);
                if(x==end1&&y==end2)
                return 0;
            }
        }
        front=front->next;
    }
    printf("Sorry,No can reach path!\n");
    return 1;
}
PathStack * Shortestpath(PathStack *top,int maze[MAX][MAX])//用栈结构保存路径
{
    int m,n;//记录指针对应的位置
    int t;//记录该位置的来源,即由哪个节点探测的
    Queue *p=rear;
    m=p->a[0];
    n=p->a[1];
    top=pushstack(top,m,n);
    maze[m][n]=ENDS;
    t=p->source;
    while(1)
    {
        p=p->parent;
        if(p->self==t)
        break;
    }
    while(p->parent)
    {
        m=p->a[0];
        n=p->a[1];
        maze[m][n]=SHORTESTPATH;
        top=pushstack(top,m,n);
        t=p->source;
        while(1)
        {
            p=p->parent;
            if(p->self==t)
            break;
        }
    }
    m=p->a[0];
    n=p->a[1];
    maze[m][n]=ENDS;
    top=pushstack(top,m,n);
    return top;
}
int PrintPath(PathStack *top,PathStack *base)//打印路径
{
    int k=0;//控制输出格式
    int m,n;//迷宫位置
    m=top->a[0];
    n=top->a[1];
    printf("(%d,%d)%c",m,n,26);
    k++;
    top=top->next;
    while(top->next!=base)
    {
        m=top->a[0];
        n=top->a[1];
        printf("(%d,%d)%c",m,n,26);
        k++;
        if(k==5)
        {
            printf("\n");
            k=0;
        }
        top=top->next;
    }
    m=top->a[0];
    n=top->a[1];
    printf("(%d,%d)\n",m,n);
    return 0;
}
int main()
{
    int maze[MAX][MAX];
    int i,j;//i行数,j列数
    PathStack head;
    head.next=NULL;
    PathStack *top;
    top=&head;
    //初始化栈结构
    while(1)
    {
        char c;
        printf("InitMap:I,PrintMap:P,SearchShortestpath:S,Route:R,Quit:Q:");
        scanf("%c",&c);
        getchar();

        if(c=='I'||c=='i')//初始化地图
        {
            printf("Please Enter The Line Number(<=30) And Column Number(<=30):");
            scanf("%d%d",&i,&j);
            getchar();
            while(i>30||j>30)
            {
                printf("Please Enter The Line Number(<=30) And Column Number(<=30):");
                scanf("%d%d",&i,&j);
                getchar();
            }
            MapRand(i,j,maze);
            //以上为创建随机迷宫
        }
        else if(c=='P'||c=='p')//打印地图
            PrintMap(i,j,maze);
        else if(c=='S'||c=='s')
        {
            InitQueue(maze);//初始化队列
            if(ShortestPath_BFS(maze)==1)//如果返回值为1,则说明没有可通路径,退出函数
            continue;
            top=Shortestpath(top,maze);
        }
        else if(c=='R'||c=='r')//打印路径
            PrintPath(top,&head);
        else if(c=='Q'||c=='q')//退出程序
            return 0;
        fflush(stdin);//清空缓冲区
    }
    return 0;
}
运行结果:

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include #include #define N1 9 #define N2 8 #define T N1*N2 #define M 4 char B[N1+1][N2+1]; int count=0; //记录路径条数 typedef struct node1 { int a1; int a2; }find,direct[M+1]; typedef struct { int b1; int b2; int id; }site; typedef struct //顺序栈 { site ht[T]; int top; }Stack; void Push(Stack *s,int a,int b) { s->top++; s->ht[s->top].b1=a; s->ht[s->top].b2=b; } void Gettop(Stack * s,int *a,int *b) { *a=s->ht[s->top].b1; *b=s->ht[s->top].b2; } void create(char *a) //从文件读出迷宫(正确) { int i=0,j=0,p=1; char x; FILE *fp; fp=fopen("in.txt","r"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } x=fgetc(fp); while(x!=EOF) { if(x=='0') { i++; a[i]=x; } if(x=='1') { i++; a[i]=x; } x=fgetc(fp); } printf(" ~~~~~~~生成迷宫~~~~~~~\n"); x=fgetc(fp); while(p<=T) //用二维数组b记录迷宫每个位置是否可行 { for(i=1;i<=N1;i++) for(j=1;j<=N2;j++) { B[i][j]=a[p]; p++; } } printf(" "); printf("■■■■■■■■■■■■\n"); printf(" ■"); printf(" ■\n"); for(i=1;i<=N1;i++) { printf(" "); printf("■ "); for(j=1;jht[s1->top].id=id; B[x][y]='*'; while(s1->top>0) { Gettop(s1,&x,&y); id=s1->ht[s1->top].id; if(x==B1&&y==B2) { count++; fprintf(fp,"%d%c%c",count,':',' '); printf("第 %d 条路径(长度为%d):\n",count,s1->top); s1->ht[s1->top].id=0; for(i=1;itop;i++) { printf("(%d,%d,%d)->",s1->ht[i].b1,s1->ht[i].b2,s1->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s1->ht[i].b1,',',s1->ht[i].b2,',',s1->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c%c%c%c",'\n',' ',' ',' '); if(i%8==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n\n"); if(s1->toptop=s1->top; min=s1->top; for(i=1;itop;i++) s2->ht[i]=s1->ht[i]; } B[x][y]='0'; s1->top--; //退栈(s1->top--) Gettop(s1,&x,&y); id=s1->ht[s1->top].id; } fun=0; while(idht[s1->top].b1; y=s1->ht[s1->top].b2; x=x+p[id].a1; y=y+p[id].a2; if(x==0||y==0||x>N1||y>N2) continue; if(B[x][y]=='0') { fun=1; break; } } if(fun==1) //找到通路 { s1->ht[s1->top].id=id; Push(s1,x,y); B[x][y]='*'; s1->ht[s1->top].id=0; } else { x=s1->ht[s1->top].b1; y=s1->ht[s1->top].b2; B[x][y]='0'; s1->top--; } } if(count==0) printf(" 无路径!\n"); else { printf("\n\n\n "); printf("所有路径已存储在文件%s 中,请去查找!\n\n",filename); } return 1; } void Print(Stack *s2,char filename[]) { int i; FILE *fp; fp=fopen(filename,"a+"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } if(count!=0) { fprintf(fp,"%s","最短路径为:"); fprintf(fp,"%c",'\n'); printf(" "); printf("%s\n","**********最短路径**********\n"); for(i=1;itop;i++) { printf("(%d,%d,%d) ->",s2->ht[i].b1,s2->ht[i].b2,s2->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s2->ht[i].b1,',',s2->ht[i].b2,',',s2->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c",'\n'); if(i%7==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n"); printf("(最短路径长度: %d)\n",s2->top); } } void main() //主函数 { char a[T+1]; //二维数组b记录迷宫的每个位置 char filename1[20],filename2[20]; int x1,x2,y1,y2,k; Stack *s1,*s2; direct f1; f1[1].a1=0; f1[1].a2=1; //判断方向(右) f1[2].a1=1; f1[2].a2=0; //(下) f1[3].a1=0; f1[3].a2=-1; //(左) f1[4].a1=-1; f1[4].a2=0; //(上) s1=(Stack *)malloc(sizeof(Stack)); s2=(Stack *)malloc(sizeof(Stack)); s1->top=0; //指向栈顶(初始化栈) s2->top=0; create(a); printf("\n\n "); printf("请输入入口坐标: "); scanf("%d%d",&x1,&x2); printf(" "); printf("请输入出口坐标: "); scanf("%d%d",&y1,&y2); printf(" "); printf("请输入存储所有路径的文件名:"); scanf("%s",filename1); printf(" "); printf("请输入存储最短路径的文件名:"); scanf("%s",filename2); system("cls"); k=search(x1,x2,y1,y2,s1,s2,f1,filename1); if(k==1) Print(s2,filename2); printf("\n"); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值