-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
215 lines (194 loc) · 6.03 KB
/
main.cpp
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
// Program to find shortest path using BFS Algorithm
#include<iostream>
#include <algorithm>
#include <list>
#define OUT
struct coord{
int x;
int y;
};
bool operator==(const coord& lhs, const coord& rhs)
{
if(lhs.x == rhs.x && lhs.y == rhs.y)
return true;
else
return false;
}
std::ostream& operator << (std::ostream& stream, coord &p)
{
stream << p.x<<"/"<<p.y;
return stream;
}
class graph{
private:
int **map;
int **distances;
coord **sources;
coord dim;
public:
graph(const int maxYpassed,const int maxXpassed, const unsigned char* pMap){
dim.x = maxXpassed;
dim.y = maxYpassed;
map = new int*[dim.y];
distances = new int*[dim.y];
sources = new coord*[dim.y];
int index = 0;
for(int y = 0; y < dim.y; y++) {
map[y] = new int[dim.x];
distances[y] = new int[dim.x];
sources[y] = new coord[dim.x];
for (int x = 0; x < dim.x; x++) {
map[y][x] = static_cast<int>(pMap[index]);
distances[y][x] = std::numeric_limits<int>::max();
sources[y][x] = coord{-1,-1};
index++;
}
}
}
~graph(){
for(int i =0 ; i < dim.y; i++) {
delete[](map[i]);
delete[](distances[i]);
delete[](sources[i]);
}
delete[](map);
delete[](distances);
delete[](sources);
}
int getMap(coord c) const {
return map[c.y][c.x];
}
int getDistance(coord c) const {
return distances[c.y][c.x];
}
void setDistance(coord c, int distance) {
graph::distances[c.y][c.x] = distance;
}
coord getSource(coord c) const {
return sources[c.y][c.x];
}
void setSource(coord c, coord source) {
graph::sources[c.y][c.x] = source;
}
int indexTransform(coord c){
return c.x+c.y*dim.x;
}
void findAdj(coord c, std::list<coord>* ret){
coord check;
if(c.x+1 < dim.x){
check = {c.x+1, c.y};
if(getMap(check) == 1)
ret->push_back(check);}
if(c.x-1 >= 0){
check = {c.x-1, c.y};
if(getMap(check) == 1)
ret->push_back(check);}
if(c.y+1 < dim.y){
check = {c.x, c.y+1};
if(getMap(check) == 1)
ret->push_back(check);}
if(c.y-1 >= 0){
check = {c.x, c.y-1};
if(getMap(check) == 1)
ret->push_back(check);}
}
};
int FindPath(const int nStartX, const int nStartY,
const int nTargetX, const int nTargetY,
const unsigned char* pMap, const int nMapWidth, const int nMapHeight,
int* pOutBuffer, const int nOutBufferSize){
coord start = coord{nStartX, nStartY};
coord target = coord{nTargetX, nTargetY};
graph* fico = new graph(nMapHeight, nMapWidth, pMap);
fico->setSource(start, start);
fico->setDistance(start, 0);
std::list<coord> queue; //TODO: let queue be a set (no duplicates)
queue.push_back(start);
// Here I'm going to implement BFS algorithm:
// watching for each "queue" nodes,
// if watched node is the target, exit
// else, take adjacents, there, update adj distances
// and parents if needed (I'm looking for the sortest path
while(!queue.empty()){
coord vertex = queue.front();
queue.pop_front();
if(vertex == target){
break;
} else{
std::list<coord> adj;
fico->findAdj(vertex, OUT &adj);
for (long i = adj.size(); i > 0 ; i--) {
coord nextVertex = adj.front();
adj.pop_front();
if(fico->getSource(nextVertex) == vertex){
continue;
}
else if(fico->getDistance(nextVertex) > fico->getDistance(vertex)+1)
{
fico->setDistance(nextVertex, fico->getDistance(vertex)+1);
fico->setSource(nextVertex, vertex);
adj.push_back(nextVertex);
}
}
queue.splice(queue.end(), adj);
}
}
// KNOW WHAT I HAVE (print "origin{source}index")
// for (int i = 0; i < nMapHeight; ++i) {
// for (int j = 0; j < nMapWidth; ++j) {
// coord ori = coord{j,i};
// coord next = fico->getSource(ori);
// if ( next == coord{-1,-1}){
// std::cerr<<"0 ";
// } else
// if (next.x < ori.x){
// std::cerr<<"← ";
// } else
// if (next.x > ori.x){
// std::cerr<<"→ ";
// } else
// if (next.y < ori.y){
// std::cerr<<"↑ ";
// } else
// if (next.y > ori.y){
// std::cerr<<"↓ ";
// }
// }
// std::cerr<<"\n";
// }
// if BFS doesn't enrich the solution
if(fico->getSource(target) == coord{-1,-1})
return -1;
// now I've climb my solution saved into my graph
coord support = target;
std::list<int> street;
while(true){
if(support == start) break;
street.push_back(fico->indexTransform(support));
support = fico->getSource(support);
}
int solLenght = fico->getDistance(target);//static_cast<int>(street.size()); they store the same information
// if my street is longer that your buffer
if(solLenght > nOutBufferSize)
return solLenght;
// the solution has to be reversed
int index = 0;
for (long j = street.size(); j > 0; j--) {
pOutBuffer[index] = street.back();
street.pop_back();
index++;
}
return solLenght;
}
int main(){
unsigned char pMap[] = {0, 0, 1, 0, 1, 1, 1, 0, 1};
const int buffdim = 7;
int pOutBuffer[buffdim];
int len = FindPath(2, 0, 0, 2, pMap, 3, 3, pOutBuffer, 7);
if(len != -1)
for (int i = 0; i < 48; ++i) {
std::cout<<pOutBuffer[i]<<" ";
}
std::cout<<std::endl;
return len;
}