1
1
#include " algorithm.h"
2
2
3
- algorithm::algorithm (std::vector<std::vector<std::string>> &p)
3
+ algorithm::algorithm (printList &p)
4
4
{
5
5
algo = &distance::manhattan; // select distance algorithm
6
6
for (int i = 0 ; i < p.size (); i++) {
@@ -9,14 +9,14 @@ algorithm::algorithm(std::vector<std::vector<std::string>> &p)
9
9
std::vector<int > st;
10
10
st.resize (p[i].size (), 0 );
11
11
for (int j = 0 ; j < p[i].size (); j++) {
12
- if (p[i][j] == " E" ) {
12
+ if (p[i][j]. val == " E" ) {
13
13
x2 = i;
14
14
y2 = j;
15
- } else if (p[i][j] == " S" ) {
15
+ } else if (p[i][j]. val == " S" ) {
16
16
x1 = i;
17
17
y1 = j;
18
18
st[j] = 1 ;
19
- } else if (p[i][j] == " O" ) {
19
+ } else if (p[i][j]. val == " O" ) {
20
20
gt[j] = 1 ;
21
21
}
22
22
}
@@ -30,8 +30,8 @@ void algorithm::setAlgo(int (distance::*algo)(int, int, int, int)) {
30
30
this ->algo = algo;
31
31
}
32
32
33
- algorithm::list algorithm::start () {
34
- list pList = astar (x1,y1 ,x2,y2,p,g,s,algo);
33
+ algorithm::printList algorithm::start () {
34
+ printList pList = astar (x1,y1 ,x2,y2,p,g,s,algo);
35
35
p = pList.back ();
36
36
return pList;
37
37
}
@@ -41,7 +41,7 @@ int algorithm::distance::chebyshev(int i, int j, int x2, int y2) {
41
41
}
42
42
int algorithm::distance::bfs (int i, int j, int x2, int y2) {
43
43
i = j = x2 = y2 = 0 ;
44
- return 0 ; // 雪比切夫距离
44
+ return 0 ; // BFS
45
45
}
46
46
int algorithm::distance::euclidean (int i, int j, int x2, int y2) {
47
47
return (int ) (sqrt ((i - x2) * (i - x2) + (j - y2) * (j - y2))); // 欧几里德距离
@@ -50,38 +50,38 @@ int algorithm::distance::manhattan(int i, int j, int x2, int y2) {
50
50
return abs (i - x2) + abs (j - y2); // 曼哈顿距离
51
51
}
52
52
53
- algorithm::list algorithm::astar (int x1, int y1, int x2, int y2,
54
- std::vector<std::vector<std::string>> &p,
53
+ algorithm::printList algorithm::astar (int x1, int y1, int x2, int y2, printList &p,
55
54
const std::vector<std::vector<int >> g, std::vector<std::vector<int >> s,
56
55
int (distance::*algo)(int , int , int , int )) {
57
- std::vector<std::vector<std::vector<std::string>>> pList;
58
- pList.push_back (p);
56
+
59
57
if (p.empty () || p[0 ].empty () || (x1==x2&&y1 ==y2) ||
60
58
x1<0 ||x2<0 ||y1 <0 ||y2<0 ||x1>=p.size ()||x2>=p.size ()||y1 >=p[0 ].size ()||y2>=p[0 ].size ())
61
- return pList ;
59
+ return p ;
62
60
int M = p.size ();
63
61
int N = p[0 ].size ();
64
62
distance dis;
63
+ // init
65
64
int c = 0 ;
66
65
s[x1][y1 ] = 1 ;
67
66
std::priority_queue<Node> q;
68
67
q.push ({x1, y1 , 0 , (dis.*algo)(x1, y1 , x2, y2)});
68
+ p[x1][y1 ].g = 0 ;
69
69
bool exit = false ;
70
+ // astar
70
71
while (!q.empty ()) {
71
72
Node n = q.top ();
72
73
q.pop ();
73
- pList.push_back (p);
74
74
for (int i = 0 ; i < 4 ; ++i) {
75
75
int a = diri[i] + n.i ;
76
76
int b = dirj[i] + n.j ;
77
77
if (overflow (a, b, M, N) && s[a][b] != 1 && g[a][b] != 1 ) {
78
78
c++;
79
79
s[a][b] = 1 ;
80
80
q.push ({a, b, n.g + 1 , (dis.*algo)(a, b, x2, y2)});
81
- p[a][b] = dir[i];
81
+ p[a][b].val = dir[i];
82
+ p[a][b].g = n.g + 1 ;
82
83
if (a == x2 && b == y2) {
83
84
print (p,x2,y2);
84
- pList.push_back (p);
85
85
std::cout << " ok " << c << " step" << std::endl;
86
86
exit = true ;
87
87
break ;
@@ -95,38 +95,38 @@ algorithm::list algorithm::astar(int x1, int y1, int x2, int y2,
95
95
if (!exit ) {
96
96
std::cout << " failed" << std::endl;
97
97
}
98
- return pList ;
98
+ return p ;
99
99
}
100
100
101
101
bool algorithm::overflow (int a, int b, int m, int n) {
102
102
return a >= 0 && b >= 0 && a < m && b < n;
103
103
}
104
- void algorithm::print (std::vector<std::vector<std::string> > &p, int x2, int y2) {
104
+ void algorithm::print (printList &p, int x2, int y2) {
105
105
int x = x2, y = y2;
106
106
int len = 0 ;
107
- while (p[x][y] != " S" ) {
108
- if (p[x][y] == dir[0 ]) {
109
- p[x][y] = " A" ;
107
+ while (p[x][y]. val != " S" ) {
108
+ if (p[x][y]. val == dir[0 ]) {
109
+ p[x][y]. val = " A" ;
110
110
x -= diri[0 ];
111
111
y -= dirj[0 ];
112
112
len++;
113
- } else if (p[x][y] == dir[1 ]) {
114
- p[x][y] = " A" ;
113
+ } else if (p[x][y]. val == dir[1 ]) {
114
+ p[x][y]. val = " A" ;
115
115
x -= diri[1 ];
116
116
y -= dirj[1 ];
117
117
len++;
118
- } else if (p[x][y] == dir[2 ]) {
119
- p[x][y] = " A" ;
118
+ } else if (p[x][y]. val == dir[2 ]) {
119
+ p[x][y]. val = " A" ;
120
120
x -= diri[2 ];
121
121
y -= dirj[2 ];
122
122
len++;
123
- } else if (p[x][y] == dir[3 ]) {
124
- p[x][y] = " A" ;
123
+ } else if (p[x][y]. val == dir[3 ]) {
124
+ p[x][y]. val = " A" ;
125
125
x -= diri[3 ];
126
126
y -= dirj[3 ];
127
127
len++;
128
128
}
129
129
}
130
130
std::cout << " length " << len << std::endl;
131
- p[x2][y2] = " E" ;
131
+ p[x2][y2]. val = " E" ;
132
132
}
0 commit comments