Last active
August 29, 2015 14:09
-
-
Save seank-com/419f1d849b3107f9de25 to your computer and use it in GitHub Desktop.
Calculate all shortest paths from one coordinate to the other
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <tchar.h> | |
#include <vector> | |
#include <string> | |
#include <math.h> | |
#include <iostream> | |
using namespace std; | |
static char* commands[] = { "W", nullptr, "E", "S", nullptr, "N" }; | |
vector<string> CalculatePath(int startX, int startY, int endX, int endY) | |
{ | |
vector<string> result; | |
int delta[] = { (endX - startX), (endY - startY) }; | |
for (int axis = 0; axis < 2; axis += 1) | |
{ | |
if (delta[axis] != 0) | |
{ | |
delta[axis] /= abs(delta[axis]); | |
char* moveCommand = commands[(axis * 3) + 1 + delta[axis]]; | |
vector<string> paths = CalculatePath(startX + (delta[0] * abs(1 - axis)), startY + (delta[1] * axis), endX, endY); | |
if (paths.empty()) | |
{ | |
result.push_back(moveCommand); | |
} | |
else | |
{ | |
for (const string& path : paths) | |
{ | |
result.push_back(moveCommand + path); | |
} | |
} | |
} | |
} | |
return result; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
vector<string> paths = CalculatePath(0, 0, 8, 1); | |
for (const string& path : paths) | |
{ | |
cout << path << endl; | |
} | |
int waitVar; | |
cin >> waitVar; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment