回 帖 发 新 帖 刷新版面

主题:小弟的迷宫问题写了近两天,大家来运行看看,很好玩的,进来欣赏的人一律给分

//==================================迷宫问题(又称电路板问题)===============================
#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::out|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[i]=new int[lie+2];
    
    for(i=0;i<=han+1;i++)
    {
        p[i][0]=-1;
        p[i][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[i][j]=0;
                if(g=='x')
                p[i][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[i][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[i].row;
            nb.col=here.col+offset[i].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[i]=new char[lie+2];
    for(i=1;i<=han;i++)                 
    {
        for(j=1;j<=lie;j++)
        {
            if(p[i][j]==-2)
                result[i][j]='*';
            else
                if(i==h1&&j==l1)
                result[i][j]='a';
                else
                    if(i==h2&&j==l2)
                    result[i][j]='z';
                    else
                        if(p[i][j]==-1)
                            result[i][j]='#';
                        else
                            result[i][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[i][j];
        fout<<endl;
        fout<<endl;
    }
    fout<<"(入口 a 出口 z 路径 * 可通行路径 _ 障碍物 #)"<<endl;
    fout.close();
    cout<<endl;
    cout<<"结果已保存到"<<road<<endl;
    char z;
    cin>>z;
}









    


[em3]

回复列表 (共39个回复)

31 楼

[quote]你用的广度优先搜索吧,也不知道前面的模板函数是不是你写的,既然知道写成类,何不把你这个迷宫模块也写成类封装起来呢,一个main写到底不好看啊。你可以分几个块来写,数据流控制部分,算法部分,用户界面接口部分,这样就不用两天时间写了。[/quote]

很好的建议,推荐楼主采纳。
把算法写成函数
数据结构和问题封装为内。

有些时候算法与问题联系的很紧像是无法分开就像迷宫问题,骑士巡游问题,皇后问题。骑士仔细分析,总能分开的
走迷宫的算法,在别的地方也很有用,独立出来更好

32 楼

[quote]在什么环境下运行阿?[/quote]
vc 6.0 
tc++ 3.0

旧语法当然旧编译

我还没看
根据错误提示稍微改了一下
现在,可以在vc6.0和所有支持当前流行c++标准的编译器中编译成功

//==================================迷宫问题(又称电路板问题)===================
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std; 
//=========================================类声明===============================
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;
}

33 楼


//============================================主程序====================================
int 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::out);                    //打开文件
    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[i]=new int[lie+2];
    
    for(int i=0;i<=han+1;i++)
    {
        p[i][0]=-1;
        p[i][lie+1]=-1;
    }
    for(int j=0;j<=lie+1;j++)
    {
        p[0][j]=-1;
        p[han+1][j]=-1;
    }
    for(int i=1;i<=han;i++)
        for(int j=1;j<=lie+1;j++)
            if(fin2>>g&&g!='.')
            {
                if(g=='0')
                p[i][j]=0;
                if(g=='x')
                p[i][j]=-1;
                
            }
    fin2.close();
    cout<<endl;
    cout<<"你输入的迷宫是:"<<endl;
    cout<<endl;
    for(int i=1;i<=han;i++)
    {
        for(int j=1;j<=lie;j++)
            cout<<setw(3)<<p[i][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;

34 楼

//----------
    here.row=start.row;                                                //寻找出口程序
    here.col=start.col;
    queue<position> assist;
    while(true)
    {
        for(int i=0;i<4;i++)
        {
            nb.row=here.row+offset[i].row;
            nb.col=here.col+offset[i].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 12;
        }
        assist.dequeue(here);
    }
    //----------
    length=p[finish.row][finish.col];                                  //回溯入口
    cout<<endl;
    cout<<"出口到入口的长度是"<<length<<endl;
    here.row=finish.row;
    here.col=finish.col;

    for(int i=length;i>=2;i--)
    {
        for(int 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(int i=0;i<=han+1;i++)
        result[i]=new char[lie+2];
    for(int i=1;i<=han;i++)                 
    {
        for(int j=1;j<=lie;j++)
        {
            if(p[i][j]==-2)
                result[i][j]='*';
            else
                if(i==h1&&j==l1)
                result[i][j]='a';
                else
                    if(i==h2&&j==l2)
                    result[i][j]='z';
                    else
                        if(p[i][j]==-1)
                            result[i][j]='#';
                        else
                            result[i][j]='_';
        }
    }
    //-----------
    ofstream fout;                                                     //迷宫输出到文本中
    fout.open(road,ios::app);
    fout<<endl;
    fout<<"以下是迷宫路线:"<<endl;
    fout<<endl;
    for(int i=1;i<=han;i++)               
    {
        for(int j=1;j<=lie;j++)
            fout<<setw(3)<<result[i][j];
        fout<<endl;
        fout<<endl;
    }
    fout<<"(入口 a 出口 z 路径 * 可通行路径 _ 障碍物 #)"<<endl; 
    fout.close();
    cout<<endl;
    cout<<"结果已保存到"<<road<<endl;
    char z;
    cin>>z;
    
    return 0;
}

35 楼

顶楼主.十二楼.十五楼
有些东西自己感兴趣.两天算啥.本人刚看到门槛.愿意请指点一二...

36 楼

楼主那么强悍~ 现在正在写迷宫问题的呢~

37 楼

队列写的  我的课设还要求用栈写

38 楼


惭愧啊!我

39 楼


由于时间的关系,我先备了一份,回去研究,下次来的时候,会给你留言哦!不过,初略看了一下,觉得你写得还是不错的,看好你哦!继续吧,努力学吧![em20]

我来回复

您尚未登录,请登录后再回复。点此登录或注册