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();
}
}