发一个迷宫问题(又称电路板问题)的源码

来源:互联网 发布:php 分割二维数组 编辑:程序博客网 时间:2024/03/29 06:38
 
  1. #include<iomanip.h>
  2. #include<fstream.h>
  3. //=========================================类声明==============================
  4. template <class T> class queue;
  5. template <class T>
  6. class node
  7. {
  8.     friend class queue<T> ;
  9.     private:
  10.         T data;
  11.         node<T>  * next;
  12. };
  13. //----------
  14. template <class T>
  15. class queue
  16. {
  17.     public:
  18.         queue(){front=rear=NULL;}
  19.         ~queue();
  20.         void enqueue(T a);
  21.         bool dequeue(T& savedeq);
  22.         bool empty();
  23.         
  24.     private:
  25.         node<T> * front;
  26.         node<T> * rear;
  27. };
  28. //----------
  29. class position
  30. {
  31. public:
  32.     int row;
  33.     int col;
  34. };
  35. //==============================================类函数声明=================================
  36. template<class T>
  37. queue<T>::~queue()
  38. {
  39.     node<T>* p;
  40.     for(int i=0;front;i++)
  41.     {
  42.         p=front;
  43.         front=front->next;
  44.         delete p;
  45.     }
  46. }
  47. //----------
  48. template<class T>
  49. bool queue<T>::empty()
  50. {
  51.     return front==NULL;
  52. }
  53. //----------
  54. template<class T>
  55. void queue<T>::enqueue(T a)
  56. {
  57.     node<T> * p=new node<T>;
  58.     p->data=a;
  59.     p->next=NULL;
  60.     if(front==NULL&&rear==NULL)
  61.     {
  62.         rear=front=p;
  63.     }
  64.     else
  65.     {
  66.         rear->next=p;
  67.         rear=p;
  68.     }
  69. }
  70. //----------
  71. template<class T>
  72. bool queue<T>::dequeue(T& savedeq)
  73. {
  74.     if(front==NULL&&rear==NULL)
  75.         return 0;
  76.     node<T> * p=front;
  77.     front=front->next;
  78.     savedeq=p->data;
  79.     if(front==NULL)
  80.         rear=NULL;
  81.     delete p;
  82.     return 1;
  83. }
  84. //============================================主程序====================================
  85. void main()
  86. {
  87.     //-------------------------------------初始化迷宫--------------------------------
  88.    
  89.     cout<<"请在文本中输入迷宫方阵.(障碍物用x表示,可通行路径用0表示满意)每行结尾用'.'标识"<<endl;
  90.     cout<<"结束后按任意键继续:"<<endl;
  91.     char wait;
  92.     cin>>wait;
  93. lb: ifstream fin1,fin2;                                                //对象声明
  94.     cout<<"输入文件路径:";                                             //文件路径指定
  95.     char * road=new char [30];
  96.     cin>>road;
  97.     fin1.open(road,ios::in|ios:ut|ios::nocreate);                    //打开文件
  98.     if(!fin1)                                                          //文件不存在处理
  99.     {
  100.         cout<<"无此文件"<<endl;
  101.         fin1.close();
  102.         goto lb;
  103.     }
  104.     char g;
  105.     int han=0,lie=0;
  106.     while(fin1>>g)
  107.     {
  108.         if(g=='.')
  109.             ++han;
  110.         if(han==0)
  111.             lie++;
  112.     }
  113.     fin1.close();
  114.     fin2.open(road);
  115.     int ** p=new int* [han+2];
  116.     for(int i=0;i<=han+1;i++)
  117.         p=new int[lie+2];
  118.    
  119.     for(i=0;i<=han+1;i++)
  120.     {
  121.         p[0]=-1;
  122.         p[lie+1]=-1;
  123.     }
  124.     for(int j=0;j<=lie+1;j++)
  125.     {
  126.         p[0][j]=-1;
  127.         p[han+1][j]=-1;
  128.     }
  129.     for(i=1;i<=han;i++)
  130.         for(j=1;j<=lie+1;j++)
  131.             if(fin2>>g&&g!='.')
  132.             {
  133.                 if(g=='0')
  134.                 p[j]=0;
  135.                 if(g=='x')
  136.                 p[j]=-1;
  137.                
  138.             }
  139.     fin2.close();
  140.     cout<<endl;
  141.     cout<<"你输入的迷宫是:"<<endl;
  142.     cout<<endl;
  143.     for(i=1;i<=han;i++)
  144.     {
  145.         for(j=1;j<=lie;j++)
  146.             cout<<setw(3)<<p[j];
  147.         cout<<endl;
  148.         cout<<endl;
  149.     }
  150.     //----------
  151.     int h1,l1,h2,l2;                                                   // 入口出口设置
  152.     cout<<"入口的行:";
  153.     cin>>h1;
  154.     cout<<"入口的列:";
  155.     cin>>l1;
  156.     cout<<"出口的行:";
  157.     cin>>h2;
  158.     cout<<"出口的列:";
  159.     cin>>l2;
  160.     position start,finish;
  161.     start.row=h1;
  162.     start.col=l1;
  163.     finish.row=h2;
  164.     finish.col=l2;
  165.     //----------
  166.     position offset[4];                                                //辅助移动对象
  167.     offset[0].row=0;
  168.     offset[0].col=1;
  169.     offset[1].row=1;
  170.     offset[1].col=0;
  171.     offset[2].row=0;
  172.     offset[2].col=-1;
  173.     offset[3].row=-1;
  174.     offset[3].col=0;
  175.     //----------
  176.     int length;                                                        //变量声明
  177.     position here,nb;
  178.     //----------
  179.     here.row=start.row;                                                //寻找出口程序
  180.     here.col=start.col;
  181.     queue<position> assist;
  182.     while(true)
  183.     {
  184.         for(i=0;i<4;i++)
  185.         {
  186.             nb.row=here.row+offset.row;
  187.             nb.col=here.col+offset.col;
  188.             if(p[nb.row][nb.col]==0)
  189.             {
  190.                 p[nb.row][nb.col]=p[here.row][here.col]+1;
  191.                 if(nb.row==finish.row&&nb.col==finish.col)
  192.                     break;
  193.                 assist.enqueue(nb);
  194.             }
  195.         }
  196.         if(nb.row==finish.row&&nb.col==finish.col)
  197.         break;
  198.         if(assist.empty())
  199.         {
  200.             cout<<"不存在通往出口的路"<<endl;
  201.             char x;
  202.             cin>>x;
  203.             return;
  204.         }
  205.         assist.dequeue(here);
  206.     }
  207.     //----------
  208.     length=p[finish.row][finish.col];                                  //回溯入口
  209.     cout<<endl;
  210.     cout<<"出口到入口的长度是"<<length<<endl;
  211.     here.row=finish.row;
  212.     here.col=finish.col;

  213.     for(i=length;i>=2;i--)
  214.     {
  215.         for(j=0;j<4;j++)
  216.         {
  217.             nb.row=here.row+offset[j].row;
  218.             nb.col=here.col+offset[j].col;
  219.             if(p[nb.row][nb.col]==i-1)
  220.                 break;
  221.         }
  222.         p[nb.row][nb.col]=-2;
  223.         here.row=nb.row;
  224.         here.col=nb.col;
  225.    
  226.     }
  227.     //-----------
  228.     char ** result=new char* [han+2];                                  //二维数组转字符型
  229.     for(i=0;i<=han+1;i++)
  230.         result=new char[lie+2];
  231.     for(i=1;i<=han;i++)                 
  232.     {
  233.         for(j=1;j<=lie;j++)
  234.         {
  235.             if(p[j]==-2)
  236.                 result[j]='*';
  237.             else
  238.                 if(i==h1&&j==l1)
  239.                 result[j]='a';
  240.                 else
  241.                     if(i==h2&&j==l2)
  242.                     result[j]='z';
  243.                     else
  244.                         if(p[j]==-1)
  245.                             result[j]='#';
  246.                         else
  247.                             result[j]='_';
  248.         }
  249.     }
  250.     //-----------
  251.     ofstream fout;                                                     //迷宫输出到文本中
  252.     fout.open(road,ios::app);
  253.     fout<<endl;
  254.     fout<<"以下是迷宫路线:"<<endl;
  255.     fout<<endl;
  256.     for(i=1;i<=han;i++)               
  257.     {
  258.         for(j=1;j<=lie;j++)
  259.             fout<<setw(3)<<result[j];
  260.         fout<<endl;
  261.         fout<<endl;
  262.     }
  263.     fout<<"(入口 a 出口 z 路径 * 可通行路径 _ 障碍物 #)"<<endl;
  264.     fout.close();
  265.     cout<<endl;
  266.     cout<<"结果已保存到"<<road<<endl;
  267.     char z;
  268.     cin>>z;
  269. }
原创粉丝点击