发一个迷宫问题(又称电路板问题)的源码
来源:互联网 发布:php 分割二维数组 编辑:程序博客网 时间:2024/03/29 06:38
- #include<iomanip.h>
- #include<fstream.h>
- //=========================================类声明==============================
- template <class T> class queue;
- template <class T>
- class node
- {
- friend class queue<T> ;
- private:
- T data;
- node<T> * next;
- };
- //----------
- template <class T>
- class queue
- {
- public:
- queue(){front=rear=NULL;}
- ~queue();
- void enqueue(T a);
- bool dequeue(T& savedeq);
- bool empty();
-
- private:
- node<T> * front;
- node<T> * rear;
- };
- //----------
- class position
- {
- public:
- int row;
- int col;
- };
- //==============================================类函数声明=================================
- template<class T>
- queue<T>::~queue()
- {
- node<T>* p;
- for(int i=0;front;i++)
- {
- p=front;
- front=front->next;
- delete p;
- }
- }
- //----------
- template<class T>
- bool queue<T>::empty()
- {
- return front==NULL;
- }
- //----------
- template<class T>
- void queue<T>::enqueue(T a)
- {
- node<T> * p=new node<T>;
- p->data=a;
- p->next=NULL;
- if(front==NULL&&rear==NULL)
- {
- rear=front=p;
- }
- else
- {
- rear->next=p;
- rear=p;
- }
- }
- //----------
- template<class T>
- bool queue<T>::dequeue(T& savedeq)
- {
- if(front==NULL&&rear==NULL)
- return 0;
- node<T> * p=front;
- front=front->next;
- savedeq=p->data;
- if(front==NULL)
- rear=NULL;
- delete p;
- return 1;
- }
- //============================================主程序====================================
- void main()
- {
- //-------------------------------------初始化迷宫--------------------------------
-
- cout<<"请在文本中输入迷宫方阵.(障碍物用x表示,可通行路径用0表示满意)每行结尾用'.'标识"<<endl;
- cout<<"结束后按任意键继续:"<<endl;
- char wait;
- cin>>wait;
- lb: ifstream fin1,fin2; //对象声明
- cout<<"输入文件路径:"; //文件路径指定
- char * road=new char [30];
- cin>>road;
- fin1.open(road,ios::in|ios:ut|ios::nocreate); //打开文件
- if(!fin1) //文件不存在处理
- {
- cout<<"无此文件"<<endl;
- fin1.close();
- goto lb;
- }
- char g;
- int han=0,lie=0;
- while(fin1>>g)
- {
- if(g=='.')
- ++han;
- if(han==0)
- lie++;
- }
- fin1.close();
- fin2.open(road);
- int ** p=new int* [han+2];
- for(int i=0;i<=han+1;i++)
- p=new int[lie+2];
-
- for(i=0;i<=han+1;i++)
- {
- p[0]=-1;
- p[lie+1]=-1;
- }
- for(int j=0;j<=lie+1;j++)
- {
- p[0][j]=-1;
- p[han+1][j]=-1;
- }
- for(i=1;i<=han;i++)
- for(j=1;j<=lie+1;j++)
- if(fin2>>g&&g!='.')
- {
- if(g=='0')
- p[j]=0;
- if(g=='x')
- p[j]=-1;
-
- }
- fin2.close();
- cout<<endl;
- cout<<"你输入的迷宫是:"<<endl;
- cout<<endl;
- for(i=1;i<=han;i++)
- {
- for(j=1;j<=lie;j++)
- cout<<setw(3)<<p[j];
- cout<<endl;
- cout<<endl;
- }
- //----------
- int h1,l1,h2,l2; // 入口出口设置
- cout<<"入口的行:";
- cin>>h1;
- cout<<"入口的列:";
- cin>>l1;
- cout<<"出口的行:";
- cin>>h2;
- cout<<"出口的列:";
- cin>>l2;
- position start,finish;
- start.row=h1;
- start.col=l1;
- finish.row=h2;
- finish.col=l2;
- //----------
- position offset[4]; //辅助移动对象
- offset[0].row=0;
- offset[0].col=1;
- offset[1].row=1;
- offset[1].col=0;
- offset[2].row=0;
- offset[2].col=-1;
- offset[3].row=-1;
- offset[3].col=0;
- //----------
- int length; //变量声明
- position here,nb;
- //----------
- here.row=start.row; //寻找出口程序
- here.col=start.col;
- queue<position> assist;
- while(true)
- {
- for(i=0;i<4;i++)
- {
- nb.row=here.row+offset.row;
- nb.col=here.col+offset.col;
- if(p[nb.row][nb.col]==0)
- {
- p[nb.row][nb.col]=p[here.row][here.col]+1;
- if(nb.row==finish.row&&nb.col==finish.col)
- break;
- assist.enqueue(nb);
- }
- }
- if(nb.row==finish.row&&nb.col==finish.col)
- break;
- if(assist.empty())
- {
- cout<<"不存在通往出口的路"<<endl;
- char x;
- cin>>x;
- return;
- }
- assist.dequeue(here);
- }
- //----------
- length=p[finish.row][finish.col]; //回溯入口
- cout<<endl;
- cout<<"出口到入口的长度是"<<length<<endl;
- here.row=finish.row;
- here.col=finish.col;
- for(i=length;i>=2;i--)
- {
- for(j=0;j<4;j++)
- {
- nb.row=here.row+offset[j].row;
- nb.col=here.col+offset[j].col;
- if(p[nb.row][nb.col]==i-1)
- break;
- }
- p[nb.row][nb.col]=-2;
- here.row=nb.row;
- here.col=nb.col;
-
- }
- //-----------
- char ** result=new char* [han+2]; //二维数组转字符型
- for(i=0;i<=han+1;i++)
- result=new char[lie+2];
- for(i=1;i<=han;i++)
- {
- for(j=1;j<=lie;j++)
- {
- if(p[j]==-2)
- result[j]='*';
- else
- if(i==h1&&j==l1)
- result[j]='a';
- else
- if(i==h2&&j==l2)
- result[j]='z';
- else
- if(p[j]==-1)
- result[j]='#';
- else
- result[j]='_';
- }
- }
- //-----------
- ofstream fout; //迷宫输出到文本中
- fout.open(road,ios::app);
- fout<<endl;
- fout<<"以下是迷宫路线:"<<endl;
- fout<<endl;
- for(i=1;i<=han;i++)
- {
- for(j=1;j<=lie;j++)
- fout<<setw(3)<<result[j];
- fout<<endl;
- fout<<endl;
- }
- fout<<"(入口 a 出口 z 路径 * 可通行路径 _ 障碍物 #)"<<endl;
- fout.close();
- cout<<endl;
- cout<<"结果已保存到"<<road<<endl;
- char z;
- cin>>z;
- }
- 发一个迷宫问题(又称电路板问题)的源码
- 电路板问题
- 画电路板中遇到的问题
- 画电路板应注意的问题
- 迷宫问题-(Java源码)
- 电路板排列问题
- 电路板排列问题
- 电路板排列问题
- 发邮件时候遇到的一个问题
- 一个"如何使用示波器安全测试接市电电路板"的问题
- 走迷宫的问题
- 迷宫问题的DFS
- 迷宫问题的求解
- 迷宫问题的反思
- 迷宫的路径问题
- UESTC 853 一个简单的走迷宫问题
- 自己写的一个迷宫问题C语言
- 最小长度电路板排列问题
- 面试
- 【基础拓扑排序】poj 2376/ural 1022
- 发一个c语言写的俄罗斯方块的代码 [
- C++写一个简单的堆栈类
- 汉诺塔问题C++的递归实现 [
- 发一个迷宫问题(又称电路板问题)的源码
- 索引选定步骤[摘]
- wxPython Installation
- goole搜索引擎的工作流程
- SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
- 缺點問答
- 随机数
- [转]每天读一遍,不久你就会变!
- DBNull与Null的区别