The full problem description I am trying to solve can be found here: https://i.stack.imgur.com/o1cFn.png
I am using a BFS and have created a RaceTrack object. The BFS queue setup is no problem, and this project would be cake (i think) if I only had to find the shortest path to one goal. But instead, I have to travel from the starting point through each checkpoint in order! Does anybody have any ideas how one would implement this with the standard idea of using a BFS? I'll include my code, but keep in mind that it is VERY unfinished, and may make no sense. The "move" method is where the majority of the work needs to be done.
You'll notice that I stopped at the if statement that tests if the space you move to is an integer.
Your suggestions are welcome.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <set>
using namespace std;
class RaceTrack
{
public:
RaceTrack() { count = 0;}
bool isSolved()
{
if (solved == 1)
return true;
else
return false;
}
int solved;
string track;
int count;
set<char> prevChkPt;
};
int main()
{
list<RaceTrack> q; // queue for BFS
set<RaceTrack> v; // set of visited tracks
RaceTrack x;
string sLine;
vector<int> chkPts;
int start;
int w, h;
int tmp = 0;
cin >> w >> h;
for (int i = 0; i < h; i++)
{
cin >> sLine;
x.track += sLine;
}
q.push_back(x);
while (q.size() > 0 && x.isSolved() == false)
{
x = q.front();
q.pop_front();
start = x.track.find_first_of('0');
if (x.isSolved() == false && v.find(x) == v.end())
{
v.insert(x);
move (start, w, h, x, q, v, 'q');
move (start, w, h, x, q, v, 'u');
move (start, w, h, x, q, v, 'p');
move (start, w, h, x, q, v, 'l');
move (start, w, h, x, q, v, 'r');
move (start, w, h, x, q, v, 'z');
move (start, w, h, x, q, v, 'd');
move (start, w, h, x, q, v, 'm');
}
}
if (x.isSolved == true)
cout << x.count;
else
cout << -1;
// for testing purposes only:
string quit;
// for testing purposes only:
cin >> quit;
if(quit == "quit")
return 0;
}
void move (int start, int w, int h, RaceTrack x, list<RaceTrack> q, set<RaceTrack> v, char direction)
{
int d1, d2, d3, d4; // diagonal indices
if (direction == 'q') // diagonal top left
{
d1 = start - w - 1;
if (start % w == 0 || start < w)
return;
else
{
if (x.track[d1] == 'x')
return;
else if (x.track[d1] == ' ')
{
x.track[d1] = x.track[start];
x.count++;
}
else if (isdigit(x.track[d1]))
x.prevChkPt.insert(x.track[d1]);
}
}
if (v.find(x) == v.end())
q.push_back(x);
}