//finder.h
#ifndef
FINDER_H
#define FINDER_H
#include <QStack>
struct Position
{
int x;
int y;
Position(int x=0, int y=0)
{
this->x = x;
this->y = y;
}
} ;
struct StackElem
{
Position pos;
int direction;
} ;
class Finder
{
public:
Finder();
bool findPath(int maze[][10],Position startpoint, Position endpoint);
void nextPos(Position *p_pos , StackElem pm);
bool pass(int maze[][10] , Position curpos);
void footPrint(int maze[][10] , Position curpos);
void setElem(Position pos , int direction , StackElem* p_e);
void markprint(int maze[][10] , Position pos);
private:
QStack<StackElem> stack;
};
#endif // FINDER_H//mainwindow.h
#ifndef
MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H//finder.cpp
#include
"finder.h"
bool Finder::findPath(int maze[][10], Position startpoint, Position end)
{
static StackElem elem;
static Position curpos= startpoint;;
static int curstep = 1;
// do
// {
if(pass(maze , curpos))
{
footPrint(maze , curpos);
setElem(curpos , 1 , &elem);
stack.push_back(elem);
if(curpos.x==end.x&&curpos.y==end.y)
{
return true;
}
nextPos(&curpos , elem);
curstep++;
}
else
{
if(!stack.empty())
{
elem=stack.top();
stack.pop();
if(elem.direction==4&&!stack.empty())
{
markprint(maze , elem.pos);
//pop(p_s , &elem);
}
if(elem.direction<4)
{
elem.direction++;
stack.push_back(elem);
nextPos(&curpos , elem);
}
}
}
// }while(!stack.empty());
return false;
}
void Finder::nextPos(Position *p_pos , StackElem pm)
{
if(pm.direction==1)
{
p_pos->x=pm.pos.x+1;
p_pos->y=pm.pos.y;
}
else if(pm.direction==2)
{
p_pos->x=pm.pos.x;
p_pos->y=pm.pos.y-1;
}
else if(pm.direction==3)
{
p_pos->x=pm.pos.x;
p_pos->y=pm.pos.y+1;
}
else
{
p_pos->x=pm.pos.x-1;
p_pos->y=pm.pos.y;
}
}
bool Finder::pass(int maze[][10] , Position curpos)
{
if(maze[curpos.x][curpos.y]==1)
{
return true;
}
else
{
return false;
}
}
void Finder::footPrint(int maze[][10] , Position curpos)
{
maze[curpos.x][curpos.y] = 2;
}
void Finder::setElem(Position pos , int direction , StackElem* p_e)
{
p_e->pos = pos;
p_e->direction = direction;
}
void Finder::markprint(int maze[][10] , Position pos)
{
maze[pos.x][pos.y] = -1;
/*根据pass的设计,1为可行,else均不可,
可以设置任意值给死胡同*/
}
Finder::Finder()
{
}
//mainwindow.cpp
#include
"mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <finder.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
//! [4] //! [5]
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//! [5] //! [6]
timer->start(100);
setWindowTitle(tr("迷宫路径"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
static int maze[10][10] = {{0,0,0,0,0,0,0,0,0,0},/*0*/
{0,1,1,0,1,1,1,0,1,0},/*1*/
{0,1,1,0,1,1,1,0,1,0},/*2*/
{0,1,1,1,1,0,0,1,1,0},/*3*/
{0,1,0,0,0,1,1,1,1,0},/*4*/
{0,1,1,1,0,1,1,1,1,0},/*5*/
{0,1,0,1,1,1,0,1,1,0},/*6*/
{0,1,0,0,0,1,0,0,1,0},/*7*/
{0,0,1,1,1,1,1,1,1,0},/*8*/
{0,0,0,0,0,0,0,0,0,0}/*9*/
};
static Finder fd;
static bool isok=false;
if(!isok)
isok=fd.findPath(maze,Position(1,1),Position(8,8));
QPainter painter(this);
QPoint lefttop(-300,-250);
QSize size(50,50);
QRect rect(lefttop,size);
painter.translate(width() / 2, height() / 2);
//! [13] //! [14]
int side = qMin(width(), height());
painter.scale(side / 600.0, side / 600.0);
int i,j;
for(i=0; i < 10; i++)
{
for(j=0; j < 10; j++)
{
lefttop.setX(lefttop.x()+50);
rect.moveTo(lefttop);
if(maze[i][j]==1)
{
painter.fillRect(rect,Qt::green);
}
else if(maze[i][j]==2)
{
painter.fillRect(rect,Qt::darkBlue);
}
else if(maze[i][j]==-1)
{
painter.fillRect(rect,Qt::cyan);
}
else if(maze[i][j]==0)
{
painter.fillRect(rect,Qt::magenta);
}
else if(maze[i][j]==3)
{
painter.fillRect(rect,Qt::blue);
}
else if(maze[i][j]==4)
{
painter.fillRect(rect,Qt::red);
}
}
lefttop.setX(-300);
lefttop.setY(lefttop.y()+50);
}
}