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. 43
      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,6 +32,7 @@ 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;
@ -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,19 +79,32 @@ 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;
} }
int bestPath=INFINITY;
node*testNode;
for (int l=0;l<possibleStartingLocs.size();l++){
testNode=possibleStartingLocs[l];
//std::cout<<"Checking location: "<<testNode->pos<<std::endl;
for (int y=0;y<grid.size();y++){
for (int x=0;x<grid[y].size();x++){
node&n=grid[y][x];
n.visited=false;
n.myDist=INFINITY;
n.parent=nullptr;
}
}
std::vector<node*>searchNodes; std::vector<node*>searchNodes;
searchNodes.push_back(startNode); searchNodes.push_back(testNode);
startNode->myDist=0.0f; testNode->myDist=0.0f;
while (searchNodes.size()>0){ while (searchNodes.size()>0){
node*currentNode=searchNodes.front(); node*currentNode=searchNodes.front();
if (currentNode->visited){ if (currentNode->visited){
@ -107,15 +124,25 @@ int main()
} }
} }
} }
node *searchNode = endNode; node *searchNode = endNode;
bool completed=false;
int jumps=0; int jumps=0;
while (searchNode!=startNode){ while (searchNode!=testNode){
std::cout<<"Node goes from "<<searchNode->pos<<" to "<<searchNode->parent->pos<<std::endl; if (searchNode->parent!=nullptr){
//std::cout<<"Node goes from "<<searchNode->pos<<" to "<<searchNode->parent->pos<<std::endl;
jumps++; jumps++;
searchNode=searchNode->parent; searchNode=searchNode->parent;
}else{
break;
}
}
if (searchNode==testNode){
if (jumps<bestPath){
std::cout<<"New Shortest path is "<<jumps<<" hops"<<std::endl;
bestPath=jumps;
}
}
} }
std::cout<<"Shortest path is "<<jumps<<" hops"<<std::endl;
return 0; return 0;
} }

Loading…
Cancel
Save