-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_line.cpp
More file actions
272 lines (259 loc) · 6.55 KB
/
Copy pathtest_line.cpp
File metadata and controls
272 lines (259 loc) · 6.55 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <iostream>
#include <queue>
#include <stack>
template <class T, class cmp>
class Search
{
public:
typedef enum
{
STATE_CONTINUE,
STATE_REACH_GOAL,
STATE_UNREACH
}e_state;
Search(){};
//广度优先搜索,返回路径和最短距离
e_state bfs(const T &start,std::vector<T> &path, unsigned int &step)
{
Info first;
first.data = start;
first.step = 0;
std::queue<Info> q;
q.push(first);
cmp mycmp;
Info tmp_new;
while (!q.empty())
{
Info cur = q.front();
q.pop();
std::vector<T> next;
e_state state = mycmp(cur.data, cur.step, cur.path, next);
switch (state)
{
case STATE_CONTINUE:
for (int i = 0; i < next.size(); ++i)
{
tmp_new.step = cur.step + 1;
tmp_new.path = cur.path;
tmp_new.path.push_back(cur.data);
tmp_new.data = next[i];
q.push(tmp_new);
}
break;
case STATE_REACH_GOAL:
step = cur.step;
path = cur.path;
path.push_back(cur.data);
return state;
break;
default:
break;
}
}
return STATE_UNREACH;
}
//深度优先搜索
e_state dfs(const T &start,std::vector<T> &path, unsigned int &step)
{
Info first;
first.data = start;
first.step = 0;
std::stack<Info> q;
q.push(first);
cmp mycmp;
Info tmp_new;
while (!q.empty())
{
Info cur = q.top();
q.pop();
std::vector<T> next;
e_state state = mycmp(cur.data,cur.step, cur.path, next);
switch (state)
{
case STATE_CONTINUE:
for (int i = 0; i < next.size(); ++i)
{
tmp_new.step = cur.step + 1;
tmp_new.path = cur.path;
tmp_new.path.push_back(cur.data);
tmp_new.data = next[i];
q.push(tmp_new);
}
break;
case STATE_REACH_GOAL:
step = cur.step;
path = cur.path;
path.push_back(cur.data);
return state;
break;
default:
break;
}
}
return STATE_UNREACH;
}
private:
typedef struct s_tmp
{
int step;
std::vector<T> path;
T data;
}*PInfo,Info;
};
#include <map>
using namespace std;
typedef struct
{
map<unsigned int,vector <int> > next;//(lineID,NextStation)
}*PNode,Node;
typedef pair<unsigned int,Node> PAIR_NODE; //(stationID,Next数组)
typedef pair<unsigned int,unsigned int> PAIR_LINE;//(lineID,startStationID)
map<unsigned int,Node> g_map_nodes; //(nodeID,附近的站点)
map<unsigned int,unsigned int> g_map_lines; //(线路号,开始的站点号)
vector< vector<unsigned int> > g_total_paths; //保存找到的路径
typedef struct
{
int cur;
}*PInfo,Info;
int g_goal;
int g_max_steps;
class myclass
{
public:
bool visited[10000];
int maxSteps;
myclass()
{
memset(visited,0,sizeof(bool) * 10000);
maxSteps = 10000;
}
Search<Info,myclass>::e_state operator()(const Info &cur, int step,const vector<Info> &path,vector<Info> &next)
{
visited[cur.cur] = true;
if (step > maxSteps) return Search<Info,myclass>::STATE_CONTINUE;
if (cur.cur == g_goal)
{ //达到了目标站点
cout <<"got it ! steps = "<< step <<endl;
maxSteps = step;
g_max_steps = step; //记录一下最大的步数
vector<unsigned int> tmp;
for (int i = 0; i < path.size(); ++i)
{
tmp.push_back(path[i].cur);
}
tmp.push_back(g_goal); //最后一个的目标站点
g_total_paths.push_back(tmp); //增加一条路径
cout <<"\n";
return Search<Info,myclass>::STATE_CONTINUE; //由于需要所有的路径,所以需要再继续搜索
}else{
Node node = g_map_nodes[cur.cur];
for (map<unsigned int,vector <int> >::iterator itr = node.next.begin(); itr != node.next.end(); ++itr)
{ //遍历所有的线路
for(int i = 0; i < (*itr).second.size();++i)
{
//遍历每一条线路上的相邻站点
unsigned int next_station = (*itr).second[i];
if (!visited[next_station])
{
Info tmp = {next_station};
next.push_back(tmp);
}
}
}
return Search<Info,myclass>::STATE_CONTINUE;
}
}
};
void AddLine(unsigned int LineNo, unsigned int StationNum ,unsigned int *pStationArray)
{
g_map_lines[LineNo] = *pStationArray;
for (int i = 1; i < StationNum; ++i)
{
unsigned int cur_station = *(pStationArray + i);
unsigned int last_station = *(pStationArray + i -1);
PNode cur_node = &(g_map_nodes[cur_station]);
cur_node->next[LineNo].push_back(last_station);
PNode last_node = &(g_map_nodes[last_station]);
last_node->next[LineNo].push_back(cur_station);
}
}
void Clear()
{
g_map_nodes.clear();
g_map_lines.clear();
}
int CalcMinPathLen(unsigned int SrcStation, unsigned int DesStation)
{
Search<Info,myclass> s;
vector<Info> path;
Info start = {SrcStation};
unsigned int steps;
g_goal = DesStation;
s.bfs(start, path, steps);
return steps;
}
int SearchMinPathes(unsigned int SrcStation,
unsigned int DesStation,
unsigned int* pPathNum,
unsigned int* pPathLen,
unsigned int **ppPathes)
{
g_total_paths.clear();
CalcMinPathLen(SrcStation,DesStation);
*pPathNum = g_total_paths.size();
*pPathLen = g_max_steps;
for (int i = 0; i < *pPathNum; ++i)
{
for (int j = 0; j < g_total_paths[i].size(); ++j)
{
ppPathes[i][j] = g_total_paths[i][j];
}
}
}
int main()
{
unsigned int line[] = {1,2,3,4,5};
AddLine(1,sizeof(line) / sizeof(unsigned int),line);
unsigned int line2[] = {1,10,9,7,6};
AddLine(2,sizeof(line2) / sizeof(unsigned int),line2);
unsigned int line3[] = {5,7,8};
AddLine(3,sizeof(line3) / sizeof(unsigned int),line3);
unsigned int line4[] = {11,5};
AddLine(4,sizeof(line4) / sizeof(unsigned int),line4);
for (map<unsigned int,Node>::iterator itr = g_map_nodes.begin(); itr != g_map_nodes.end(); ++itr)
{
cout << "cur node = " << (*itr).first << endl << endl;
Node node = (*itr).second;
for (map<unsigned int,vector <int> >::iterator j = node.next.begin(); j != node.next.end(); ++j)
{
cout << "at line "<<(*j).first<<endl;
for (int ii = 0; ii < (*j).second.size(); ++ii)
cout << "next " <<(*j).second[ii]<<endl;
//cout << "next "<<(*j).second[1]<<endl;
}
}
cout << CalcMinPathLen(1,9) << endl;
unsigned int pathNums;
unsigned int pathLen;
unsigned int **paths = (unsigned int**)malloc(sizeof(unsigned int*) * 100);
for (int i = 0; i < 100; ++i)
{
/* code */
paths[i] = (unsigned int*)malloc(sizeof(unsigned int)*200);
}
SearchMinPathes(1,11,&pathNums,&pathLen,paths);
cout << "pathNums " << pathNums << endl;
cout << "pathLens " << pathLen << endl;
for (int i = 0; i < pathNums; ++i)
{
int j = 0;
cout << "path " << i << endl;
while (paths[i][j] != 11)
{
cout << paths[i][j++]<<endl;
}
cout << paths[i][j] << endl;
}
Clear();
return 0;
}