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
| #include <iostream>
#include <vector>
#include <queue>
#include <limits>
#define MAZE_N 24
struct point {
int x, y;
point(void):x(0), y(0){}
point(int _x, int _y):x(_x), y(_y){}
bool operator == (const point& p) const {
return ((x == p.x) && (y == p.y));
}
bool operator != (const point& p) const {
return !(*this == p);
}
};
struct vertex {
int dist;
point parent;
vertex(void):dist(0){}
vertex(int _dist):dist(_dist){}
};
struct path {
int dist;
point ver;
path(void): dist(0){}
path(int _dist, const point& _ver): dist(_dist), ver(_ver){}
bool operator > (const path& p) const {
return (dist > p.dist);
}
};
#define dist_coord(x1, y1, x2, y2) (abs((x2) - (x1)) + abs((y2) - (y1))) * 10
//алгоритм Дейкстры для поиска кратчайшего пути в лабиринте
bool shortestD(int mas[][MAZE_N], const point& start, const point& end, std::vector<point>& pos){
const int dirs[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
std::priority_queue<path, std::vector<path>, std::greater<path> > pqs;
std::vector<point> parent(MAZE_N*MAZE_N);
std::vector<int> dist(MAZE_N*MAZE_N, std::numeric_limits<int>::max());
int d, x, y, px, py;
point v;
d = dist_coord(start.x, start.y, end.x, end.y);
dist[start.y*MAZE_N + start.x] = d;
bool ok = false;
pqs.push( path(d, start) );
while(! pqs.empty()){
v = pqs.top().ver;
d = pqs.top().dist;
pqs.pop();
if(v == end){
ok = true;
break;
} else if(d > dist[v.y*MAZE_N + v.x])
continue;
for(int i = 0; i < 4; ++i){
px = v.x + dirs[i][0];
py = v.y + dirs[i][1];
if((px < 0) || (py < 0) || (px >= MAZE_N) || (py >= MAZE_N) || (mas[py][px] == 1))
continue;
d = dist_coord(px, py, end.x, end.y);
x = py *MAZE_N + px;
y = v.y*MAZE_N + v.x;
if(dist[x] > (dist[y] + d)){
dist[x] = dist[y] + d;
parent[x] = v;
pqs.push( path(dist[x], point(px, py)) );
}
}
}
if(ok){
pos.push_back(end);
for(v = end; v != start; v = parent[v.y*MAZE_N + v.x])
pos.insert(pos.end(), parent[v.y*MAZE_N + v.x]);
}
parent.clear();
dist.clear();
return ok;
}
int main(void){
int i, j;
int mas[MAZE_N][MAZE_N] = {
{0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0},
{0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
//2-это начальная точка, 3-конечная точка пути
point start, end;
for(i = 0; i < MAZE_N; ++i){
for(j = 0; j < MAZE_N; ++j){
if(mas[i][j] == 2){
start.x = j;
start.y = i;
} else if(mas[i][j] == 3){
end.x = j;
end.y = i;
}
}
}
//найти путь от start до end
std::vector<point> pos;
if(! shortestD(mas, start, end, pos)){
std::cout << "путь не найден!" << std::endl;
return 1;
}
//пометить найденный путь
std::vector<point>::const_iterator it = pos.begin();
while(it != pos.end()){
mas[it->y][it->x] = 'X' - '0';
++it;
}
//вывести лабиринт
for(i = 0; i < MAZE_N; ++i){
for(j = 0; j < MAZE_N; ++j)
std::cout.put(mas[i][j] + '0');
std::cout << std::endl;
}
return 0;
} |