Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2, Sig, Sigo 2 years ago
parent a7f729d41a
commit 89974bc0ef
  1. BIN
      C++ProjectTemplate
  2. 82
      input
  3. 87
      main.cpp

Binary file not shown.

82
input

@ -1,41 +1,41 @@
abacccaaaacccccccccccaaaaaacccccaaaaaaccccaaacccccccccccccccccccccccccccccccccccccccccccaaaaa abacccaaaacccccccccccaaaaaacccccadaaaaccccaaacccccccccccccccccccccccccccccccccccccccccccaaaaa
abaaccaaaacccccccccccaaaaaaccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa abaaccaaaacccccccccccaaaaaaccccccdaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa
abaaccaaaacccccccccccaaaaacccccaaaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa abaaccaaaacccccccccccaaaaacccccaadaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccaaaaa
abccccccccccccccccccccaaaaacccaaaaaaaaaaaaaaaacccccccccccccccccccccccccccaaaccccccccccccaaaaa abccccccccccccccccccccaaaaacccaaadaaaaaaaaaaaacccccccccccccccccccccccccccaaaccccccccccccaaaaa
abccccccccccccccccccccaacaacccaaaaaaaaccaaaaaccccccccccccccccccccccccccccaaaccccccccccccaccaa abccccccccccccccccccccaacaacccaaadaaaaccaaaaaccccccccccccccccccccccccccccaaaccccccccccccaccaa
abcccccccccccccaacccaaaccccccaaaaaaaaaccaaaaaccccccccccccccccccccccccccccccacccccccccccccccca abcccccccccccccaacccaaaccccccaaaadaaaaccaaaaaccccccccccccccccccccccccccccccacccccccccccccccca
abcccccccccccaaaaaaccaaaccacccccaaaaaaacccccccccccccccccccccccccciiiicccccccddddddccccccccccc abcccccccccccaaaaaaccaaaccacccccadaaaaacccccccccccccccccccccccccciiiicccccccddddddccccccccccc
abcccccccccccaaaaaaccaaaaaaaccccaaaaaacccccaacccccccaaaccccccccciiiiiiiicccdddddddddacaaccccc abcccccccccccaaaaaaccaaaaaaaccccadaaaacccccaacccccccaaaccccccccciiiiiiiicccdddddddddacaaccccc
abccccccccccccaaaaaaaaaaaaacccccaaaaaaacaaaacccccccaaaacccccccchhiiiiiiiiicddddddddddaaaccccc abccccccccccccaaaaaaaaaaaaacccccadaaaaacaaaacccccccaaaacccccccchhiiiiiiiiicddddddddddaaaccccc
abcccccccccccaaaaaaaaaaaaaacccccccaaacccaaaaaacccccaaaaccccccchhhipppppiiiijjjjjjjddddaaccccc abcccccccccccaaaaaaaaaaaaaaccccccdaaacccaaaaaacccccaaaaccccccchhhipppppiiiijjjjjjjddddaaccccc
abcccccccccccaaaaaaaaaaaaaaccccccccccccccaaaaaccccccaaaccccccchhhpppppppiijjjjjjjjjddeeaccccc abcccccccccccaaaaaaaaaaaaaaccccccdcccccccaaaaaccccccaaaccccccchhhpppppppiijjjjjjjjjddeeaccccc
abcccccccccccccccccaaaaaaaacccccccccccccaaaaaccccccccccccccccchhppppppppppjjqqqjjjjjeeeaacccc abcccccccccccccccccaaaaaaaaccccccdccccccaaaaaccccccccccccccccchhppppppppppjjqqqjjjjjeeeaacccc
abccccccccccccccccccaaaaaaaacccccccccccccccaacccccccccccccccchhhpppuuuupppqqqqqqqjjjeeeaacccc abccccccccccccccccccaaaaaaaacccccdcccccccccaacccccccccccccccchhhpppuuuupppqqqqqqqjjjeeeaacccc
abcccccccccccccccccccaacccacccccccccccccccccccccccccccccccccchhhopuuuuuuppqqqqqqqjjjeeecccccc abcccccccccccccccccccaacccaccccccdccccccccccccccccccccccccccchhhopuuuuuuppqqqqqqqjjjeeecccccc
abacccccccccccccaaacaaaccccccccccccccccccccccccccccaaccccccchhhhoouuuuuuuqvvvvvqqqjkeeecccccc abacccccccccccccaaacaaaccccccccccdcccccccccccccccccaaccccccchhhhoouuuuuuuqvvvvvqqqjkeeecccccc
abaccccccccccccaaaaaacccccaaccccccccccccccccccccccaaaccccccchhhooouuuxxxuvvvvvvqqqkkeeecccccc abaccccccccccccaaaaaacccccaacccccdccccccccccccccccaaaccccccchhhooouuuxxxuvvvvvvqqqkkeeecccccc
abaccccccccccccaaaaaacccaaaaaaccccccccccccccccccaaaaaaaaccchhhhooouuxxxxuvyyyvvqqqkkeeecccccc abaccccccccccccaaaaaacccaaaaaacccdccccccccccccccaaaaaaaaccchhhhooouuxxxxuvyyyvvqqqkkeeecccccc
abcccccccccccccaaaaacccaaaaaaaccccccccccccccccccaaaaaaaaccjjhooooouuxxxxyyyyyvvqqqkkeeecccccc abcccccccccccccaaaaacccaaaaaaacccdccccccccccccccaaaaaaaaccjjhooooouuxxxxyyyyyvvqqqkkeeecccccc
abccccccccccccccaaaaaacaaaaaaaccccccccaaaccccccccaaaaaaccjjjooootuuuxxxxyyyyyvvqqkkkeeecccccc abccccccccccccccaaaaaacaaaaaaacccdccccaaaccccccccaaaaaaccjjjooootuuuxxxxyyyyyvvqqkkkeeecccccc
abccccccccccccccaaaaaaaaaaaaacccccccccaaaacccccccaaaaaacjjjooootttuxxxxxyyyyvvrrrkkkeeecccccc abccccccccccccccaaaaaaaaaaaaaccccdccccaaaacccccccaaaaaacjjjooootttuxxxxxyyyyvvrrrkkkeeecccccc
SbccccccccccccccccccaaaaaaaaacccccccccaaaacccccccaaaaaacjjjoootttxxxEzzzzyyvvvrrrkkkfffcccccc SbccccccccccccccccccaaaaaaaaaccccdccccaaaacccccccaaaaaacjjjoootttxxxEzzzzyyvvvrrrkkkfffcccccc
abcccccccccccaaacccccaaaaaaacaaaccccccaaaccccccccaaccaacjjjoootttxxxxxyyyyyyvvvrrkkkfffcccccc abcccccccccccaaacccccaaaaaaacaaacdccccaaaccccccccaaccaacjjjoootttxxxxxyyyyyyvvvrrkkkfffcccccc
abcccccccccaaaaaacccaaaaaacccaaacacccaacccccccccccccccccjjjoootttxxxxyxyyyyyywvvrrkkkfffccccc abcccccccccaaaaaacccaaaaaacccaaacdcccaacccccccccccccccccjjjoootttxxxxyxyyyyyywvvrrkkkfffccccc
abcccccccccaaaaaacccaaaaaaaaaaaaaaaccaaacaaacccccaacccccjjjnnnttttxxxxyyyyyyywwwrrkkkfffccccc abcccccccccaaaaaacccaaaaaaaaaaaaadaccaaacaaacccccaacccccjjjnnnttttxxxxyyyyyyywwwrrkkkfffccccc
abcaacacccccaaaaacccaaacaaaaaaaaaaaccaaaaaaacccccaacaaacjjjnnnntttttxxyywwwwwwwwrrrlkfffccccc abcaacacccccaaaaacccaaacaaaaaaaaadaccaaaaaaacccccaacaaacjjjnnnntttttxxyywwwwwwwwrrrlkfffccccc
abcaaaaccccaaaaacccccccccaacaaaaaaccccaaaaaacccccaaaaacccjjjnnnnnttttwwywwwwwwwrrrrllfffccccc abcaaaaccccaaaaacccccccccaacaaaaadccccaaaaaacccccaaaaacccjjjnnnnnttttwwywwwwwwwrrrrllfffccccc
abaaaaaccccaaaaaccccccaaaaaccaaaaacaaaaaaaaccccaaaaaaccccjjjjinnnntttwwwwwsssrrrrrllllffccccc abaaaaaccccaaaaaccccccaaaaaccaaaadcaaaaaaaaccccaaaaaaccccjjjjinnnntttwwwwwsssrrrrrllllffccccc
abaaaaaaccccccccccccccaaaaacaaaaaacaaaaaaaaacccaaaaaaacccciiiiinnnntswwwwssssrrrrrlllfffccccc abaaaaaaccccccccccccccaaaaacaaaaadcaaaaaaaaacccaaaaaaacccciiiiinnnntswwwwssssrrrrrlllfffccccc
abacaaaaccccccccccccccaaaaaacaaccccaaaaaaaaaaccccaaaaaaccccciiiinnnssswwsssssllllllllfffccccc abacaaaaccccccccccccccaaaaaacaaccdcaaaaaaaaaaccccaaaaaaccccciiiinnnssswwsssssllllllllfffccccc
abccaaccccccccccccccccaaaaaaccccccccccaaacaaaccccaaccaacccccciiiinnsssssssmmllllllllfffaacccc abccaaccccccccccccccccaaaaaacccccdccccaaacaaaccccaaccaacccccciiiinnsssssssmmllllllllfffaacccc
abccccccccccccccccccccaaaaaaccccccccccaaaccccccccaaccccccccccciiinnmsssssmmmmlllllgggffaacccc abccccccccccccccccccccaaaaaacccccdccccaaaccccccccaaccccccccccciiinnmsssssmmmmlllllgggffaacccc
abcccccccccccccccaccccccaaacccccccccccaaccccccccccccccccccccccciiimmmsssmmmmmgggggggggaaacccc abcccccccccccccccaccccccaaaccccccdccccaaccccccccccccccccccccccciiimmmsssmmmmmgggggggggaaacccc
abcccccccccaaaaaaaaccccccccccccccccccccccccccccaaaaaccccccccccciiimmmmmmmmmgggggggggaaacccccc abcccccccccaaaaaaaaccccccccccccccdcccccccccccccaaaaaccccccccccciiimmmmmmmmmgggggggggaaacccccc
abccccccccccaaaaaaccccccccccccccccccaacccccccccaaaaacccccccccccciiimmmmmmmhhggggcaaaaaaaccccc abccccccccccaaaaaacccccccccccccccdccaacccccccccaaaaacccccccccccciiimmmmmmmhhggggcaaaaaaaccccc
abccccccccccaaaaaacccccccccccccccccaacccccccccaaaaaacccccccccccciihhmmmmhhhhgccccccccaacccccc abccccccccccaaaaaacccccccccccccccdcaacccccccccaaaaaacccccccccccciihhmmmmhhhhgccccccccaacccccc
abccccaacaaaaaaaaaaccccccccccccccccaaaccccccccaaaaaaccccccccccccchhhhhhhhhhhaaccccccccccccccc abccccaacaaaaaaaaaaccccccccccccccdcaaaccccccccaaaaaaccccccccccccchhhhhhhhhhhaaccccccccccccccc
abccccaaaaaaaaaaaaaaccccccccccaaccaaaaccccccccaaaaaacccaaacccccccchhhhhhhhaaaaccccccccccccccc abccccaaaaaaaaaaaaaaccccccccccaacdaaaaccccccccaaaaaacccaaacccccccchhhhhhhhaaaaccccccccccccccc
abcccaaaaaaaaaaaaaaaccccccccaaaaaacaaaacacaccccaaaccccaaaacccccccccchhhhccccaaccccccccccaaaca abcccaaaaaaaaaaaaaaaccccccccaaaaadcaaaacacaccccaaaccccaaaacccccccccchhhhccccaaccccccccccaaaca
abcccaaaaaacacaaacccccccccccaaaaaaaaaaaaaaacccccccccccaaaacccccccccccaaaccccccccccccccccaaaaa abcccaaaaaacacaaacccccccccccaaaaadaaaaaaaaacccccccccccaaaacccccccccccaaaccccccccccccccccaaaaa
abcccccaaaacccaaaccccccccccaaaaaaaaaaaaaaaaccccccccccccaaacccccccccccaaacccccccccccccccccaaaa abcccccaaaacccaaaccccccccccaaaaaadaaaaaaaaaccccccccccccaaacccccccccccaaacccccccccccccccccaaaa
abcccccaacccccaacccccccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaa abcccccaacccccaacccccccccccaaaaaadaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaa

@ -32,8 +32,9 @@ struct node{
vi2d playerPos={0,0}; vi2d playerPos={0,0};
vi2d endingPos={0,0}; vi2d endingPos={0,0};
std::vector<std::vector<node>>grid; std::vector<std::vector<node>>grid;
std::vector<node*>possibleStartingLocs;
node*startNode; node*startNode;
node*endNode; node *endNode;
int main() int main()
{ {
@ -61,6 +62,9 @@ int main()
for (int x=0;x<grid[y].size();x++){ for (int x=0;x<grid[y].size();x++){
std::cout<<grid[y][x].getElevation(); std::cout<<grid[y][x].getElevation();
grid[y][x].pos={x,y}; grid[y][x].pos={x,y};
if (grid[y][x].getElevation()=='a'){
possibleStartingLocs.push_back(&grid[y][x]);
}
if (x>0&&grid[y][x-1].getElevation()-1<=grid[y][x].getElevation()){ if (x>0&&grid[y][x-1].getElevation()-1<=grid[y][x].getElevation()){
grid[y][x].connections.push_back(&grid[y][x-1]); grid[y][x].connections.push_back(&grid[y][x-1]);
} }
@ -75,47 +79,70 @@ int main()
} }
if (x==playerPos.x&&y==playerPos.y){ if (x==playerPos.x&&y==playerPos.y){
startNode=&grid[y][x]; startNode=&grid[y][x];
std::cout<<"Starting node set."<<std::endl; //std::cout<<"Starting node set."<<std::endl;
} }
if (x==endingPos.x&&y==endingPos.y){ if (x==endingPos.x&&y==endingPos.y){
endNode=&grid[y][x]; endNode = &grid[y][x];
std::cout<<"Ending node set."<<std::endl; //std::cout << "Ending node set." << std::endl;
} }
} }
std::cout<<std::endl; std::cout<<std::endl;
} }
std::vector<node*>searchNodes; int bestPath=INFINITY;
searchNodes.push_back(startNode); node*testNode;
startNode->myDist=0.0f; for (int l=0;l<possibleStartingLocs.size();l++){
while (searchNodes.size()>0){ testNode=possibleStartingLocs[l];
node*currentNode=searchNodes.front(); //std::cout<<"Checking location: "<<testNode->pos<<std::endl;
if (currentNode->visited){ for (int y=0;y<grid.size();y++){
searchNodes.erase(searchNodes.begin()); for (int x=0;x<grid[y].size();x++){
continue; node&n=grid[y][x];
n.visited=false;
n.myDist=INFINITY;
n.parent=nullptr;
}
} }
currentNode->visited=true; std::vector<node*>searchNodes;
for (int i=0;i<currentNode->connections.size();i++){ searchNodes.push_back(testNode);
node*neighbor=currentNode->connections[i]; testNode->myDist=0.0f;
if (!neighbor->visited){ while (searchNodes.size()>0){
searchNodes.push_back(neighbor); node*currentNode=searchNodes.front();
if (currentNode->visited){
searchNodes.erase(searchNodes.begin());
continue;
} }
float lowerGoal=currentNode->myDist+pow(neighbor->pos.x-currentNode->pos.x,2)+pow(neighbor->pos.y-currentNode->pos.y,2); currentNode->visited=true;
if (lowerGoal<neighbor->myDist){ for (int i=0;i<currentNode->connections.size();i++){
neighbor->parent=currentNode; node*neighbor=currentNode->connections[i];
neighbor->myDist=lowerGoal; if (!neighbor->visited){
searchNodes.push_back(neighbor);
}
float lowerGoal=currentNode->myDist+pow(neighbor->pos.x-currentNode->pos.x,2)+pow(neighbor->pos.y-currentNode->pos.y,2);
if (lowerGoal<neighbor->myDist){
neighbor->parent=currentNode;
neighbor->myDist=lowerGoal;
}
}
}
node *searchNode = endNode;
bool completed=false;
int jumps=0;
while (searchNode!=testNode){
if (searchNode->parent!=nullptr){
//std::cout<<"Node goes from "<<searchNode->pos<<" to "<<searchNode->parent->pos<<std::endl;
jumps++;
searchNode=searchNode->parent;
}else{
break;
}
}
if (searchNode==testNode){
if (jumps<bestPath){
std::cout<<"New Shortest path is "<<jumps<<" hops"<<std::endl;
bestPath=jumps;
} }
} }
} }
node*searchNode=endNode;
int jumps=0;
while (searchNode!=startNode){
std::cout<<"Node goes from "<<searchNode->pos<<" to "<<searchNode->parent->pos<<std::endl;
jumps++;
searchNode=searchNode->parent;
}
std::cout<<"Shortest path is "<<jumps<<" hops"<<std::endl;
return 0; return 0;
} }

Loading…
Cancel
Save