Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active July 25, 2019 14:50
Show Gist options
  • Save manojnaidu619/c34b62cf0dd573a7418ac9b7492a18c0 to your computer and use it in GitHub Desktop.
Save manojnaidu619/c34b62cf0dd573a7418ac9b7492a18c0 to your computer and use it in GitHub Desktop.
Solving "On the way Home problem using DP"
/*
Question:
You are located at the top-left corner of a mxn grid. You can only move either right or down at any point in time.
Your home is located at the bottom right corner of this grid. In how many unique ways can you reach your home?
*/
#include <iostream>
#include <vector>
using namespace std;
int main(){
int row=3;
int col=3;
int ways[row][col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
ways[i][j]=0;
}
}
for(int i=0;i<col;i++){
ways[row-1][i] = 1;
}
for(int i=0;i<row;i++){
ways[i][col-1] = 1;
}
for(int x=row-2;x>=0;x--){
for(int y=col-2;y>=0;y--){
ways[x][y] = ways[x][y+1] + ways[x+1][y];
}
}
/*
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout << ways[i][j] << " ";
}
cout << endl;
}
*/
cout << ways[0][0] << " ways" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment