QT A*寻路算法代码

 

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include"road.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

    road *rd;
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

road.h

#ifndef ROAD_H
#define ROAD_H

#include <QWidget>
#include<QVector>
#include<QStack>
#include<QLabel>
#include<QDebug>
#include<QPainter>
#include<QTimer>
#include<qmath.h>
#define ROW 20
#define COL 20

struct Mypoint
{
    int x;
    int y;
};

enum direct{
    P_up,
    P_left,
    P_down,
    P_right
};

struct Treecode
{
    Mypoint mypoint;
    int f;
};

class road : public QWidget
{
    Q_OBJECT
public:
    explicit road(QWidget *parent = nullptr);
    Treecode tree[ROW][COL];
    void map();
     bool PathMap[ROW][COL]={0};
     void boolmap();
    void countroad();
    void coutroad();
    int FGH(Treecode current,Treecode end);
    Treecode *newTree(int x,int y);
    int Map[ROW][COL]={
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,0,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1},
                        {1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1},
                        {1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,1},
                        {1,0,1,1,1,0,0,0,1,1,1,0,1,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,1,0,1,1,0,0,0,1,1,1,1,0,1,1},
                        {1,1,1,1,0,0,0,0,0,0,0,1,0,1,1,1,1,0,1,1},
                        {1,1,1,1,0,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
                        {1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
                        {1,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,0,1,1},
                        {1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,1},
                        {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} };
signals:


private:
        QLabel *lab;
        QStack<Treecode> stack;
        int g=0;
        int h=0;
};

#endif // ROAD_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    rd = new road(this);
    rd->map();
     rd->boolmap();
    rd->countroad();
    rd->coutroad();

}

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

main.cpp

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

road.cpp

#include "road.h"

road::road(QWidget *parent) : QWidget(parent)
{
}

void road::map()   //画出地图
{
    for(int i=0;i<ROW;i++)
    {
        for(int j=0;j<COL;j++)
        {
            if(Map[i][j]==1)
            {
                 lab=new QLabel(this);
                 lab->setGeometry(QRect(j*25,i*25,25,25));
                 lab->setStyleSheet("QLabel{background-color:rgb(0,0,0);}");
                 PathMap[i][j]=1;
            }
            else {
            }
        }
    }
}

void road::boolmap() //地图是否可行,不可行为1
{
    for (int i=0;i<ROW;i++)
    {
        for (int j=0;j<COL;j++)
        {
            if(Map[i][j]==1)
            {
                PathMap[i][j]=1;
            }
        }
    }
}

void road::countroad()
{

    Treecode begin={1,1};//起点坐标
    Treecode end={18,18};//终点坐标
    PathMap[begin.mypoint.x][begin.mypoint.y] = true;
    stack.push(begin);
    Treecode current = begin;
    int a=12;
    while(a)
    {
        current.f=0;
        int row=current.mypoint.x;
        int col = current.mypoint.y;
        qDebug()<<"aaaa"<<current.mypoint.y<<current.mypoint.x;
        QVector<Treecode> vec;

        switch (P_up)
        {
        case P_up:
            if(PathMap[--row][col]==false)
            {
                current.mypoint.x=row;
                current.mypoint.y=col;
                current.f =FGH(current,end);
                qDebug()<<"up"<<current.f;
                vec.push_back(current);
                if(row==end.mypoint.x&&col==end.mypoint.y)//判断是否为终点
                {
                    stack.push(current);
                    return;
                }
            }
            else{
            };
        case P_left:
            if(PathMap[++row][--col]==false)
            {


                current.mypoint.x=row;
                current.mypoint.y=col;
                current.f =FGH(current,end);
                qDebug()<<"left"<<current.f;
                vec.push_back(current);
                if(row==end.mypoint.x&&col==end.mypoint.y)//判断是否为终点
                {
                    stack.push(current);
                    return;
                }
            }
            else {
            };
        case P_down:
            if(PathMap[++row][++col]==false)
            {


                current.mypoint.x=row;
                current.mypoint.y=col;
                current.f =FGH(current,end);
                qDebug()<<"down"<<current.f;
                vec.push_back(current);
                if(row==end.mypoint.x&&col==end.mypoint.y)//判断是否为终点
                {
                    stack.push(current);
                    return;
                }
            }
            else {
            };
        case P_right:
            if(PathMap[--row][++col]==false)
            {


                current.mypoint.x=row;
                current.mypoint.y=col;
                current.f =FGH(current,end);
                qDebug()<<"right"<<current.f;
                vec.push_back(current);
                if(row==end.mypoint.x&&col==end.mypoint.y)//判断是否为终点
                {
                    stack.push(current);
                    return;
                }
            }
            else {
            };
        default:
            if(!vec.isEmpty())
            {
                int i=0;
                Treecode *min=&vec[0];
                int min1 = vec[0].f;
                qDebug()<<"vec.size"<<vec.size();
                while (i<vec.size()-1)
                {
                     if(min1>vec[++i].f)
                     {
                         min1=vec[i].f;
                         min = &vec[i];
                     }
                }

                stack.push(*min);
                current.mypoint.x = min->mypoint.x;
                current.mypoint.y = min->mypoint.y;
                PathMap[min->mypoint.x][min->mypoint.y]=true;
            }
            else
            {
                stack.pop();
                current.mypoint.x=stack.top().mypoint.x;
                current.mypoint.y=stack.top().mypoint.y;
            }
            break;
        }
    }
}

int road::FGH(Treecode current,Treecode end)
{
    g=0;
    int f;
    QStack<Treecode> stack1 = stack;
    //qDebug()<<"end.x"<<end.mypoint.x;
    h= qAbs(end.mypoint.x-current.mypoint.x)+ qAbs(end.mypoint.y-current.mypoint.y);
    //qDebug()<<"H"<<h;
    while (!stack1.isEmpty()) {
        g++;
        //qDebug()<<"G"<<g;
        stack1.pop();
    }

    f= h+g;

    return f;
}

Treecode *road::newTree(int x, int y)
{
    Treecode *code = new Treecode;
    code->mypoint.x = x;
    code->mypoint.y = y;
    return code;
}

void road::coutroad()
{
    while(!stack.empty())
    {
        qDebug()<<"("<<stack.top().mypoint.y<<","<<stack.top().mypoint.x<<")"<<endl;
        lab=new QLabel(this);
        lab->setGeometry(QRect(stack.top().mypoint.y*25,stack.top().mypoint.x*25,25,25));
        lab->setStyleSheet("QLabel{background-color:rgb(250,0,0);}");
        stack.pop();
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值