预览效果:
Maze.pro文件
1 #------------------------------------------------- 2 # 3 # Project created by QtCreator 2016-12-26T14:10:37 4 # 5 #------------------------------------------------- 6 7 QT += core gui 8 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 11 TARGET = Maze 12 TEMPLATE = app 13 14 15 SOURCES += main.cpp 16 mainwindow.cpp 17 MAZE.cpp 18 DijkstraWindow.cpp 19 head.cpp 20 21 HEADERS += mainwindow.h 22 MAZE.h 23 DijkstraWindow.h 24 head.h 25 26 FORMS += 27 head.ui 28 29 RESOURCES += 30 img.qrc
DijkstraWindow.h文件
1 #ifndef DIJKSTRAWINDOW_H 2 #define DIJKSTRAWINDOW_H 3 4 #include <QWidget> 5 #include <iostream> 6 #include <QTime> 7 #include <QLineEdit> 8 #include <QPushButton> 9 #include <QPainter> 10 #include <QLabel> 11 #include <QMessageBox> 12 #include <QDebug> 13 #include <QKeyEvent> 14 #include <QPixmap> 15 #include <QTextEdit> 16 17 class DijkstraWindow : public QWidget 18 { 19 Q_OBJECT 20 21 public: 22 23 24 DijkstraWindow(QWidget *parent = 0); 25 // void paintEvent(QPaintEvent *); 26 // void keyPressEvent(QKeyEvent *e); 27 void dijkstra(); 28 void set_n(int tn){n = tn;} 29 int get_n(){return n;} 30 void set_m(int tm){m = tm;} 31 int get_m(){return m;} 32 ~DijkstraWindow(); 33 private: 34 QTextEdit *te_datain; 35 QPushButton *queding; 36 QLabel *bushu, *huafei, *bushuOut, *huafeiOut, *shuoming; 37 int n, m, s, t; 38 private slots: 39 void startDijkstra(); 40 }; 41 #endif // DIJKSTRAWINDOW_H
head.h文件
1 #ifndef HEAD_H 2 #define HEAD_H 3 4 #include <QWidget> 5 #include "DijkstraWindow.h" 6 #include "mainwindow.h" 7 8 namespace Ui { 9 class Head; 10 } 11 12 class Head : public QWidget 13 { 14 Q_OBJECT 15 16 public: 17 explicit Head(QWidget *parent = 0); 18 ~Head(); 19 20 private slots: 21 void on_pushButton_clicked(); 22 23 void on_pushButton_2_clicked(); 24 25 private: 26 Ui::Head *ui; 27 DijkstraWindow d; 28 MainWindow w; 29 }; 30 31 #endif // HEAD_H
mainwindow.h文件
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include "MAZE.h" 5 #include <QMainWindow> 6 #include <QWidget> 7 #include <iostream> 8 #include <QTime> 9 #include <QLineEdit> 10 #include <QPushButton> 11 #include <QPainter> 12 #include <QLabel> 13 #include <QMessageBox> 14 #include <QDebug> 15 #include <QKeyEvent> 16 #include <QPixmap> 17 18 class MainWindow : public QWidget 19 { 20 Q_OBJECT 21 22 public: 23 MainWindow(QWidget *parent = 0); 24 void paintEvent(QPaintEvent *); 25 void keyPressEvent(QKeyEvent *e); 26 ~MainWindow(); 27 private: 28 QLabel *ql_shuru,*ql_bushu, *ql_bushuOut, *yongshi, *yongshiOut; 29 QLineEdit *infile; 30 QPushButton *queding; 31 QPushButton *zhaolu; 32 MAZE *m; 33 int X = 1; 34 int Y = 0; 35 bool bfs_fg = false; 36 private slots: 37 void startMaze(); 38 void startBFS(); 39 }; 40 41 #endif // MAINWINDOW_H
MAZE.h文件
1 #ifndef MAZE_H 2 #define MAZE_H 3 4 static const int N = 1000; 5 6 class MAZE 7 { 8 public: 9 int maze[N][N]; 10 struct point 11 { 12 int x, y, pre; 13 }q[N*N], path[N*N]; 14 MAZE(); 15 void set_n(int tn); 16 int get_n(); 17 int get_len_path(){return len_path;} 18 void set_len_path(int tn){len_path = tn;} 19 void printPath() 20 { 21 bfs(); 22 for(int i = len_path-1; i >= 0; i--) 23 if(maze[path[i].x][path[i].y]==0) 24 maze[path[i].x][path[i].y] = 6; 25 } 26 void recoverPath() 27 { 28 for(int i = len_path-1; i >= 0; i--) 29 if(maze[path[i].x][path[i].y]==6) 30 maze[path[i].x][path[i].y] = 0; 31 } 32 void mazeInit(); 33 int searchPath(int x, int y); 34 void print(); 35 ~MAZE(); 36 private: 37 int n, len_path, nn; 38 void bfs(); 39 void getPath(int pos); 40 }; 41 42 #endif // MAZE_H
DijkstraWindow.cpp文件
1 #include "DijkstraWindow.h" 2 #include <cstring> 3 #include <iostream> 4 #include <QString> 5 #include <QStringList> 6 7 static const int maxn = 1005; 8 static const int inf = 9999999; 9 int Tu_dist[maxn][maxn], Tu_pay[maxn][maxn], dis[maxn], pay[maxn], book[maxn]; 10 11 DijkstraWindow::DijkstraWindow(QWidget *parent) 12 : QWidget(parent) 13 { 14 this->setFixedSize(220,450); 15 16 shuoming = new QLabel(this); 17 shuoming->setText("说明:第一行输入4个数,分别表示 点的个数,边的个数,起点,终点。 之后每一行输入4个数u、v、d、p, 表示u和v之间有一条长度为d的路,需 要p的花费。给出图,可计算起点到终 点的最短距离及其花费,如果最短距 离有多条路线,则输出花费最少的。"); 18 shuoming->setGeometry(10, 5, 200, 120); 19 20 te_datain = new QTextEdit(this); 21 te_datain->setText("3 2 1 3 1 2 5 6 2 3 4 5"); 22 te_datain->setGeometry(10, 130, 200, 220); 23 24 queding = new QPushButton(this); 25 queding->setText("确定"); 26 queding->setGeometry(60, 370, 100, 20); 27 28 bushu = new QLabel(this); 29 bushu->setText("最短路径长度:"); 30 bushu->setGeometry(40, 400, 80, 20); 31 32 bushuOut = new QLabel(this); 33 bushuOut->setText("0"); 34 bushuOut->setGeometry(140, 400, 20, 20); 35 36 huafei = new QLabel(this); 37 huafei->setText("花费:"); 38 huafei->setGeometry(85, 425, 40, 20); 39 40 huafeiOut = new QLabel(this); 41 huafeiOut->setText("0"); 42 huafeiOut->setGeometry(140, 425, 20, 20); 43 44 connect(queding,SIGNAL(clicked()),this,SLOT(startDijkstra())); 45 } 46 47 void DijkstraWindow::startDijkstra() 48 { 49 50 int a, b, d, p; 51 //QString str = te_datain->toPlainText(); 52 int line_n=te_datain->document()->lineCount(); 53 if(line_n != 0) 54 { 55 for(int i = 0; i < line_n; i++) 56 { 57 QString str=te_datain->toPlainText().section(' ',i-line_n,i-line_n,QString::SectionSkipEmpty); 58 QStringList strlist=str.split(" "); 59 if(i == 0) 60 { 61 n = strlist[0].toInt(); 62 m = strlist[1].toInt(); 63 s = strlist[2].toInt(); 64 t = strlist[3].toInt(); 65 for(int i = 0; i <= n; i++) 66 for(int j = 0; j <= n; j++) 67 { 68 Tu_dist[i][j] =inf; 69 Tu_pay[i][j] = inf; 70 } 71 //std::cout<<n<<" "<<m<<" "<<s<<" "<<t<<std::endl; 72 }else 73 { 74 a = strlist[0].toInt(); 75 b = strlist[1].toInt(); 76 d = strlist[2].toInt(); 77 p = strlist[3].toInt(); 78 if(d<Tu_dist[a][b]) 79 { 80 Tu_dist[a][b] = Tu_dist[b][a] = d; 81 Tu_pay[a][b] = Tu_pay[b][a] = p; 82 } 83 //std::cout<<a<<" "<<b<<" "<<d<<" "<<p<<std::endl; 84 } 85 } 86 87 for(int i = 1; i <= n; i++) 88 { 89 dis[i] = Tu_dist[s][i]; 90 pay[i] = Tu_pay[s][i]; 91 } 92 93 memset(book, 0, sizeof(book)); 94 book[s] = 1; 95 dis[s] = 0; 96 pay[s] = 0; 97 98 int mindist, u, v; 99 for(int i = 1; i < n; i++) 100 { 101 mindist = inf; 102 for(int j = 1; j <= n; j++) 103 { 104 if(book[j]==0&&dis[j]<mindist) 105 { 106 mindist = dis[j]; 107 u = j; 108 } 109 } 110 book[u] = 1; 111 for(int v = 1; v <= n; v++) 112 { 113 if(!book[v] && Tu_dist[u][v]<inf) 114 { 115 if(dis[v]>dis[u]+Tu_dist[u][v]) 116 { 117 dis[v] = dis[u]+Tu_dist[u][v]; 118 pay[v] = pay[u]+Tu_pay[u][v]; 119 } 120 else if(dis[v]==(dis[u]+Tu_dist[u][v])) 121 pay[v] = (pay[u]+Tu_pay[u][v])<pay[v]?pay[u]+Tu_pay[u][v]:pay[v]; 122 } 123 } 124 } 125 QString str; 126 bushuOut->setText(str.setNum(dis[t])); 127 huafeiOut->setText(str.setNum(pay[t])); 128 129 update(); 130 } 131 } 132 133 DijkstraWindow::~DijkstraWindow() 134 { 135 136 }
head.cpp文件
1 #include "head.h" 2 #include "ui_head.h" 3 4 Head::Head(QWidget *parent) : 5 QWidget(parent), 6 ui(new Ui::Head) 7 { 8 ui->setupUi(this); 9 } 10 11 Head::~Head() 12 { 13 delete ui; 14 } 15 16 void Head::on_pushButton_clicked() 17 { 18 w.show(); 19 } 20 21 void Head::on_pushButton_2_clicked() 22 { 23 d.show(); 24 }
main.cpp文件
1 #include "head.h" 2 #include <QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 Head h; 8 h.show(); 9 10 return a.exec(); 11 }
mainwindow.cpp文件
1 #include "mainwindow.h" 2 #include "MAZE.h" 3 #include <iostream> 4 #include <time.h> 5 //#include <QTimer> 6 7 #define size 20 8 using namespace std; 9 MainWindow::MainWindow(QWidget *parent) 10 : QWidget(parent) 11 { 12 int n = 31; 13 m = new MAZE(); 14 m->set_n(n); 15 m->mazeInit(); 16 m->print(); 17 //m->printPath(); 18 this->setWindowTitle("迷宫"); 19 this->setFixedSize((n+9)*size,n*size); 20 //this->resize((n+10)*size,n*size); 21 this->setFocus(Qt::MouseFocusReason); 22 QPalette pa; 23 pa.setColor(QPalette::WindowText,Qt::black); 24 QFont ft; 25 ft.setPointSize(14); 26 27 ql_shuru = new QLabel(this); 28 ql_shuru->setText("迷宫大小"); 29 ql_shuru->setPalette(pa); 30 ql_shuru->setFont(ft); 31 ql_shuru->setGeometry((n+2)*size, 2*size, 100, 20); 32 33 infile = new QLineEdit(this); 34 infile->setText("30"); 35 infile->setGeometry((n+2)*size, 4*size, 90, 20); 36 37 queding = new QPushButton(this); 38 queding->setText("创建"); 39 queding->setGeometry((n+3)*size, 6*size, 60,20); 40 41 zhaolu = new QPushButton(this); 42 zhaolu->setText("找最短路"); 43 zhaolu->setGeometry((n+3)*size, (n-6)*size, 60,20); 44 45 ql_bushu = new QLabel(this); 46 ql_bushu->setText("最短路步数"); 47 ql_bushu->setGeometry((n+2)*size, (n-4)*size, 60,20); 48 49 //QString str; 50 ql_bushuOut = new QLabel(this); 51 //ql_bushuOut->setText(str.setNum(m->get_len_path())); 52 ql_bushuOut->setText("0"); 53 ql_bushuOut->setGeometry((n+7)*size, (n-4)*size, 60,20); 54 55 yongshi = new QLabel(this); 56 yongshi->setText("用时"); 57 yongshi->setGeometry((n+4)*size, (n-2)*size, 60,20); 58 59 yongshiOut = new QLabel(this); 60 yongshiOut->setText("0"); 61 yongshiOut->setGeometry((n+7)*size, (n-2)*size, 60,20); 62 63 connect(queding,SIGNAL(clicked()),this,SLOT(startMaze())); 64 connect(zhaolu,SIGNAL(clicked()),this,SLOT(startBFS())); 65 } 66 67 MainWindow::~MainWindow() 68 { 69 70 } 71 72 void MainWindow::paintEvent(QPaintEvent *) 73 { 74 QPainter painter(this); 75 // 反走样 painter.setRenderHint(QPainter::Antialiasing, true); 76 // 绘制图标 painter.drawPixmap(rect(), QPixmap(":/img/nwafu.jmp")); 77 //painter.setPen(Qt::black); 78 //painter.setRenderHint(QPainter::Antialiasing, true);// 设置画笔颜色、宽度 79 //painter.setPen(QPen(QColor(0, 160, 160), 1)); 80 int n = m->get_n(); 81 for(int i = 0; i < n; i++) 82 { 83 for(int j = 0; j < n; j++) 84 { 85 if(m->maze[i][j] ==1){ 86 painter.setPen(Qt::darkCyan); 87 painter.setBrush(QBrush(Qt::darkCyan,Qt::SolidPattern)); 88 painter.drawRect(QRect(j*size,i*size,size,size)); 89 }else if(m->maze[i][j] == 2){ 90 // painter.setPen(Qt::darkMagenta); 91 // painter.setBrush(QBrush(Qt::darkMagenta,Qt::SolidPattern)); 92 // painter.drawRect(QRect(j*size,i*size,size,size)); 93 painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/new/prefix1/img/panda2.jpg").scaled(20,20)); 94 }else if(m->maze[i][j] == 3){ 95 painter.setPen(Qt::red); 96 painter.setBrush(QBrush(Qt::red,Qt::SolidPattern)); 97 painter.drawRect(QRect(j*size,i*size,size,size)); 98 painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/new/prefix1/img/bamboo.jpg").scaled(20,20)); 99 }else if(m->maze[i][j] == 0){ 100 painter.setPen(Qt::white); 101 painter.setBrush(QBrush(Qt::white,Qt::SolidPattern)); 102 painter.drawRect(QRect(j*size,i*size,size,size)); 103 }else if(m->maze[i][j] == 6){ 104 // painter.setPen(Qt::darkGray); 105 // painter.setBrush(QBrush(Qt::darkGray,Qt::SolidPattern)); 106 // painter.drawRect(QRect(j*size,i*size,size,size)); 107 painter.drawPixmap(j*20,i*20,20,20,QPixmap(":/new/prefix1/img/foot.jpg").scaled(20,20)); 108 } 109 } 110 } 111 } 112 113 void MainWindow::keyPressEvent(QKeyEvent *e) 114 { 115 if(bfs_fg){ 116 m->recoverPath(); 117 bfs_fg = false; 118 ql_bushuOut->setText("0"); 119 update(); 120 } 121 int tx = X, ty = Y; 122 int n = m->get_n(); 123 if(e->key()==87||e->key()==16777235)//上 124 { 125 if(X>0 && m->maze[X-1][Y] != 1) 126 { 127 X=X-1; 128 } 129 } 130 else if(e->key()==83||e->key()==16777237)//下 131 { 132 if(X<n-1 && m->maze[X+1][Y] != 1) 133 { 134 X=X+1; 135 } 136 } 137 else if(e->key()==65||e->key()==16777234)//左 138 { 139 if(Y>0 && m->maze[X][Y-1] != 1) 140 { 141 Y=Y-1; 142 } 143 } 144 else if(e->key()==68||e->key()==16777236)//右 145 { 146 147 if(Y<n-1 && m->maze[X][Y+1] != 1) 148 { 149 Y=Y+1; 150 } 151 } 152 int tmp = m->maze[X][Y]; 153 if(tmp == 3){ 154 QMessageBox::information(this,"提示","到达终点",QMessageBox::Yes); 155 }else{ 156 m->maze[X][Y] = m->maze[tx][ty]; 157 m->maze[tx][ty] = tmp; 158 } 159 update(); 160 } 161 162 void MainWindow::startMaze() 163 { 164 int n = infile->text().toInt(); 165 //n = 2*n+2; 166 if(n<15)n = 15; 167 if(n>100)n = 100; 168 if(n%2 == 0)n++; 169 this->setFixedSize((n+9)*size,n*size); 170 //this->resize((n+10)*size,n*size); 171 172 // ql_shuru->setText("请输入迷宫的大小"); 173 ql_shuru->setGeometry((n+2)*size, 2*size, 100, 20); 174 infile->setGeometry((n+2)*size, 4*size, 90, 20); 175 //queding->setText("创建"); 176 queding->setGeometry((n+3)*size, 6*size, 60,20); 177 //zhaolu->setText("找最短路"); 178 zhaolu->setGeometry((n+3)*size, (n-6)*size, 60,20); 179 //ql_bushu->setText("最短路步数"); 180 ql_bushu->setGeometry((n+2)*size, (n-4)*size, 60,20); 181 ql_bushuOut->setText("0"); 182 ql_bushuOut->setGeometry((n+7)*size, (n-4)*size, 60,20); 183 yongshi->setGeometry((n+4)*size, (n-2)*size, 60,20); 184 yongshiOut->setGeometry((n+7)*size, (n-2)*size, 60,20); 185 m->set_n(n); 186 m->mazeInit(); 187 X = 1, Y = 0; 188 this->setFocus(Qt::MouseFocusReason); 189 update(); 190 } 191 192 void MainWindow::startBFS() 193 { 194 time_t start, end; 195 time(&start); 196 m->printPath(); 197 time(&end); 198 double cost = difftime(end,start); 199 QString str; 200 ql_bushuOut->setText(str.setNum(m->get_len_path())); 201 yongshiOut->setText(str.setNum(cost)); 202 //cout<<"ok"<<endl; 203 bfs_fg = true; 204 this->setFocus(Qt::MouseFocusReason); 205 update(); 206 }
MAZE.cpp文件
1 #include "MAZE.h" 2 #include <iostream> 3 #include <windows.h> 4 #include <cstdio> 5 #include <cmath> 6 #include <time.h> 7 #include <cstring> 8 using namespace std; 9 int fa[N*N]; 10 int dx[4] = {1, 0, -1, 0}; 11 int dy[4] = {0, 1, 0, -1}; 12 13 MAZE::MAZE(){} 14 15 void MAZE::set_n(int tn) 16 { 17 n = tn; 18 nn = n/2; 19 } 20 21 int MAZE::get_n() 22 { 23 return n; 24 } 25 26 void MAZE::print() 27 { 28 bfs(); 29 // for(int i = 0; i < n; i++) 30 // { 31 // for(int j = 0; j < n; j++) 32 // cout<<maze[i][j]<<" "; 33 // cout<<endl; 34 // } 35 } 36 37 void init() 38 { 39 for(int i = 0; i < N*N; i++) 40 fa[i] = i; 41 } 42 43 int getfa(int x) 44 { 45 if(fa[x] == x)return x; 46 else return fa[x] = getfa(fa[x]); 47 } 48 49 void Merge(int a, int b) 50 { 51 int af = getfa(a); 52 int bf = getfa(b); 53 if(af != bf) 54 fa[bf] = af; 55 } 56 57 int tolist(int x, int y, int n) 58 { 59 return x*n+y; 60 } 61 62 void MAZE::mazeInit() 63 { 64 for(int i=0; i<=nn*2+2; ++i) 65 for(int j=0; j<=nn*2+2; ++j) 66 maze[i][j] = 1; 67 68 for(int i=0, j=2*nn+2; i<=2*nn+2; ++i) 69 { 70 maze[i][0] = 0; 71 maze[i][j] = 0; 72 } 73 for(int i=0, j=2*nn+2; i<=2*nn+2; ++i) 74 { 75 maze[0][i] = 0; 76 maze[j][i] = 0; 77 } 78 maze[2][1] = 2; 79 maze[2*nn][2*nn+1] = 3; 80 81 srand((unsigned)time(NULL)); 82 searchPath(rand()%nn+1, rand()%nn+1); 83 84 for(int i = 0; i < n; i++) 85 { 86 for(int j = 0; j < n; j++) 87 { 88 maze[i][j] = maze[i+1][j+1]; 89 } 90 } 91 92 len_path = 0; 93 // int sx, sy, ex, ey, x, y; 94 // init(); 95 // for(int i = 0; i < n; i++) 96 // for(int j = 0; j < n; j++) 97 // maze[i][j] = 1; 98 // for(int i = 1; i < n; i++) 99 // { 100 // if(i&1) 101 // for(int j = 1; j < n; j+=2) 102 // maze[i][j] = 0; 103 // } 104 // //print(n); 105 // //cout<<"*********************"<<endl; 106 // srand(time(NULL)); 107 // int d; 108 // int tx1, ty1, tx2, ty2; 109 // sx = sy = 1; 110 // ex = ey = n-3; 111 112 // //cout<<"ok"<<endl; 113 // maze[sx][sy] = maze[ex][ey] = 0; 114 // while(getfa(tolist(sx, sy, n)) != getfa(tolist(ex, ey, n))) 115 // { 116 // do 117 // { 118 // x = rand()%(n-2)+1; 119 // y = (rand()+2333)%(n-2)+1; 120 // } 121 // while(maze[x][y] != 1); 122 // d = x%2; 123 // if(!d) 124 // { 125 // tx1 = x+1; 126 // ty1 = y; 127 // tx2 = x-1; 128 // ty2 = y; 129 // if(getfa(tolist(tx1, ty1, n)) != getfa(tolist(tx2, ty2, n))) 130 // { 131 // maze[x][y] = 0; 132 // Merge(tolist(tx1, ty1, n), tolist(tx2, ty2, n)); 133 // } 134 // } 135 // else 136 // { 137 // tx1 = x; 138 // ty1 = y+1; 139 // tx2 = x; 140 // ty2 = y-1; 141 // if(getfa(tolist(tx1, ty1, n)) != getfa(tolist(tx2, ty2, n))) 142 // { 143 // maze[x][y] = 0; 144 // Merge(tolist(tx1, ty1, n), tolist(tx2, ty2, n)); 145 // } 146 // } 147 // } 148 // for(int i = 0; i < n; i++) 149 // { 150 // maze[i][n-1] = 1; 151 // maze[n-1][i] = 1; 152 // } 153 // maze[sx][sy] = 2; 154 // maze[ex][ey] = 3; 155 //print(); 156 } 157 158 int MAZE::searchPath(int x, int y) 159 { 160 static int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; 161 int zx = x*2; 162 int zy = y*2; 163 int next, turn, i; 164 maze[zx][zy] = 0; 165 turn = rand()%2 ? 1 : 3; 166 for(i=0, next=rand()%4; i<4; ++i, next=(next+turn)%4) 167 if(maze[zx+2*dir[next][0]][zy+2*dir[next][1]] == 1) 168 { 169 maze[zx+dir[next][0]][zy+dir[next][1]] = 0; 170 searchPath(x+dir[next][0], y+dir[next][1]); 171 } 172 return 0; 173 } 174 175 void MAZE::getPath(int pos) 176 { 177 while(pos != -1) 178 { 179 path[len_path].x = q[pos].x; 180 path[len_path].y = q[pos].y; 181 len_path++; 182 pos = q[pos].pre; 183 } 184 } 185 186 void MAZE::bfs() 187 { 188 int front, tail, sx, sy; 189 for(int i = 0; i < n; i++) 190 for(int j = 0; j < n; j++) 191 if(maze[i][j] == 2) 192 { 193 sx = i; sy = j; 194 } 195 front = tail = 0; 196 q[tail].x = sx; 197 q[tail].y = sy; 198 q[tail].pre = -1; 199 tail++; 200 int x, y, nx, ny; 201 bool fg = false; 202 while(front < tail) 203 { 204 x = q[front].x; 205 y = q[front].y; 206 for(int i = 0; i < 4; i++) 207 { 208 nx = x+dx[i]; 209 ny = y+dy[i]; 210 if(nx>=0&&nx<n&&ny>=0&&ny<n&&maze[nx][ny]==0) 211 { 212 maze[nx][ny] = 5; 213 q[tail].x = nx; 214 q[tail].y = ny; 215 q[tail].pre = front; 216 tail++; 217 } 218 if(maze[nx][ny] == 3){ 219 q[tail].x = nx; 220 q[tail].y = ny; 221 q[tail].pre = front; 222 tail++; 223 fg = true; 224 len_path = 0; 225 path[len_path].x = nx; 226 path[len_path].y = ny; 227 len_path++; 228 getPath(front); 229 } 230 } 231 if(fg)break; 232 front++; 233 } 234 for(int i = 0; i < n; i++) 235 for(int j = 0; j < n; j++) 236 if(maze[i][j] == 5) 237 maze[i][j] = 0; 238 }
head.ui文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>Head</class> 4 <widget class="QWidget" name="Head"> 5 <property name="enabled"> 6 <bool>true</bool> 7 </property> 8 <property name="geometry"> 9 <rect> 10 <x>0</x> 11 <y>0</y> 12 <width>351</width> 13 <height>220</height> 14 </rect> 15 </property> 16 <property name="mouseTracking"> 17 <bool>false</bool> 18 </property> 19 <property name="windowTitle"> 20 <string>Maze</string> 21 </property> 22 <property name="windowIcon"> 23 <iconset resource="img.qrc"> 24 <normaloff>:/new/prefix1/img/xingong.jpg</normaloff>:/new/prefix1/img/xingong.jpg</iconset> 25 </property> 26 <property name="autoFillBackground"> 27 <bool>false</bool> 28 </property> 29 <widget class="QPushButton" name="pushButton"> 30 <property name="geometry"> 31 <rect> 32 <x>50</x> 33 <y>140</y> 34 <width>101</width> 35 <height>51</height> 36 </rect> 37 </property> 38 <property name="font"> 39 <font> 40 <family>微软雅黑</family> 41 <pointsize>12</pointsize> 42 <weight>75</weight> 43 <bold>true</bold> 44 </font> 45 </property> 46 <property name="styleSheet"> 47 <string notr="true">background: rgb(0, 170, 127)</string> 48 </property> 49 <property name="text"> 50 <string>Maze</string> 51 </property> 52 </widget> 53 <widget class="QPushButton" name="pushButton_2"> 54 <property name="geometry"> 55 <rect> 56 <x>200</x> 57 <y>140</y> 58 <width>101</width> 59 <height>51</height> 60 </rect> 61 </property> 62 <property name="font"> 63 <font> 64 <family>微软雅黑</family> 65 <pointsize>12</pointsize> 66 <weight>75</weight> 67 <bold>true</bold> 68 </font> 69 </property> 70 <property name="cursor"> 71 <cursorShape>ArrowCursor</cursorShape> 72 </property> 73 <property name="styleSheet"> 74 <string notr="true">background: rgb(0, 170, 127)</string> 75 </property> 76 <property name="text"> 77 <string>Dijkstra</string> 78 </property> 79 </widget> 80 <widget class="QFrame" name="frame"> 81 <property name="geometry"> 82 <rect> 83 <x>-1</x> 84 <y>-1</y> 85 <width>351</width> 86 <height>221</height> 87 </rect> 88 </property> 89 <property name="styleSheet"> 90 <string notr="true">border-image: url(:/new/prefix1/img/mazeimg.jpg);</string> 91 </property> 92 <property name="frameShape"> 93 <enum>QFrame::StyledPanel</enum> 94 </property> 95 <property name="frameShadow"> 96 <enum>QFrame::Raised</enum> 97 </property> 98 </widget> 99 <zorder>frame</zorder> 100 <zorder>pushButton</zorder> 101 <zorder>pushButton_2</zorder> 102 </widget> 103 <resources> 104 <include location="img.qrc"/> 105 </resources> 106 <connections/> 107 </ui>