|
|
|
@ -1,20 +1,31 @@ |
|
|
|
|
#define OLC_PGE_APPLICATION |
|
|
|
|
#include "pixelGameEngine.h" |
|
|
|
|
#include "olcutils.h" |
|
|
|
|
#include <vector> |
|
|
|
|
#include <fstream> |
|
|
|
|
#include <iostream> |
|
|
|
|
#include <cmath> |
|
|
|
|
|
|
|
|
|
using namespace olc; |
|
|
|
|
struct vi2d{ |
|
|
|
|
int x,y; |
|
|
|
|
friend std::ostream&operator<<(std::ostream&out,vi2d&rhs){ |
|
|
|
|
out<<"("<<rhs.x<<","<<rhs.y<<")"; |
|
|
|
|
return out; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
struct node{ |
|
|
|
|
private: |
|
|
|
|
char elevation='a'; |
|
|
|
|
public: |
|
|
|
|
bool visited=false; |
|
|
|
|
std::vector<node*>connections; |
|
|
|
|
int myDist=INFINITY; |
|
|
|
|
int storedDist=INFINITY; |
|
|
|
|
vi2d pos; |
|
|
|
|
node*parent=nullptr; |
|
|
|
|
node(char elevation){ |
|
|
|
|
this->elevation=elevation; |
|
|
|
|
}; |
|
|
|
|
char getElevation(){ |
|
|
|
|
return elevation=='S'?'a':elevation=='E'?'z'+1:elevation; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -26,7 +37,7 @@ node*endNode; |
|
|
|
|
|
|
|
|
|
int main() |
|
|
|
|
{ |
|
|
|
|
std::ifstream file("testinput2"); |
|
|
|
|
std::ifstream file("input"); |
|
|
|
|
while (file.good()){ |
|
|
|
|
std::string line; |
|
|
|
|
std::getline(file,line); |
|
|
|
@ -48,18 +59,18 @@ int main() |
|
|
|
|
std::cout<<"================"<<std::endl; |
|
|
|
|
for (int y=0;y<grid.size();y++){ |
|
|
|
|
for (int x=0;x<grid[y].size();x++){ |
|
|
|
|
std::cout<<grid[y][x].elevation; |
|
|
|
|
std::cout<<grid[y][x].getElevation(); |
|
|
|
|
grid[y][x].pos={x,y}; |
|
|
|
|
if (x>0){ |
|
|
|
|
if (x>0&&grid[y][x-1].getElevation()-1<=grid[y][x].getElevation()){ |
|
|
|
|
grid[y][x].connections.push_back(&grid[y][x-1]); |
|
|
|
|
} |
|
|
|
|
if (x<grid[y].size()-1){ |
|
|
|
|
if (x<grid[y].size()-1&&grid[y][x+1].getElevation()-1<=grid[y][x].getElevation()){ |
|
|
|
|
grid[y][x].connections.push_back(&grid[y][x+1]); |
|
|
|
|
} |
|
|
|
|
if (y>0){ |
|
|
|
|
if (y>0&&grid[y-1][x].getElevation()-1<=grid[y][x].getElevation()){ |
|
|
|
|
grid[y][x].connections.push_back(&grid[y-1][x]); |
|
|
|
|
} |
|
|
|
|
if (y<grid.size()-1){ |
|
|
|
|
if (y<grid.size()-1&&grid[y+1][x].getElevation()-1<=grid[y][x].getElevation()){ |
|
|
|
|
grid[y][x].connections.push_back(&grid[y+1][x]); |
|
|
|
|
} |
|
|
|
|
if (x==playerPos.x&&y==playerPos.y){ |
|
|
|
@ -76,38 +87,26 @@ int main() |
|
|
|
|
|
|
|
|
|
std::vector<node*>searchNodes; |
|
|
|
|
searchNodes.push_back(startNode); |
|
|
|
|
startNode->myDist=0.0f; |
|
|
|
|
while (searchNodes.size()>0){ |
|
|
|
|
node*currentNode=searchNodes.front(); |
|
|
|
|
if (currentNode==startNode){ |
|
|
|
|
currentNode->myDist=pow(currentNode->pos.x-endNode->pos.x,2)+pow(currentNode->pos.y-endNode->pos.y,2); |
|
|
|
|
if (currentNode->visited){ |
|
|
|
|
searchNodes.erase(searchNodes.begin()); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
currentNode->visited=true; |
|
|
|
|
if (currentNode->elevation=='S'){ |
|
|
|
|
currentNode->elevation='a'-1; |
|
|
|
|
} |
|
|
|
|
if (currentNode->elevation=='E'){ |
|
|
|
|
currentNode->elevation='z'+1; |
|
|
|
|
} |
|
|
|
|
for (int i=0;i<currentNode->connections.size();i++){ |
|
|
|
|
node*neighbor=currentNode->connections[i]; |
|
|
|
|
if (neighbor==endNode){ |
|
|
|
|
//We can't go any higher.
|
|
|
|
|
endNode->parent=currentNode; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (neighbor->elevation-1<=currentNode->elevation){ |
|
|
|
|
if (neighbor->parent==nullptr||currentNode->myDist<neighbor->storedDist){ |
|
|
|
|
neighbor->parent=currentNode; |
|
|
|
|
neighbor->storedDist=currentNode->myDist; |
|
|
|
|
neighbor->myDist=pow(neighbor->pos.x-endNode->pos.x,2)+pow(neighbor->pos.y-endNode->pos.y,2); |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
searchNodes.erase(searchNodes.begin()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
node*searchNode=endNode; |
|
|
|
|
int jumps=0; |
|
|
|
|