Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit 9959e9a

Browse files
committed
fix: 修复图片过大时递归DFS崩溃
1 parent 7d9cb24 commit 9959e9a

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 23)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
55
set(CMAKE_CXX_EXTENSIONS OFF)
66

7-
project(DAC LANGUAGES C CXX VERSION 1.1.0)
7+
project(maze-algorithm-visualization LANGUAGES C CXX VERSION 1.2.0)
88

99
set(CMAKE_AUTOMOC ON)
1010
set(CMAKE_AUTORCC ON)

src/Algorithm/include/GraphPath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GraphPath final : public QObject {
4646
void updateTime(double time);
4747

4848
void _DFSRecursiveVersion(std::vector<Point> &points, std::vector<Point> &path, QImage &image, QPoint start,
49-
QPoint end, std::vector<std::vector<bool> > &vis);
49+
QPoint end, std::vector<std::vector<char> > &vis);
5050
};
5151

5252
#endif

src/Algorithm/src/GraphPath.cpp

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#include <queue>
1212
#include "windows.h"
1313
#include "GraphPath.h"
14+
1415
#define GLOG_NO_ABBREVIATED_SEVERITIES
16+
1517
#include <Config.h>
1618
#include <QThread>
1719
#include <Queue.hpp>
@@ -20,8 +22,18 @@
2022
#include <complex>
2123
#include <glog/logging.h>
2224

