Given the input xm, ym,h I have to find all paths from point (1,0,1) to (xm, ym, 1) in a 3d grid such as:
possible moves from (x,y,z) are (x+1, y, z), (x+1, y+1, z), (x+1, y-1, z)
if z < h more possible moves are (x+1, y, z+1), (x+1, y+1, z+1), (x+1, y-1, z+1)
if z > 1 more possible moves are also (x+1, y, z-1), (x+1, y+1, z-1),(x+1, y-1, z-1)
I figured out an algorithm that works for z = 1, but I am not sure how to make it work for other values of z. Any assistance will be appreciated.
llong countPaths3(int xm, int ym, int h) {
std::pair<int,int> pair1 = {1,0};
std::map<std::pair<int,int>, llong> map1;
map1.insert({pair,1});
int starty = -1;
int endy = 1;
llong value = 0;
for(int x = 2; x <= xm; x++) {
for(int y = starty; y <= endy; y++)
{
if (map1.count({x, y}) == 0) {
value = map1[{x - 1, y}] + map1[{x - 1, y + 1}] + map1[{x - 1, y - 1}];
}
map1[{x, y}] = value;
}
starty--;
endy++;
}
return map1[{xm,ym}];
}