Code
Output:
Found all possible paths
| 0 || 1 || 2 || 3 |
| 1 || 2 || 3 || 4 |
| 2 || 3 || 4 || 5 |
| 3 || 4 || 5 || 6 |
Found choses shortest path
| 1 || 1 || 1 || 1 |
| 0 || 0 || 0 || 1 |
| 0 || 0 || 0 || 1 |
| 0 || 0 || 0 || 0 |
Number of steps to get from coords 0, 0 to coords 2,3: 6
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#define element(width, x, y) (x)*width+(y)
#define xLength 4
#define yLength 4
void populatePaths(int *mappedPath, int level, int xStart, int yStart);
void findPath(int *mappedPath, int *chosenPath, int level, int xStart, int yStart, int xEnd, int yEnd);
void printGrid(int *mappedPath);
void initializeGrid(int *mappedPath);
int main() {
int mappedPath[xLength*yLength] = {0};
int chosenPath[xLength*yLength] = {0};
int xStart = 0;
int yStart = 0;
int xEnd = 2;
int yEnd = 3;
initializeGrid(mappedPath);
mappedPath[element(xLength, xStart, yStart)] = 0;
populatePaths(mappedPath, 0, xStart, yStart);
printf("Found all possible paths\n");
printGrid(mappedPath);
findPath(mappedPath, chosenPath, 0, xStart, yStart, xEnd, yEnd);
printf("Found choses shortest path\n");
printGrid(chosenPath);
printf("Number of steps to get from coords %d, %d to coords %d,%d: %d\n", xStart, yStart, xEnd, yEnd, mappedPath[element(xLength, xEnd, yEnd)]+1);
return 0;
}
void initializeGrid(int *mappedPath) {
int i = 0;
for(i = 0; i < (xLength * yLength); i++) {
mappedPath[i] = xLength * yLength;
}
}
void printGrid(int *mappedPath) {
int i = 0;
int j = 0;
for(i = 0; i < yLength; i++) {
for(j = 0; j < xLength; j++) {
printf("| %d |", mappedPath[element(xLength, i, j)]);
}
printf("\n");
}
printf("\n\n");
}
void findPath(int *mappedPath, int *chosenPath, int level, int xStart, int yStart, int xEnd, int yEnd) {
level = mappedPath[element(xLength, xEnd, yEnd)];
chosenPath[element(xLength, xEnd, yEnd)] = 1;
if(xEnd == xStart && yEnd == yStart) {
return;
}
if(xEnd+1 < xLength) {
if(mappedPath[element(xLength, xEnd+1, yEnd)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd+1, yEnd);
}
}
if(xEnd-1 >= 0) {
if(mappedPath[element(xLength, xEnd-1, yEnd)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd-1, yEnd);
}
}
if(yEnd+1 < yLength) {
if(mappedPath[element(xLength, xEnd, yEnd+1)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd, yEnd+1);
}
}
if(yEnd-1 >= 0) {
if(mappedPath[element(xLength, xEnd, yEnd-1)] < level) {
return findPath(mappedPath, chosenPath, level-1, xStart, yStart, xEnd, yEnd-1);
}
}
return;
}
void populatePaths(int *mappedPath, int level, int xStart, int yStart) {
mappedPath[element(xLength, xStart, yStart)] = level;
if(xStart+1 < xLength) {
if(mappedPath[element(xLength, xStart+1, yStart)] > level+1) {
populatePaths(mappedPath, level+1, xStart+1, yStart);
}
}
if(xStart-1 >= 0) {
if(mappedPath[element(xLength, xStart-1, yStart)] > level+1) {
populatePaths(mappedPath, level+1, xStart-1, yStart);
}
}
if(yStart+1 < yLength) {
if(mappedPath[element(xLength, xStart, yStart+1)] > level+1) {
populatePaths(mappedPath, level+1, xStart, yStart+1);
}
}
if(yStart-1 >= 0) {
if(mappedPath[element(xLength, xStart, yStart-1)] > level+1) {
populatePaths(mappedPath, level+1, xStart, yStart-1);
}
}
}
ducks is leets
This post was edited by j0ltk0la on Feb 1 2016 02:55am