C语言迷宫(经典题目)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

void main() { try1(0,0); } 最深层次问题:找发现食物的最短路。 #include "stdio.h" #include "stdlib.h" #include<string.h> #define N 8 int path[64],k=0; struct ginfo{ int up,down,left,right; int food,visit; } a[N][N]={ {0,0,0,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {0,0,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 1,1,0,1,0,0, 0,1,1,0,0,0, 1,1,0,0,0,0}, {0,1,0,0,0,0, 0,1,0,0,0,0, 0,1,0,0,1,0, 1,1,0,0,0,0, 1,1,0,0,0,0, 1,1,0,0,0,0}, {1,1,0,0,0,0, 1,1,0,1,0,0, 1,1,1,0,0,0, 1,0,1,0,0,0, 1,0,0,0,1,0, 1,1,0,0,0,0}, {1,1,0,1,0,0, 1,1,1,0,0,0, 1,1,0,0,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,1,1,0,0,0}, {1,0,0,0,0,0, 1,0,0,0,0,0, 1,1,0,1,0,0, 0,1,1,1wk.baidu.com0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}, {0,1,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 1,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {1,0,0,1,1,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}}; typedef struct pathNode{ int x,y,k; int *r;
int food,visit; } a[N][N]={ {0,0,0,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {0,0,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 1,1,0,1,0,0, 0,1,1,0,0,0, 1,1,0,0,0,0}, {0,1,0,0,0,0, 0,1,0,0,0,0, 0,1,0,0,1,0, 1,1,0,0,0,0, 1,1,0,0,0,0, 1,1,0,0,0,0}, {1,1,0,0,0,0, 1,1,0,1,0,0, 1,1,1,0,0,0, 1,0,1,0,0,0, 1,0,0,0,1,0, 1,1,0,0,0,0}, {1,1,0,1,0,0, 1,1,1,0,0,0, 1,1,0,0,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,1,1,0,0,0}, {1,0,0,0,0,0, 1,0,0,0,0,0, 1,1,0,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}, {0,1,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 1,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {1,0,0,1,1,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}}; void print() { int i,x=0,y=0; printf("\n0,0 -> "); for(i=1;i<=k;i++) { switch(path[i]) { case 1 :x--;break; case 2 :x++;break; case 3 :y--;break; case 4 :y++;break; } if(i<k) printf("%d,%d -> ",x,y); else printf("%d,%d food!\n",x,y); } } void try1(int x,int y)
1,1,1,0,0,0, 1,1,0,0,0,0, 1,0,0,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,1,1,0,0,0}, {1,0,0,0,0,0, 1,0,0,0,0,0, 1,1,0,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}, {0,1,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 0,1,1,1,0,0, 0,1,1,1,0,0, 1,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {1,0,0,1,1,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0, 1,0,0,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 1,0,1,0,0,0}}; void print() { int i,x=0,y=0; printf("0,0->"); for(i=1;i<=k;i++) { switch(path[i]) { case 1 :x--;break; case 2 :x++;break; case 3 :y--;break; case 4 :y++;break; } if(i<k) printf("%d,%d->",x,y); else printf("%d,%d found!\n",x,y); } } void try1(int x,int y) { a[x][y].visit=1; if(a[x][y].food){ a[x][y].food=0;print();} k++; if(a[x][y].up) { path[k]=1;//方向为1 x--;a[x][y].down=0; if(!a[x][y].visit)try1(x,y); a[x][y].down=1;x++; }
在上面的迷宫中只有一个入口,有▲地方是有食物的,现在一只老 鼠从入口处进来,要求能找到所有的食物并且打印找到食物的路径。 (1)图信息: #define N 8 struct ginfo{ int up,down,left,right; int food,visit;; } a[N][N]={…}; (2)记录路径path[N*N]; (3)找到食物a[x][y].food==1 (4)代码 #include "stdio.h" #include<string.h> #define N 8 int path[64],k=0; struct ginfo{ int up,down,left,right; int food,visit; } a[N][N]={ {0,0,0,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 0,0,1,1,0,0, 0,1,1,0,0,0}, {0,0,0,1,0,0, 0,0,1,1,0,0, 1,0,1,1,0,0, 0,1,1,0,0,0, 0,1,0,0,0,0, 1,1,0,1,0,0, 0,1,1,0,0,0, 1,1,0,0,0,0}, {0,1,0,0,0,0, 0,1,0,0,0,0, 0,1,0,0,1,0, 1,1,0,0,0,0, 1,1,0,0,0,0, 1,1,0,0,0,0, 1,1,0,0,0,0, 1,1,0,0,0,0}, {1,1,0,0,0,0, 1,1,0,1,0,0, 1,1,1,0,0,0, 1,1,0,1,0,0, 1,0,1,1,0,0, 1,0,1,0,0,0, 1,0,0,0,1,0, 1,1,0,0,0,0}, {1,1,0,1,0,0,
0,0,1,1,0,0, 0,1,1,0,0,0, 1,1,0,0,0,0, 1,1,0,1,0,0, 1,0,0,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 1,0,1,0,0,0,
0,0,1,1,0,0, 0,1,0,0,0,0, 1,1,0,0,0,0, 1,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 1,0,0,1,0,0,
0,0,1,1,0,0, 0,1,1,0,0,0, 1,1,0,0,0,0, 1,1,0,1,0,0, 1,0,0,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 1,0,1,0,0,0,
0,0,1,1,0,0, 0,1,0,0,0,0, 1,1,0,0,0,0, 1,0,1,1,0,0, 0,0,1,1,0,0, 0,0,1,1,0,0, 0,1,1,1,0,0, 1,0,0,1,0,0,
{ a[x][y].visit=1; if(a[x][y].food && !f[x][y]){ f[x][y]=k;print();}//a[x][y].food=0; if(a[x][y].food && f[x][y]>k){f[x][y]=k;printf("\nFound a shortest one "); print();} k++; if(a[x][y].up) { path[k]=1;//方向为1 x--;a[x][y].down=0; if(!a[x][y].visit)try1(x,y); a[x][y].down=1;x++; } if(a[x][y].down) { path[k]=2;//方向为2 x++;a[x][y].up=0; if(!a[x][y].visit)try1(x,y); a[x][y].up=1;x--; } if(a[x][y].left) { path[k]=3;//方向为3 y--;a[x][y].right=0; if(!a[x][y].visit)try1(x,y); a[x][y].right=1;y++; } if(a[x][y].right) { path[k]=4;//方向为4 y++;a[x][y].left=0; if(!a[x][y].visit)try1(x,y); a[x][y].left=1;y--; } a[x][y].visit=0;k--; }
struct pathNode * next; }foodRoad; foodRoad * head; foodRoad * searchPath(int x,int y) { foodRoad * p=head; while(p) { if(p->x == x && p->y == y) return p; p=p->next; } return 0; } void createHead() { head=(foodRoad *)malloc(sizeof(foodRoad)); head->next=0; } void createPathNode(int x,int y,int k) { int i,*pr; foodRoad *p; p=searchPath(x,y); if(p==0) { foodRoad *pf; pf=(foodRoad *)malloc(sizeof(foodRoad)); pf->x=x;pf->y=y;pf->k=k; pr=(int *)malloc(sizeof(int)*(k+1)); pf->r=pr; for(i=1;i<=k;i++) pr[i]=path[i]; pf->next=0; p=head; while(p->next)
if(a[x][y].down) { path[k]=2;//方向为2 x++;a[x][y].up=0; if(!a[x][y].visit)try1(x,y); a[x][y].up=1;x--; } if(a[x][y].left) { path[k]=3;//方向为3 y--;a[x][y].right=0; if(!a[x][y].visit)try1(x,y); a[x][y].right=1;y++; } if(a[x][y].right) { path[k]=4;//方向为4 y++;a[x][y].left=0; if(!a[x][y].visit)try1(x,y); a[x][y].left=1;y--; } a[x][y].visit=0;k--; } void main() { try1(0,0); } 深层次问题:找发现食物的更短路。 #include "stdio.h" #include<string.h> #define N 8 int path[64],k=0; int f[N][N]={0}; struct ginfo{ int up,down,left,right;
相关文档
最新文档