Attempt at A*, have to review notes

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 36236a54be
commit fcdbb1e8b4
  1. BIN
      C++ProjectTemplate
  2. 162
      main.cpp
  3. 372105
      out

Binary file not shown.

@ -4,62 +4,25 @@
using namespace olc;
std::vector<int> validPathStepCounts;
struct node{
char elevation='a';
bool visited=false;
std::vector<node*>connections;
int myDist=INFINITY;
int storedDist=INFINITY;
vi2d pos;
node*parent=nullptr;
node(char elevation){
this->elevation=elevation;
};
};
vi2d playerPos={0,0};
vi2d endingPos={0,0};
std::vector<std::vector<char>>grid;
int lowest=2468;
void step(char currentTile,vi2d pos,int xoffset,int yoffset,int stepCount,std::vector<std::vector<char>>visitedgrid,std::string currentPath){
pos.x+=xoffset;
pos.y+=yoffset;
std::cout<<currentPath<<std::endl;
if (currentTile=='S'){
currentTile='a';
}
if (stepCount+abs(pos.x-endingPos.x)+abs(pos.y-endingPos.y)>lowest){
//std::cout<<"Took too long..."<<std::endl;
return;
}
if (pos.y<0||pos.x<0||pos.x>=visitedgrid[0].size()||pos.y>=visitedgrid.size()){
//std::cout<<"Dead end reached in "<<stepCount<<" steps ("<<currentPath<<")"<<std::endl;
return;
}
if (visitedgrid[pos.y][pos.x]!='0'&&(visitedgrid[pos.y][pos.x]!='E'&&visitedgrid[pos.y][pos.x]-currentTile<=1||visitedgrid[pos.y][pos.x]=='E'&&currentTile>='y')){
currentPath+=visitedgrid[pos.y][pos.x];
if (visitedgrid[pos.y][pos.x]=='E'){
validPathStepCounts.push_back(++stepCount);
if (stepCount<lowest){
lowest=stepCount;
std::cout<<"Valid path found in "<<stepCount<<" steps! ("<<currentPath<<")"<<std::endl;
}
return;
}
++stepCount;
char newTile=visitedgrid[pos.y][pos.x];
visitedgrid[pos.y][pos.x]='0';
if (pos.y+1<visitedgrid.size()&&visitedgrid[pos.y+1][pos.x]!='0'&&yoffset!=-1){
//visitedgrid[pos.y+1][pos.x]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,0,1,stepCount,newvisitedgrid,currentPath);
}
if (pos.y-1>=0&&visitedgrid[pos.y-1][pos.x]!='0'&&yoffset!=1){
//visitedgrid[pos.y-1][pos.x]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,0,-1,stepCount,newvisitedgrid,currentPath);
}
if (pos.x-1>=0&&visitedgrid[pos.y][pos.x-1]!='0'&&xoffset!=1){
//visitedgrid[pos.y][pos.x-1]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,-1,0,stepCount,newvisitedgrid,currentPath);
}
if (pos.x+1<visitedgrid[0].size()&&visitedgrid[pos.y][pos.x+1]!='0'&&xoffset!=-1){
//visitedgrid[pos.y][pos.x+1]='0';
std::vector<std::vector<char>>newvisitedgrid=visitedgrid;
step(newTile,pos,1,0,stepCount,newvisitedgrid,currentPath);
}
}
}
std::vector<std::vector<node>>grid;
node*startNode;
node*endNode;
int main()
{
@ -67,10 +30,10 @@ int main()
while (file.good()){
std::string line;
std::getline(file,line);
std::vector<char>row;
std::vector<node>row;
if (line.length()>0){
for (int x=0;x<line.length();x++){
row.push_back(line[x]);
row.push_back(node(line[x]));
if (line[x]=='S'){
playerPos={x,(int)grid.size()};
}
@ -85,30 +48,75 @@ 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];
std::cout<<grid[y][x].elevation;
grid[y][x].pos={x,y};
if (x>0){
grid[y][x].connections.push_back(&grid[y][x-1]);
}
if (x<grid[y].size()-1){
grid[y][x].connections.push_back(&grid[y][x+1]);
}
if (y>0){
grid[y][x].connections.push_back(&grid[y-1][x]);
}
if (y<grid.size()-1){
grid[y][x].connections.push_back(&grid[y+1][x]);
}
if (x==playerPos.x&&y==playerPos.y){
startNode=&grid[y][x];
std::cout<<"Starting node set."<<std::endl;
}
if (x==endingPos.x&&y==endingPos.y){
endNode=&grid[y][x];
std::cout<<"Ending node set."<<std::endl;
}
}
std::cout<<std::endl;
}
std::cout<<"Player Pos: "<<playerPos<<std::endl;
char newTile=grid[playerPos.y][playerPos.x];
grid[playerPos.y][playerPos.x]='0';
std::vector<std::vector<char>>visitedgrid=grid;
std::vector<std::vector<char>>visitedgrid2=grid;
std::vector<std::vector<char>>visitedgrid3=grid;
std::vector<std::vector<char>>visitedgrid4=grid;
std::cout<<"Part 1"<<std::endl;
step(newTile,playerPos,1,0,0,visitedgrid,"S");
std::cout<<"Part 2"<<std::endl;
step(newTile,playerPos,-1,0,0,visitedgrid2,"S");
std::cout<<"Part 3"<<std::endl;
step(newTile,playerPos,0,1,0,visitedgrid3,"S");
std::cout<<"Part 4"<<std::endl;
step(newTile,playerPos,0,-1,0,visitedgrid4,"S");
std::sort(validPathStepCounts.begin(),validPathStepCounts.end());
for (int i=0;i<validPathStepCounts.size();i++){
std::cout<<validPathStepCounts[i]<<std::endl;
}
std::cout<<"Best: "<<validPathStepCounts[0]<<std::endl;
std::vector<node*>searchNodes;
searchNodes.push_back(startNode);
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);
}
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);
}
}
}
}
searchNodes.erase(searchNodes.begin());
}
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;
}

372105
out

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save