QT 两百行代码教你GUI实现走迷宫

QT 两百行代码教你GUI实现走迷宫,第1张

肝了六个小时,才真正实现了这个深搜走迷宫的图形界面。


真的深感不易,快要吐了都。


皇天不负有心人,终于鼓捣出来了。


最主要的思路:基于迷宫基础问题,实现从左上角走到右下角。


采用深度优先搜索的方式。


只展示了最优路径。


效果展示:

很low,但是成就感还是满满滴。


特别适合新手同学,能很好的学习容器的遍历,绘图事件、定时器。


代码放在下面了,需要的自取。


注意:

1、男人头像和旗子都是在阿里巴巴矢量图标库里找的,大小为64*64

2、txt的格式是,前两个为行、列。


然后是m数组里的值,可以参考下面的

8 8 
0 1 0 0 0 0 0 0
0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
1 1 1 1 0 1 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 1
1 1 1 1 0 1 1 0
0 0 0 1 0 0 0 0

node.h
#ifndef NODE_H
#define NODE_H


class Node
{
public:
    Node();
    Node(int xx,int yy);
    int x,y;
};

#endif // NODE_H
widget.h 
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include "node.h"
#include 
#include 
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    int row,col;//迷宫的大小
    int posX,posY;
    QVectorv;
    int vis[505][505];
    int m[505][505];
    int fx[4]={0,1,0,-1};
    int fy[4]={1,0,-1,0};
    int flag = 0;
    int flag1 = 0;//判断有无路径
    int point;
    void Init();
    void start();
    void DFS(int x,int y,QVectorq);//深度优先搜索
    int sum;//记录步数
    int nowsum;//每次到达终点的步数
    void paintEvent(QPaintEvent *event);
    void paintBackgroud();
    void paintMaze(int x,int y);
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

node.cpp
#include "node.h"

Node::Node()
{

}

Node::Node(int xx, int yy)
{
    this->x = xx;
    this->y = yy;
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("迷宫");
    this->resize(1050,1050);
    this->setMinimumSize(1050,1050);
    this->setMaximumSize(1050,1050);
    Init();
    start();
    QTimer *timer = new QTimer(this);
    timer->start(500);
    connect(timer,&QTimer::timeout,[=](){
       if(v.isEmpty())
       {
           timer->stop();
           return;
       }
       Node t1 = *v.begin();
       v.pop_front();
       posX = t1.x;
       posY = t1.y;
       update();
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::Init()
{
    QFile file("D:\\QT\\maze_01\\qt.txt");
    file.open(QIODevice::ReadOnly);
    QByteArray array = file.readAll();
    file.close();
    for(int i=0;i=('0')&&array[i]<=('9'))
        {
           if(flag>=2)
           {
               point = i;
               break;
           }else{
               if(flag == 0)
               {

                   row = array[i]-'0';
               }
               if(flag ==1)
               {
                   col = array[i]-'0';
               }
               flag++;
           }
        }
    }
    sum = 99999;
    nowsum = 0;
    int nx = 0;int ny =0;
    for(int i=point;iq;
        q.push_back(t);
        DFS(0,0,q);

}

void Widget::DFS(int x, int y, QVector Q)
{

        if(x==row-1&&y==col-1)
         {
            if(sum>nowsum)
            {
                sum = nowsum;
                v.clear();
                QVector::iterator it;
                for (it = Q.begin(); it != Q.end(); it++) {
                     v.append(*it);
                }

            }
            return;
         }
         for(int i=0;i<4;i++)
         {
            int nx = x+fx[i];
            int ny = y+fy[i];
            if(nx>=0&&nx=0&&ny
 main.cpp
#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/563405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)