23-
static constexpr int fourDirs[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
24-
static constexpr int eightDirs[8][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
25+
static constexpr int fourDirs[4][2] = {{1, 0},
26+
{0, -1},
27+
{-1, 0},
28+
{0, 1}};
29+
static constexpr int eightDirs[8][2] = {{1, 0},
30+
{0, -1},
31+
{-1, 0},
32+
{0, 1},
33+
{1, 1},
34+
{-1, 1},
35+
{1, -1},
36+
{-1, -1}};
2537
static constexpr QPoint invalidPoint(-1, -1);
2638
static constexpr auto visitPoint = [](const QRgb color) {
2739
return color == Config::getInstance()->getConfigField(WALL_COLOR);
@@ -37,18 +49,18 @@ struct StorePoint {
3749
QPoint point;
3850
double distance;
3951

40-
StorePoint():
41-
point{invalidPoint}, distance{-1} {};
52+
StorePoint() :
53+
point{invalidPoint}, distance{-1} {};
4254

4355
StorePoint(const QPoint &point, const double distance) :
44-
point{point},
45-
distance{distance} {}
56+
point{point},
57+
distance{distance} {}
4658

4759
StorePoint(const StorePoint &other) = default;
4860

49-
StorePoint(StorePoint &&other) noexcept :
50-
point{other.point},
51-
distance{other.distance} {}
61+
StorePoint(StorePoint &&other) noexcept:
62+
point{other.point},
63+
distance{other.distance} {}
5264

5365
StorePoint &operator=(const StorePoint &other) {
5466
if (this == &other) return *this;
@@ -68,29 +80,29 @@ struct StorePoint {
6880
struct AStarPoint final : StorePoint {
6981
double weight;
7082

71-
AStarPoint():
72-
weight{1e9} {}
83+
AStarPoint() :
84+
weight{1e9} {}
7385

7486
AStarPoint(const QPoint &point, const double distance, const double weight) :
75-
StorePoint{point, distance},
76-
weight{weight} {}
87+
StorePoint{point, distance},
88+
weight{weight} {}
7789

7890
AStarPoint(const AStarPoint &other) = default;
7991

80-
AStarPoint(AStarPoint &&other) noexcept :
81-
StorePoint{std::move(other)},
82-
weight{other.weight} {}
92+
AStarPoint(AStarPoint &&other) noexcept:
93+
StorePoint{std::move(other)},
94+
weight{other.weight} {}
8395

8496
AStarPoint &operator=(const AStarPoint &other) {
8597
if (this == &other) return *this;
86-
StorePoint::operator =(other);
98+
StorePoint::operator=(other);
8799
weight = other.weight;
88100
return *this;
89101
}
90102

91103
AStarPoint &operator=(AStarPoint &&other) noexcept {
92104
if (this == &other) return *this;
93-
StorePoint::operator =(std::move(other));
105+
StorePoint::operator=(std::move(other));
94106
weight = other.weight;
95107
return *this;
96108
}
@@ -171,8 +183,8 @@ void GraphPath::BFS(std::vector<Point> &points, const QPixmap &pixmap, const QPo
171183
tmp = temp;
172184
}
173185
}
174-
EndFunc:
175-
TIMER_STOP
186+
EndFunc:
187+
TIMER_STOP
176188
LOG(INFO) << "Bfs search finish";
177189
updateTime(time);
178190
}
@@ -208,16 +220,16 @@ void GraphPath::DFSStackVersion(std::vector<Point> &points, const QPixmap &pixma
208220
}
209221
}
210222
stack.pop();
211-
EndLoop:
223+
EndLoop:
212224
continue;
213225
}
214226
if (!stack.isEmpty() && stack.pop() == end) {
215227
while (!stack.isEmpty()) {
216228
points.emplace_back(stack.pop(), PATH_POINT_COLOR);
217229
}
218230
}
219-
EndFunc:
220-
TIMER_STOP
231+
EndFunc:
232+
TIMER_STOP
221233
LOG(INFO) << "DfsStackVersion search finish";
222234
updateTime(time);
223235
}
@@ -226,7 +238,7 @@ void GraphPath::DFSRecursiveVersion(std::vector<Point> &points, const QPixmap &p
226238
const QPoint end) {
227239
LOG(INFO) << "DfsRecursiveVersion search start";
228240
TIMER_START
229-
std::vector vis(pixmap.width(), std::vector(pixmap.height(), false));
241+
std::vector<std::vector<char>> vis(pixmap.width(), std::vector<char>(pixmap.height(), 0));
230242
std::vector<Point> path;
231243
QImage image = pixmap.toImage();
232244
returnFlag = false;
@@ -241,15 +253,15 @@ void GraphPath::DFSRecursiveVersion(std::vector<Point> &points, const QPixmap &p
241253

242254
void GraphPath::_DFSRecursiveVersion(std::vector<Point> &points, std::vector<Point> &path, QImage &image,
243255
const QPoint start, const QPoint end,
244-
std::vector<std::vector<bool> > &vis) {
256+
std::vector<std::vector<char> > &vis) {
245257
if (returnFlag) return;
246258
if (start.x() == end.x() && start.y() == end.y()) {
247259
returnFlag = true;
248260
return;
249261
}
250262
if (start.x() < 0 || start.x() >= image.width() || start.y() < 0 || start.y() >= image.height()) return;
251263
if (vis[start.x()][start.y()]) return;
252-
vis[start.x()][start.y()] = true;
264+
vis[start.x()][start.y()] = 1;
253265
if (visitPoint(image.pixel(start))) return;
254266
points.emplace_back(start, SEARCHED_POINT_COLOR);
255267

@@ -309,8 +321,8 @@ void GraphPath::GBFS(std::vector<Point> &points, const QPixmap &pixmap, const QP
309321
tmp = temp;
310322
}
311323
}
312-
EndFunc:
313-
TIMER_STOP
324+
EndFunc:
325+
TIMER_STOP
314326
LOG(INFO) << "GBFS search finish";
315327
updateTime(time);
316328
}
@@ -368,8 +380,8 @@ void GraphPath::Dijkstra(std::vector<Point> &points, const QPixmap &pixmap, cons
368380
tmp = temp;
369381
}
370382
}
371-
EndFunc:
372-
TIMER_STOP
383+
EndFunc:
384+
TIMER_STOP
373385
LOG(INFO) << "Dijkstra search finish";
374386
updateTime(time);
375387
}
@@ -429,8 +441,8 @@ void GraphPath::aStar(std::vector<Point> &points, const QPixmap &pixmap, const Q
429441
tmp = temp;
430442
}
431443
}
432-
EndFunc:
433-
TIMER_STOP
444+
EndFunc:
445+
TIMER_STOP
434446
LOG(INFO) << "A* search finish";
435447
updateTime(time);
436448
}

0 commit comments

Comments
 (0)