Skip to content

Instantly share code, notes, and snippets.

@pradhuman-soni
Created October 21, 2021 10:07
Show Gist options
  • Select an option

  • Save pradhuman-soni/07b4b985a20033b98fc45d6fccebebed to your computer and use it in GitHub Desktop.

Select an option

Save pradhuman-soni/07b4b985a20033b98fc45d6fccebebed to your computer and use it in GitHub Desktop.
pepcoding.com 21/10/2021 printMazePathsWithJumps
#include<iostream>
using namespace std;
void printMazePathsWithJumps(int sr, int sc, int dr, int dc, string psf) {
if(sr == dr && sc == dc) { //base case
cout << psf << endl;
return;
}
// horizontal moves
for(int jumps = 1; jumps + sc <= dc; jumps++) {
printMazePathsWithJumps(sr, sc + jumps, dr, dc, psf + 'h' + to_string(jumps));
}
// vertical moves
for(int jumps = 1; jumps + sr <= dr; jumps++) {
printMazePathsWithJumps(sr + jumps, sc, dr, dc, psf + 'v' + to_string(jumps));
}
// diagonal moves
for(int jumps = 1; jumps + sr <= dr && jumps + sc <= dc; jumps++) {
printMazePathsWithJumps(sr + jumps, sc + jumps, dr, dc, psf + 'd' + to_string(jumps));
}
}
int main() {
int n ;
int m ;
cin >> n >> m;
printMazePathsWithJumps(0, 0, n - 1, m - 1, "");
}
def printMazePathsWithJumps(sr, sc, dr, dc, asf):
if sr == dr and sc == dc: # base case
print(asf)
return
if sr > dr or sc > dc: # prevents illegal moves
return
# horizontal moves
for jumps in range(1, dc + 1):
if sc + jumps <= dc:
printMazePathsWithJumps(sr, sc + jumps, dr, dc, asf + 'h' + str(jumps))
# vertical moves
for jumps in range(1, dr + 1):
if sr + jumps <= dr:
printMazePathsWithJumps(sr + jumps, sc, dr, dc, asf + 'v' + str(jumps))
# diagonal moves
for jumps in range(1, dc * dr):
if sc + jumps <= dc and sr + jumps <= dr:
printMazePathsWithJumps(sr + jumps, sc + jumps, dr, dc, asf + 'd' + str(jumps))
n = int(input())
m = int(input())
printMazePathsWithJumps(0, 0, n - 1, m - 1, "")
#include<iostream>
using namespace std;
void printMazePathsWithJumps(int sr, int sc, int dr, int dc, string psf) {
// write your code here
}
int main() {
int n ;
int m ;
cin >> n >> m;
printMazePathsWithJumps(0, 0, n - 1, m - 1, "");
}
def printMazePathsWithJumps(sr, sc, dr, dc, asf):
# write your code here
n = int(input())
m = int(input())
printMazePathsWithJumps(0, 0, n - 1, m - 1, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment