迷宫的两种解法,第一种求出了全部路径(C语言实现)

题目:

4. 读取file4.txt 文件中的字符数据,该文件中的数据用来描述一个正方形的迷宫,“# 表示可到达的地方,“—” 表示 可以到达的地方, 迷宫的 入口 出口都 在正方形的某个边上,要求找到 入口 到出口的 所有 路径(不走重复的路),并将每一种路径显示出来,显示方法 “—”改为“+”,

并将所有解保存到 file4_answer.txt

 

例如:file.txt中存放的 字符数据为:

# - # # # #
# - - - # #
- - # - - #
# - - - - #
# - # - - #
# # # - # #

 

则输出结果:


# + # # # #
# + + + # #
-  -  # + + #
# -  -  - + #
# -  # + + #
# # # + # #

 

# + # # # #
# + + + # #
-  - # + + #
#  - - + + #
#  - # + - #
# # # + # #


# + # # # #
# + + + # #
-   - # + - #
#  - - + + #
# - # + + #
# # # + # #


# + # # # #
# + + + # #
-   - # + - #
#   - - + - #
#  - # + - #
# # # + # #


# + # # # #
# +  - - # #
-  + # - - #
# + + + + #
# - # + + #
# # # + # #


# + # # # #
# + - - # #
-  + # - - #
# + + + - #
# - # + - #
# # # + # #


# + # # # #
#  + - - # #
- + # + + #
# + + + + #
#  - # + + #
# # # + # #

 

   

 

第一种解法:用栈,主要是回溯法,深度优先搜索路径

 

#include<stdio.h>
#include<windows.h>

