C语言 用双向链表写迷宫问题
#include#include #include #define N 10 typedef struct PATH { int m;//地图数组的行下标 int n;//地图数组的列下标 PATH *next; PATH *prev; }PA; PA *add(PA *head,int i,int j)//增加一个节点 记录AI的轨迹 { PA *p,*pr=head; p=(PA *)malloc(sizeof(PA)); if(p == NULL) { printf(“Failed!n”); exit(0); } if(head == NULL) { head=p; } else { while(pr->next!=NULL) { pr=pr->next; } pr->next=p; p->prev=pr; p->next=NULL; } p->m=i; p->n=j; return head; } PA *dl(PA *head,int *i,int *j)//碰壁 AI开始往回走 删除原先已建立节点 另寻他路 { PA *pr=head,*p=NULL; while(pr->next!=NULL) { pr=pr->next; } p=pr; pr=pr->prev; free( p ); pr->next=NULL; *i=pr->m; *j=pr->n; return head; } void output(int output[N][N]) { for(int i=0;i m=i; head->n=j; head->prev=NULL; head->next=NULL; op[i][j]=5; output(op); while(maze[i+1][j]!=3 && maze[i-1][j]!=3 && maze[i][j+1]!=3 && maze[i][j-1]!=3)//开始进行走迷宫 直到走到出口为止 { if(maze[i][j+1]==0)//右移 { op[i][j]=0; j++; add(head,i,j); maze[i][j]=1; op[i][j]=5; Sleep(600); system(“cls”); output(op); } else if(maze[i+1][j]==0)//下移 { op[i][j]=0; i++; add(head,i,j); maze[i][j]=1; op[i][j]=5; Sleep(600); system(“cls”); output(op); } else if(maze[i][j-1]==0)//左移 { op[i][j]=0; j–; add(head,i,j); maze[i][j]=1; op[i][j]=5; Sleep(600); system(“cls”); output(op); } else if(maze[i-1][j]==0)//上移 { op[i][j]=0; i–; add(head,i,j); maze[i][j]=1; op[i][j]=5; Sleep(600); system(“cls”); output(op); } else if(maze[i+1][j]==1 && maze[i-1][j]==1 && maze[i][j+1]==1 && maze[i][j-1]==1)//回退另寻他路 { op[i][j]=0; dl(head,&i,&j); op[i][j]=5; Sleep(600); system(“cls”); output(op); } else if(maze[i][j]==2) { printf(“Game over : (n”); return 0; } } printf(“You did it! : )n”); return 0; } ———————————————— 版权声明:本文为CSDN博主「Winddo.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Eenie_meenie_17/article/details/110291006



