This repository was archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRoute.cpp
More file actions
132 lines (108 loc) · 3.14 KB
/
SearchRoute.cpp
File metadata and controls
132 lines (108 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "SearchRoute.h"
#include <QVector>
#include <QQueue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <QDebug>
//遍历时用到的四个方向
const int fx[] = {-1, 0, 1, 0};
const int fy[] = {0, -1, 0, 1};
//最小转弯问题中使用到的结点
struct Node {
int f, x, y;//方向,坐标
Node() {}
Node(int f, int x, int y) :
f(f), x(x), y(y) {}
};
SearchRoute::SearchRoute(const QVector<QVector<int> >& map) :
map_copy(map)
{
n = map.size();
m = map[0].size();
routeX.setSharable(false);
routeY.setSharable(false);
}
SearchRoute::~SearchRoute()
{
// for(int i=0;i<4;i++)
// {
// delete[] dis[i];
// for(int j=0;j<n;j++)
// delete[] dis[i][j];
// }
// delete[] dis;
}
int SearchRoute::minTurn(int stx, int sty, int edx, int edy) {
//return 0;
int temp = map_copy[stx][sty];
map_copy[stx][sty] = map_copy[edx][edy] = -1;
qDebug()<<"create dis[][][]";
// for(int i=0;i<4;i++)
// {
// dis[i] = new int* [n];
// for(int j=0;j<n;j++)
// dis[i][j] = new int[m];
// }
// for(int i=0;i<4;i++)
// for(int j=0;j<n;j++)
// for(int k=0;k<m;k++)
// dis[i][j][k]=0;
memset(dis, 0x3f, sizeof(dis));
QQueue<Node>* pre = new QQueue<Node>();
QQueue<Node>* cur = new QQueue<Node>();
int step = 0;
for(int i = 0; i < 4; ++i) pre->push_back(Node(i, stx, sty));
for(int i = 0; i < 4; ++i) dis[i][stx][sty] = 0;
while(step <= MAXTURN) {
if(pre->empty()) {
step = MAXTURN + 1;
break;
}
Node t = pre->front(); pre->pop_front();
if(dis[t.f][t.x][t.y] != step) continue;
if(t.x == edx && t.y == edy) break;
for(int i = 0; i < 4; ++i) {
if((t.f + 2) % 4 == i) continue;
int x = t.x + fx[i], y = t.y + fy[i], d = dis[t.f][t.x][t.y] + (t.f != i);//方向不同即转弯
if(0 <= x && x < n && 0 <= y && y < m && map_copy[x][y] == -1 && d < dis[i][x][y]) {
dis[i][x][y] = d;
if(t.f == i) pre->push_back(Node(i, x, y));
else cur->push_back(Node(i, x, y));
}
}
if(pre->empty()) {
step++;
std::swap(pre, cur);
}
}
map_copy[stx][sty] = map_copy[edx][edy] = temp;
delete pre;
delete cur;
if(step > MAXTURN) return -1;
routeX.clear();
routeY.clear();
int x = edx, y = edy, f = 0;
for(int i = 1; i < 4; ++i)
if(dis[i][x][y] < dis[f][x][y]) f = i;
while(x != stx || y != sty) {
routeX.push_back(x);
routeY.push_back(y);
for(int i = 0; i < 4; ++i) {
if((i + 2) % 4 == f) continue;
int px = x - fx[f], py = y - fy[f];
if(dis[f][x][y] == dis[i][px][py] + (f != i)) {
x = px, y = py, f = i;
break;
}
}
}
// qDebug()<<"spot 5";
routeX.push_back(x);
routeY.push_back(y);
return step;
}
void SearchRoute::getRoute(QVector<int> &routeX, QVector<int> &routeY) {
routeX = this->routeX;
routeY = this->routeY;
}