char maze_char[20][20];//存储从文件中读取的'-''#'字符,构成迷宫
int m,n;
int direct[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上四个方向
int remark[100][2];
int step=1;
int end_i,end_j;

//判断是否可以走
int can_go(int i,int j)
{
 int k;
 if(maze_char[i][j]=='#')
  return 0;
 for(k=0;k<step;k++){//判断此步是否已在路径中,在的话,返回0,否则构成回路
  if(i==remark[k][0]&&j==remark[k][1])
   return 0;
 }
 return 1;
}
//写入文件
void write()
{
 FILE *fp;
 int k,i,j;

 for(k=0;k<step;k++){//把路径上的-号变成+号
  maze_char[remark[k][0]][remark[k][1]]='+';
 }
 //打印路径
 puts("--迷宫路径(以第一行第一个-号为入口,最后一行第一个-号为出口)----");
 for(i=1;i<m-1;i++){
  for(j=1;j<n-1;j++)
   printf("%c  ",maze_char[i][j]);
  printf("\n");
 }

    if((fp=fopen("document/file4_answer.txt","a"))==NULL)
 {
  printf("can not open the file!!!\n");
  exit(0);
 }  
 fputs("\n***********************************************\n",fp);
 for(i=1;i<m-1;i++){
  for(j=1;j<n-1;j++)
  {
            fputc(maze_char[i][j],fp);
   fputc(' ',fp);
  }
  fputc('\n',fp);
 }
 if(fclose(fp))
 {
  printf("can not close the file!!!\n");
  exit(0);
 }
 //写完文件后,把路径的+号变成-号
 for(k=0;k<step;k++){
  maze_char[remark[k][0]][remark[k][1]]='-';
 }
}
//搜索路径
void search(int i,int j)
{
 int k=0;
 if(i==end_i&&j==end_j){//找到出口
         write();
   return;
 }
 for(k=0;k<4;k++){
  if(can_go(i+direct[k][0],j+direct[k][1])){
   remark[step][0]=i+direct[k][0];
   remark[step][1]=j+direct[k][1];
   step++;
   search(i+direct[k][0],j+direct[k][1]);
   step--;//search完四个方向后,返回上一个路径
  }
 }
}

void main()
{
 char ch;
 int i=1,j=1;//m和n表示m*n维的迷宫(为了便宜判断,以m*n是围上一层墙的迷宫)
 FILE *fp; 

 if((fp=fopen("document/file4.txt","r"))==NULL)
 {
  printf("can not open the file!!!\n");
  exit(0);
 }

    //从文件中读取迷宫
 while((ch=fgetc(fp))!=EOF)
 {
  if(ch=='\n')
  {
   j=1;
   i++;
  }
  else if(ch!=' ')//丢弃空格
  {
   if(i==1&&ch=='-')//第一行的第一个'-'作为入口
   {
    remark[0][0]=i;
    remark[0][1]=j;
   }
            maze_char[i][j]=ch;
   j++;
  }
 }
 m=i+2;
 n=j+1;
 for(j=1;j<n-1;j++)
  if(maze_char[m-2][j]=='-'){//把最后一行的第一个'-'作为出口
   end_i=m-2;
   end_j=j;
   break;
  }
 //给迷宫围上一层墙
    for(i=0;i<m;i++){
       maze_char[0][i]='#';
       maze_char[m-1][i]='#';
 }
    for(j=0;j<n;j++){
       maze_char[j][0]='#';
       maze_char[j][n-1]='#';
 }
    puts("----迷宫----");
 for(i=1;i<m-1;i++){
  for(j=1;j<n-1;j++)
   printf("%c  ",maze_char[i][j]);
  printf("\n");
 }
 search(remark[0][0],remark[0][1]);

 

 }

 

第二种解法: 用队列,广度优先搜索

 

#include<stdio.h>
#include<windows.h>
typedef struct node
{
 int i,j,pre;
}Node;
Node remark[100];//记录每一个位置所能到达的位置
int count=1;//记录remark中元素的个数
char maze_char[20][20];//存储从文件中读取的'-''#'字符,构成迷宫
int direct[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//右下左上四个方向

void search(int front)
{
 int k;
 int i=remark[front].i;
 int j=remark[front].j;
 if(front>=count)
  return ;
 for(k=0;k<4;k++)
 {

  if(maze_char[i+direct[k][0]][j+direct[k][1]]=='-'&&maze_char[i+direct[k][0]][j+direct[k][1]]!='*'){
   if(remark[remark[front].pre].i==i+direct[k][0]&&remark[remark[front].pre].j==j+direct[k][1])
   {
   }else{
       remark[count].i=i+direct[k][0];
        remark[count].j=j+direct[k][1];
       remark[count].pre=front;
                maze_char[i+direct[k][0]][j+direct[k][1]]='*';
       count++;
   }
  }
 }
 search(front+1);
}

void main()
{
 char ch;
 int i=1,j=1,m=0,n=0;//m和n表示m*n维的迷宫(为了便宜判断,以m*n是围上一层墙的迷宫)
 int end_i,end_j;
 FILE *fp;
 if((fp=fopen("document/file4.txt","r"))==NULL)
 {
  printf("can not open the file!!!\n");
  exit(0);
 }

    //从文件中读取迷宫
 while((ch=fgetc(fp))!=EOF)
 {
  if(ch=='\n')
  {
   j=1;
   i++;
  }
  else if(ch!=' ')//丢弃空格
  {
   if(i==1&&ch=='-')//第一行的第一个'-'作为入口
   {
    remark[0].i=i;
    remark[0].j=j;
    remark[0].pre=-1;
   }
            maze_char[i][j]=ch;
   j++;
  }
 }
 m=i+2;
 n=j+1;
 for(j=1;j<n-1;j++)
  if(maze_char[m-2][j]=='-'){
   end_i=m-2;
   end_j=j;
  }
 //给迷宫围上一层墙
    for(i=0;i<m;i++){
       maze_char[0][i]='#';
       maze_char[m-1][i]='#';
 }
    for(j=0;j<n;j++){
       maze_char[j][0]='#';
       maze_char[j][n-1]='#';
 }
   
 for(i=0;i<m;i++){
  for(j=0;j<n;j++)
   printf("%c  ",maze_char[i][j]);
  printf("\n");
 }
 search(0);

    for(i=0;i<count;i++){
  if(remark[i].i==end_i&&remark[i].j==end_j){
   j=i;
   while(remark[j].pre>=0){
    maze_char[remark[j].i][remark[j].j]='+';
    j=remark[j].pre;
   }
  }
  else
   maze_char[remark[i].i][remark[i].j]='-';
 }
 maze_char[remark[0].i][remark[0].j]='+';
    printf("***************************************************\n");
 for(i=0;i<m;i++){
  for(j=0;j<n;j++)
   printf("%c  ",maze_char[i][j]);
  printf("\n");
 }
    //把改变后的maze_char写到文件file4_answer.txt中
    if((fp=fopen("document/file4_answer.txt","w"))==NULL)
 {
  printf("can not open the file!!!\n");
  exit(0);
 }  
 for(i=1;i<m-1;i++){
  for(j=1;j<n-1;j++)
  {
            fputc(maze_char[i][j],fp);
   fputc(' ',fp);
  }
  fputc('\n',fp);
 }
 if(fclose(fp))
 {
  printf("can not close the file!!!\n");
  exit(0);
 }
 }

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#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"); }
问题描述: 以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫求出从入口(0,0)到出口(m-1,n-1)的通路通路总数,或得出没有通路的结论。例如下图, 0(入口) 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0(出口) 从入口到出口有6条不同的通路。 而下图: 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 从入口到出口则没有通路。 算法设计: 给定一个m*n的长方阵表示迷宫,设计算法输出入口到出口的通路通路总数,或得出没有通路的结论。 算法提示: 和皇后问题与分书问题类似。可以用二维数组存储迷宫数据,对于迷宫中任一位置,均可约定有东、南、西、北四个方向可通。从当前位置a(用(x,y)表示一个位置,假定它是以向右的x轴和向下的y轴组成的平面上的一个点)出发依次尝试四个方向是否有路,若某个方向的位置b可通,则按照同样的方法继续从b出发寻找。若到达出口,则找到一条通路。 数据输入: 由文件input.txt 提供输入数据。第一行是m和n的值,空格分隔,其后共m行。每行有n个数字,数和数之间用空格分隔。 结果输出: 将计算出的所有从入口到出口的通路输出到文件output.txt 中。若没有通路,则将0写入文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值