windows程序设计2017考试题03 MazeTeacher 迷宫(老师的参考代码)

1.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);
                painter.setBrush(Qt::green);
                painter.drawRect(rect);
            }
            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);
    }

}



2.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



3.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


4.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()
{
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值