-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
178 lines (168 loc) · 3.28 KB
/
Copy pathsearch.cpp
File metadata and controls
178 lines (168 loc) · 3.28 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
#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;
};
using namespace std;
typedef struct s_info
{
int data;
}*PInfo,Info;
class myclass
{
public:
bool visited[2000];
myclass()
{
memset(visited,0, sizeof(bool) * 2000);
}
//重载(),当前的位置以及当前的步数,填充next
Search<Info,myclass>::e_state operator()(const Info & cur,const int step, const vector<Info> &path,vector<Info>& next)
{
//标记已经走过
visited[cur.data] = true;
if (step > 3) return Search<Info,myclass>::STATE_CONTINUE; //超过指定步数直接继续
if (cur.data == 10)//达到目标点
{
cout <<"got it\n";
cout << step << endl << endl;
for (int i = 0; i < path.size(); ++i)
{
cout << path[i].data << endl;
}
return Search<Info,myclass>::STATE_CONTINUE;
}
else{
//产生下一次遍历的点
int cnt = 0;
for (int i = 0; i < 30; ++i)
{
if (!visited[i])
{
Info t = {i};
next.push_back(t);
cnt++;
if (cnt == 5)
break;
}
}
}
return Search<Info,myclass>::STATE_CONTINUE;
}
};
int main()
{
Search<Info,myclass> s;
Info st;
st.data = 15;
vector<Info> path;
unsigned int step;
if (s.bfs(st, path, step)==Search<Info,myclass>::STATE_REACH_GOAL)
{
cout << step << endl << endl;
for (int i = 0; i < path.size(); ++i)
{
cout << path[i].data << endl;
}
}else{
cout << "cant reach\n";
}
return 0;
}