Другие языки программирования и технологии

Нахождение выхода из лабиринта на языке C++

Готовлю курсовую но никак не могу найти материала. Не знаете где можно найти какую либо информацию, или может кто-нибудь знает на счёт этой темы?
самое грубое - просто сделай рекурсию подставляя в ветви копии лабиринта с перекрытыми уже путями и стенками в сторону пройденного. Экспоненциальные затраты ресурса, Но дубово, напролом и формально правильно.
Антон Ермошин
Антон Ермошин
27 060
Лучший ответ
Смотря какой лабиринт.
Если под выходом подразумевается выход именно наружу (а не попадание в центр) , то по "правилу правой руки" (ну или левой руки) . Все время идем так, чтобы правая рука касалась стенки.
Анатолий .
Анатолий .
54 366
#include <iostream>
#include <deque>
#include <queue>
#include <vector>
#include <climits>
using namespace std;

struct coord {
int x, y;
coord(void):x(0), y(0){}
coord(int _x, int _y):x(_x), y(_y){}

bool operator == (const coord& pt) const {
return ((x == pt.x) && (y == pt.y));
}

bool operator != (const coord& pt) const {
return !(*this == pt);
}
};

// выход из лабиринта поиском в ширину (BFS)
bool is_path(const vector<vector<int> >& m, const coord& start,
const coord& end, deque<coord>* p){

const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}
};

queue<coord> qpos;
vector<int> dist(m.size()*m.size(), INT_MAX);
vector<coord> par(m.size()*m.size());
coord cur;

dist[start.y*(int)m.size() + start.x] = 0;
qpos.push(start);

while(! qpos.empty()) {

cur = qpos.front();
qpos.pop();
if(cur == end)
break;

int sel = cur.y*(int)m.size() + cur.x;
for(int i = 0; i < 8; ++i) {
int x = cur.x + dir[i][0];
int y = cur.y + dir[i][1];
if((x < 0) || (y < 0) || (x >= (int)m.size()) || (y >= (int)m.size()))
continue;
if(m[y][x] != 0)
continue;

int inx = y*(int)m.size() + x;
if(dist[inx] > (dist[sel] + 1)) {
qpos.push(coord(x, y));
dist[inx] = dist[sel] + 1;
par[inx] = cur;
}
}
}

p->clear();
bool ifnd = false;
if(cur == end) {
cur = end;
p->push_front(cur);
while(cur != start) { // выделяем путь
p->push_front(par[cur.y*(int)m.size()+cur.x]);
cur = par[cur.y*(int)m.size()+cur.x];
}
ifnd = true;
}
par.clear();
dist.clear();
return ifnd;
}

int main(void){
//лабиринт: 0-свободный ход, 1-стена
const char s[] = {
"00000010000000"\
"00100010000000"\
"01101100010000"\
"00000000100000"\
"00101000000000"\
"00001001101110"\
"00100010000010"\
"00000000000000"\
"00010000000101"\
"00101001110101"\
"10100100000100"\
"00000000000110"\
"11010100000000"\
"00000100111000"
};

const size_t N = 14;
vector<vector<int> > m(N);
for(size_t r = 0; r < N; ++r){
for(size_t c = 0; c < N; ++c)
m[r].push_back((int)(s[r*N+c] - '0'));
}

deque<coord> p;
if(is_path(m, coord(0, 0), coord(12, 12), &p)){

while(! p.empty()){
m[p.front().y][p.front().x] = 2;
p.pop_front();
}
cout << "Yes find exit maze." << endl;
} else
cout << "Not find exit maze !!!" << endl;

// выводим лабиринт
const unsigned char chs[3] = { 0xB0, 0xDB, '\2' };
for(size_t i = 0; i < m.size(); ++i){
for(size_t j = 0; j < m[i].size(); ++j)
cout.put(chs[m[i][j]]);
cout << endl;
}
return 0;
}
Ник Пабедитель
Ник Пабедитель
11 372
Поиск пути A*(A star)
Все зависит от того как вы представляете решение (чисто техническое) , начните главвное