std::vector<Coordinate> SearchMaze(std::vector<std::vector<Color>> maze, const Coordinate start, const Coordinate finish) { std::stack<Coordinate> pstack; std::vector<Coordinate> vertices; std::map<Coordinate, int> color; for (size_t i=0; i<maze.size(); ++i) for (size_t j=0; j<maze[i].size(); ++j) { vertices.push_back( {i, j} ); color[{i, j}] = WHITE; } Graph<Coordinate> g(EdgeType::UNDIRECTED, vertices); // Build graph for (size_t i=0; i<maze.size(); ++i) { for (size_t j=0; j<maze[i].size(); ++j) { if (j < maze[i].size()-1 && maze[i][j] == WHITE && maze[i][j+1] == WHITE) g.add_edge({{i, j}, {i, j+1}}); if (i < maze.size()-1 && maze[i][j] == WHITE && maze[i+1][j] == WHITE) g.add_edge({{i, j}, {i+1, j}}); } } #if 0 std::cout << "adjacent(start) = "; for (const auto &neighbor : g.adj(start)) std::cout << neighbor; std::cout << std::endl; std::cout << "adjacent(finish) = "; for (const auto &neighbor : g.adj(finish)) std::cout << neighbor; std::cout << std::endl; #endif if (start == finish) pstack.push(start); else { for (const auto &neighbor : g.adj(start)) { if (color[neighbor] == WHITE) { if (!SearchDown(g, neighbor, finish, color, pstack)) { pstack.push(start); break; } } } } std::vector<Coordinate> path; path.reserve(pstack.size()); while (!pstack.empty()) { path.push_back(pstack.top()); pstack.pop(); } return path; }
inline void JPSPlus::ExploreFromParentLeft(PathfindingNode * currentNode, DistantJumpPoints * distantJumpPoints) { if (distantJumpPoints->jumpDistance[Up] != 0) SearchUp(currentNode, distantJumpPoints->jumpDistance[Up]); if (distantJumpPoints->jumpDistance[UpLeft] != 0) SearchUpLeft(currentNode, distantJumpPoints->jumpDistance[UpLeft]); if (distantJumpPoints->jumpDistance[Left] != 0) SearchLeft(currentNode, distantJumpPoints->jumpDistance[Left]); if (distantJumpPoints->jumpDistance[DownLeft] != 0) SearchDownLeft(currentNode, distantJumpPoints->jumpDistance[DownLeft]); if (distantJumpPoints->jumpDistance[Down] != 0) SearchDown(currentNode, distantJumpPoints->jumpDistance[Down]); }
inline void JPSPlus::ExploreFromParentAllDirections(PathfindingNode * currentNode, DistantJumpPoints * distantJumpPoints) { SearchDown(currentNode, distantJumpPoints->jumpDistance[Down]); SearchDownRight(currentNode, distantJumpPoints->jumpDistance[DownRight]); SearchRight(currentNode, distantJumpPoints->jumpDistance[Right]); SearchUpRight(currentNode, distantJumpPoints->jumpDistance[UpRight]); SearchUp(currentNode, distantJumpPoints->jumpDistance[Up]); SearchUpLeft(currentNode, distantJumpPoints->jumpDistance[UpLeft]); SearchLeft(currentNode, distantJumpPoints->jumpDistance[Left]); SearchDownLeft(currentNode, distantJumpPoints->jumpDistance[DownLeft]); }
// Hleda vsechny potomky (Pro vypocet hodnot do matice) void CKerNamesMain::SearchDown(CKerName *n, char * Matrix) { // +1 CKerNameList *p= n->childs; int mp; while (p) { mp = p->name->MatrixPos; if (Matrix[mp]==0||Matrix[mp]==2) { if (Matrix[mp]==2) { if (KerMain) KerMain->Errors->LogError(eKRTEcyclusInKSID,0,p->name->GetNameString()); NameInCycle = p->name; } Matrix[mp]++; SearchDown(p->name,Matrix); } p=p->next; } }
bool SearchDown(Graph<Coordinate> &g, Coordinate vertex, Coordinate finish, std::map<Coordinate, int> &color, std::stack<Coordinate> &pstack) { //std::cout << vertex << std::endl; if (vertex == finish) { pstack.push(vertex); return false; } color[vertex] = GRAY; for (const auto &neighbor : g.adj(vertex)) { if (color[neighbor] == WHITE) { if (!SearchDown(g, neighbor, finish, color, pstack)) { pstack.push(vertex); return false; } } } color[vertex] = BLACK; return true; }
// Vytvori Matici // Matice = Pole o velikosti MatrixSize (=Numbers+1) pointeru na ruzne velka pole charu // Slouzi k rychlemu porovnani dvou jmen // Kdyz jsou jmena ve stejnych komponentach, tak informace o jejich vztahu bude v // Matrix[jm1->numer][jm2->MatrixPos]. Viz Funkce Compare void CKerNamesMain::CreateMatrix() { int f; CKerNameList *p; CKerName *root; int size; ClearMatrix(); MatrixSize=Numbers+1; // vytvorim prvni rozmer matice Matrix = new char*[MatrixSize]; for (f=0;f<MatrixSize;f++) Matrix[f]=0; // Smazu informaci o komponente u vsech jmen p=Names; while (p) { p->name->Component=0; p=p->next; } p=Names; // pro vsechny jmena while (p) { root = p->name; if (root->Component==0) { // jmeno dosud nebylo v zadne komponente. Najdu tuto komponenu. // spocitam jeji velikost, jmenum priradim poradova cisla a pridam je do komponenty size = 0; FindComponent(root,root,size); root->matrixsize=size; // nastavim velikost komponenty } else size = root->Component->matrixsize; // prectu si velikost komponenty Matrix[root->number] = new char[size]; // pridam sloupec do matice for (f=0;f<size;f++) Matrix[root->number][f] = 0; SearchDown(root,Matrix[root->number]); SearchUp(root,Matrix[root->number]); // a spocitam jeho hodnoty p=p->next; } MatrixesCalculated = 1; FindMethodsAndParams(); }
inline void JPSPlus::ExploreFromParentDownRight(PathfindingNode * currentNode, DistantJumpPoints * distantJumpPoints) { if (distantJumpPoints->jumpDistance[Down] != 0) SearchDown(currentNode, distantJumpPoints->jumpDistance[Down]); if (distantJumpPoints->jumpDistance[DownRight] != 0) SearchDownRight(currentNode, distantJumpPoints->jumpDistance[DownRight]); if (distantJumpPoints->jumpDistance[Right] != 0) SearchRight(currentNode, distantJumpPoints->jumpDistance[Right]); }