1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
#include<algorithm>
using namespace std;
char directions[4] = {'L', 'R', 'U', 'D'};
//string direcPrint[4] = {"Left", "Right", "Up", "Down"} ;
const int pondSize = 5;
struct frontier{
int currentPad;
string currentPath;
vector <int> previousPads;
};
struct solution{
string pathway;
int nodesLooked;
int length;
};
int nextPad(int cPad, int dist, int direc)
{
//cPad is the index of the current lilypad
//dist is distance jumped(1-4)
//direc is the direction(L,R,U,D)
int nPad = -1; //-1 by default
int currentCol;
if (direc == 0) //Left
{
currentCol = cPad % pondSize;
if (currentCol >= dist)
{
nPad = cPad-dist;
}
}
else if (direc == 1) //Right
{
currentCol = cPad % pondSize;
if (currentCol + dist < pondSize)
{
nPad = cPad + dist;
}
}
else if (direc == 2) //Up
{
nPad = max(max(-1, cPad), (dist*pondSize));
}
else if (direc == 3) //Down
{
nPad = cPad + (dist*pondSize);
if (nPad >= (pondSize^2))
{
nPad = -1;
}
}
else
nPad = -1;
return nPad;
}
solution shortestPath(string pondPath)
{
int pad;
string path;
vector <int> pads; //Same as previousPads
vector <int>::iterator it;
solution sol;
queue <frontier> fringe;
frontier currentItem;
currentItem = {0,"",vector<int>()};
currentItem.currentPad = 0;
currentItem.currentPath = "";
currentItem.previousPads.push_back(0);
fringe.push(currentItem);
int nodeLooked = 0;
int i = 0;
int counting = 0;
while (!fringe.empty())
{
frontier currentFringe;
currentFringe = fringe.front();
pad = currentFringe.currentPad;
path = currentFringe.currentPath;
pads.push_back(currentFringe.previousPads[i]);
nodeLooked++;
i++;
if (pondPath[pad] == 'G')
{
break;
}
int dist;
int futurePad;
for(int jump=0; jump<4; jump++)
{
char temp = pondPath[pad];
dist = (int)temp;
futurePad = nextPad(pad, dist, jump);
if (futurePad != -1)
{
it = find(pads.begin(), pads.end(), futurePad);
if(it != pads.end())
{
break;
}
else
{
//i++;
string futurePath = path + directions[jump];
pads.push_back(futurePad);
currentFringe.currentPad = futurePad;
currentFringe.currentPath = futurePath;
currentFringe.previousPads = pads;
fringe.push(currentFringe);
}
}
}
path = "No way to the solution!!";
}
sol.pathway = path;
sol.nodesLooked = nodeLooked;
sol.length = fringe.size();
return sol;
}
int main()
{
solution answer;
string userInput = "";
clock_t t1, t2;
cout<<"Please enter your string: ";
getline(cin, userInput);
cout<<"This is the pond that you have entered: "<<endl;
cout<<userInput<<endl;
t1 = clock();
cout<<clock();
answer = shortestPath(userInput);
t2 = clock();
cout<<endl<<t2<<endl;
cout<<endl;
cout<<"The solution is: "<<answer.pathway<<endl;
cout<<"Nodes looked at: "<<answer.nodesLooked<<endl;
cout<<"Nodes not looked at: "<<answer.length<<endl;
float diff ((float)t2-(float)t1);
cout<<"time is: "<<diff<<endl;
system("PAUSE");
return 0;
}
|