資源簡介
迷宮求解的源碼
以一個 m*n 的長方陣表示迷宮,0 和 1 分別表示迷宮中的通路和障礙。設計一個程序,對任意設 定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。
代碼片段和文件信息
#include
#include
#include
int?imag[20][20];
int?vis[20][20];
//存儲走過的路徑
int?dir[4][2]={{01}{10}{0-1}{-10}};
int?mn;
int?sxsyexey;
//迷宮起點和終點
typedef?struct?Node{
????int?xydirection;
????}ElementType;
typedef?struct?SNode{
????ElementType?data;
????SNode*next;}SNode*linkstack;
linkstack?path;
void?initlinkstack(linkstack?&L){
?????L?=?(SNode*)malloc(sizeof(SNode));
?????L->next=NULL;
?????}
void?Push(linkstack&LElementType?e){
?????SNode*?p?=?(SNode*)malloc(sizeof(SNode));
?????p->data=e;
?????p->next=L->next;
?????L->next=p;
?????}
void?pop(linkstack&L){
????if(L->next==NULL)
????????printf(“The?linkstack?is?empty\n“);
????else{
????????SNode*p;
????????p=L->next;
????????L->next=p->next;
????????free(p);
????}
}
bool?emptylinkStack(linkstack?L){
????if(L->next==NULL)
????????return?true;
????else
????????return?false;
}
ElementType?GetTop(linkstack?L){
????if(!emptylinkStack(L)){
????????ElementType?e;
????????SNode?*p=L;
????????p=L->next;
????????e=p->data;
????????return?e;
????}
????else{
????????printf(“The?linkstack?is?empty\n“);
????}
}
void?init(){
????printf(“請輸入迷宮的行數和列數\n“);
????scanf(“%d?%d“&n?&m);
????printf(“請輸入迷宮\n“);
????int?ij;
????for(i=1;i<=n;i++)
????????for(j=1;j<=m;j++)
????????????scanf(“%d“&imag[i][j]);
}
void?Putin(){
????int?s1s2e1e2;
????printf(“請輸入起點的橫縱坐標\n“);
????scanf(“%d?%d“&s1?&s2);
????if(s1<1||s1>n||s2<1||s2>m)
????{
????????printf(“輸入有誤請重新輸入\n“);
????????scanf(“%d?%d“&s1?&s2);
評論
共有 條評論