| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 779 人关注过本帖
标题:迷宫求解出现问题求大神修改
只看楼主 加入收藏
风吹心在动
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-5-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
迷宫求解出现问题求大神修改
#include"stdlib.h"
#include"stdio.h"
#define N 39
#define M 39
int X;  
int maze[N+2][M+2];
struct point
{   
    int row,col,predecessor;
 }queue[512];
int head=0,tail=0;
 void shoudong_maze(int m,int n)
 {  
     int i,j;
     printf("\n\n");
     printf("请按行输入迷宫,0表示通路,1表示障碍:\n\n");
     for(i=0;i<m;i++)  
         for(j=0;j<n;j++)      
             scanf("%d",&maze[i][j]);
 }  
 void zidong_maze(int m,int n)
 {  
     int i,j;   
     printf("\n迷宫生成中……\n\n");
     system("pause");
     for(i=0;i<m;i++)
         for(j=0;j<n;j++)   
             maze[i][j]=rand()%2;
         //由于rand()产生的随机数是从0到RAND_MAX
         //RAND_MAX是定义在stdlib.h中的,其值至少为32767)
         //要产生从X到Y的数,只需要这样写:k=rand()%(Y-X+1)+X;  
 }  
         void print_maze(int m,int n)
         {
             int i,j;  
             printf("\n迷宫生成结果如下:\n\n");
             printf("迷宫入口\n");
             printf("↓");  
             for(i=0;i<m;i++)
             {
                 printf("\n");
                 for(j=0;j<n;j++)   
                 {
                     if(maze[i][j]==0)
                         printf("□");
                     if(maze[i][j]==1)
                         printf("■");
                 }  
             }   
             printf("→迷宫出口\n");
         }
         void result_maze(int m,int n)
         {
          int i,j;
  printf("迷宫通路(用☆表示)如下所示:\n\t");
  for(i=0;i<m;i++)  
  {
      printf("\n");
      for(j=0;j<n;j++)   
      {
          if(maze[i][j]==0||maze[i][j]==2)
              printf("□");  
          if(maze[i][j]==1)
              printf("■");   
          if(maze[i][j]==3)
              printf("☆");  
      }
  }  
         }  
         void enqueue(struct point p)
         {
             queue[tail]=p;
             tail++;
         }  
         struct point dequeue()
         {  
             head++;  
         return queue[head-1];
         }  
         int is_empty(){
             return head==tail;
         }  
         void visit(int row,int col,int maze[41][41])
         {  
             struct point visit_point={row,col,head-1};
             maze[row][col]=2;  
             enqueue(visit_point);
         }  
         int mgpath(int maze[41][41],int m,int n)
         {  
             X=1;
             struct point p={0,0,-1};
             if(maze[p.row][p.col]==1)
             {
                 printf("\n===============================================\n");
                 printf("此迷宫无解\n\n");
                 X=0;
                 return 0;
             }  
             maze[p.row][p.col]=2;
             enqueue(p);  
             while(!is_empty())
             {
                 p=dequeue();  
                 if((p.row==m-1)&&(p.col==n-1))
                     break;
                 if((p.col+1<n)&&(maze[p.row][p.col+1]==0))
                     visit(p.row,p.col+1,maze);  
                 if((p.row+1<m)&&(maze[p.row+1][p.col]==0))
                     visit(p.row+1,p.col,maze);  
                 if((p.col-1>=0)&&(maze[p.row][p.col-1]==0))
                     visit(p.row,p.col-1,maze);  
                 if((p.row-1>=0)&&(maze[p.row-1][p.col]==0))
                     visit(p.row-1,p.col,maze);  
             }   
             if(p.row==m-1&&p.col==n-1)   
             {printf("\n================================================ ==================\n");
             printf("迷宫路径为:\n");
             printf("(%d,%d)\n",p.row,p.col);
             maze[p.row][p.col]=3;
             while(p.predecessor!=-1)  
             {
                 p=queue[p.predecessor];
                 printf("(%d,%d)\n",p.row,p.col);
                 maze[p.row][p.col]=3;  
             }  
             }   
             else  
             {
                 printf("\n=============================================================\n");
                 printf("此迷宫无解!\n\n");
                  X=0;
             }  
             return 0;
         }  
         void main()
         {
             int i,m,n,cycle=0;
             while(cycle!=(-1))
             {  
                 printf("********************************************************************************\n");
                 printf("                              欢迎进入迷宫求解系统\n");
                 printf("                                                  \n");  
                 printf("********************************************************************************\n");  
                 printf("                             手动生成迷宫  请按:1\n");
                 printf("                             自动生成迷宫  请按:2\n");
                 printf("                             退出          请按:3\n\n");
                 printf("********************************************************************************\n");
                 printf("\n");
                 printf("请选择你的操作:\n");
                 scanf("%d",&i);
                 switch(i)  
                 {
                 case 1:
                     printf("\n请输入行数:");
                     scanf("%d",&m);     
                     printf("\n");         
                     printf("请输入列数:");
                     scanf("%d",&n);  
                     while((m<=0||m>39)||(n<=0||n>39))
                     {
                         printf("\n抱歉,你输入的行列数超出预设范围(0-39,0-39),请重新输入:\n\n");
                         printf("请输入行数:");
                         scanf("%d",&m);
                         printf("\n");
                         printf("请输入列数:");
                         scanf("%d",&n);
         }   
                     shoudong_maze(m,n);
                     print_maze(m,n);
                     mgpath(maze,m,n);  
                     if(X!=0)
                         result_maze(m,n);
                     printf("\n\nPressEnterContiue!\n");
                     getchar();
                     while(getchar()!='\n');
                     break;   
                 case 2:
                     printf("\n请输入行数:");
                     scanf("%d",&m);  
                     printf("\n");   
                     printf("请输入列数:");
                     scanf("%d",&n);
                     while((m<=0||m>39)||(n<=0||n>39))  
                     {
                         printf("\n抱歉,你输入的行列数超出预设范围(0-39,0-39),请重新输 入:\n\n");   
                         printf("请输入行数:");
                         scanf("%d",&m);   
                         printf("\n");   
                         printf("请输入列数:");
                         scanf("%d",&n);   
         }        
                     zidong_maze(m,n);
                     print_maze(m,n);  
                     mgpath(maze,m,n);
                     if(X!=0)
                         result_maze(m,n);
                     printf("\n\nPressEnterContiue!\n");
                     getchar();
                     while(getchar()!='\n');
                     break;   
                 case 3:
                     cycle=(-1);
                     break;  
                 default:printf("\n");printf("你的输入有误!\n");
                     printf("\nPress Enter Contiue!\n");
                     getchar();
                     while(getchar()!='\n');break;  
         }
         }
         }
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.c
g:\c语言\迷宫求解\11\1.c(92) : error C2143: syntax error : missing ';' before 'type'
g:\c语言\迷宫求解\11\1.c(93) : error C2065: 'p' : undeclared identifier
g:\c语言\迷宫求解\11\1.c(93) : error C2224: left of '.row' must have struct/union type
g:\c语言\迷宫求解\11\1.c(93) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
搜索更多相关主题的帖子: include 通路 
2017-06-08 08:44
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:20 
代码挺恶心的
回正题,编译失败是因为你用的是过时的编译器
可以这样改

         int mgpath(int maze[41][41],int m,int n)
         {  
             X=1;
             struct point p={0,0,-1};
改为
         int mgpath(int maze[41][41],int m,int n)
         {  
             struct point p={0,0,-1};
             X=1;
2017-06-08 09:15
快速回复:迷宫求解出现问题求大神修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029820 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved