鱼C论坛

 找回密码
 立即注册
查看: 2086|回复: 2

迷宫(C语言)

[复制链接]
发表于 2022-5-27 22:13:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
迷宫的入口为左上角的黄色方格,出口为右下角的黄色方格。在迷宫中,只能从一个方格走到相邻的上、下、左、右四个方向之一。



找到一条从起点到终点的迷宫路径,并将路径输出。如果从起点到终点没有路径,则输出NO PASS!

注:所有迷宫的起点为左上角,终点为右下角。

【输入形式】依次输入n行由0和1构成的字符串,每行字符串长度相同,输入空串结束,其中1表示围墙,0表示可行路径。
【输出形式】如果起点到终点有路,则依次输出由L、R、D、U组成的路径字符串;否则输出NO PASS!。
【样例输入】
0111111
0011101
1001101
0011001
1000111
1110000
【样例输出】
DRDDDRRDRRR
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-28 22:28:25 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-28 22:30 编辑
  1. typedef struct POINTINI
  2. {
  3.         int x;
  4.         int y;
  5.         int ispass;
  6. }ptini,*pptini;
  7. int fun(ptini* pt, int fx/*来的方向(上下左右)*/,ptini* start,ptini* end,char* patnstr,int* strlen)
  8. {
  9.         int row=end->x-start->x,col=end->y-start->y;
  10.         row=(row<0?-1*row:row)+1,col=(col<0?-1*col:col)+1;
  11.         if (pt->x==end->x&&pt->y==end->y)
  12.         {
  13.                 return 1;
  14.         }
  15.         if (pt->ispass)return 0;
  16.         if (fx!=3&&(pt->y+1<col)&&fun(pt+1,2,start,end,patnstr,strlen))
  17.         {
  18.                 patnstr[--(*strlen)]='R';
  19.        
  20.                

  21.         }
  22.         else if(fx!=1&&(pt->x+1<row)&&fun(pt+col,0,start,end,patnstr,strlen))
  23.         {
  24.                 patnstr[--(*strlen)]='D';
  25.        
  26.                
  27.         }
  28.         else if(fx!=2&&(pt->y-1>=0)&&fun(pt-1,3,start,end,patnstr,strlen))
  29.         {
  30.                 patnstr[--(*strlen)]='L';
  31.        
  32.                
  33.                
  34.         }
  35.         else if(fx!=0&&(pt->x-1>=0)&&fun(pt-col,1,start,end,patnstr,strlen))
  36.         {
  37.                 patnstr[--(*strlen)]='U';
  38.         }
  39.         else
  40.         {
  41.                 pt->ispass=1;
  42.                 return 0;
  43.         }
  44.         pt->ispass=1;
  45.         return 1;
  46. }
  47. int main()
  48. {

  49.         ptini a[6][7]={0};
  50.         int len=6*7+1;
  51.         char * pathstr=(char*)malloc(6*7+1);
  52.         for (int i = 0; i < 6; i++)
  53.         {
  54.                 for (int j = 0; j < 7; j++)
  55.                 {
  56.                         a[i][j].x=i;
  57.                         a[i][j].y=j;
  58.                         if('1'==getchar())a[i][j].ispass=1;
  59.                         else a[i][j].ispass=0;
  60.                 }
  61.                 getchar();
  62.         }
  63.         a[0][0].ispass=0;
  64.         a[5][6].ispass=0;
  65.         fun((ptini*)a,0,(ptini*)a,&a[5][6],pathstr,&len);
  66.         pathstr[42]=0;
  67.         printf("%s",pathstr+len);
  68.         free(pathstr);
  69.         return 0;

  70. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-18 16:08:30 | 显示全部楼层
本帖最后由 蔚蓝水晶 于 2022-6-18 16:12 编辑
  1. #include <stdio.h>
  2. #define W 100           //迷宫的宽

  3. char De[4]={'U','D','L','R'};           //用来输出路径

  4. int deM(int a[][W],int road[],int count,int h,int w,int choice,int x,int y);         //检测函数,用来指明方向
  5. int mm(int a[][W],int road[],int count,int h,int w,int choice,int x,int y);      //deM的过渡函数,主要用来生成完整路径

  6. int mm(int a[][W],int road[],int count,int h,int w,int choice,int x,int y){
  7.     int n=count+1;

  8.     if(deM(a,road,n,h,w,choice,x,y)){
  9.         road[count]=De[choice];
  10.         return 1;
  11.     }

  12.     else
  13.         return 0;

  14. }


  15. int deM(int a[][W],int road[],int count,int h,int w,int choice,int x,int y){

  16.     if(h==y&&w==x)
  17.         return 1;
  18.    
  19.     else{

  20.         if(y!=0&&choice!=1){
  21.             if(*(*(a+y-1)+x)==0){
  22.                 if(mm(a,road,count,h,w,0,x,y-1))
  23.                     return 1;
  24.             }
  25.         }

  26.         if(y!=h&&choice!=0){
  27.             if(*(*(a+y+1)+x)==0){
  28.                 if(mm(a,road,count,h,w,1,x,y+1))
  29.                     return 1;
  30.             }
  31.         }

  32.         if(x!=0&&choice!=3){
  33.             if(*(*(a+y)+x-1)==0){
  34.                 if(mm(a,road,count,h,w,2,x-1,y))
  35.                     return 1;
  36.             }
  37.         }

  38.         if(x!=w&&choice!=2){
  39.             if(*(*(a+y)+x+1)==0){
  40.                 if(mm(a,road,count,h,w,3,x+1,y))
  41.                     return 1;
  42.             }
  43.         }

  44.         return 0;

  45.     }



  46. }


  47. int main(void){
  48.     int i,j,h,w;

  49.     printf("请输入迷宫的规模:");
  50.     scanf("%d %d",&h,&w);

  51.     getchar();

  52.     int M[h][W],road[h*w+1]={0};

  53.     for(i=0;i<h;i++){
  54.         printf("请输入第%d行迷宫:",i+1);
  55.         for(j=0;j<w;j++){
  56.             scanf("%d",&M[i][j]);
  57.             getchar();
  58.         }

  59.     }

  60.     if(deM(M,road,0,h-1,w-1,-1,0,0)){
  61.         for(i=0;i<h*w;i++)
  62.             printf("%c",road[i]);
  63.     }
  64.     else
  65.         printf("No pass!");
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 04:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表