Football Player - Total Dribble Paths
A football coach wants his team to improve their dribbling skills. So he sets up a R * C grid, where R is the number of rows and C is the number of columns. From any given cell, a player can only dribble to the right cell or bottom cell.
A player always starts from top left most cell and must end at bottom right most cell where he can collect the reward if he has dribbled according to the rules above.
The program must accept R, C and print the total number of possible paths P in which a player can reach the destination.
Input Format:
The first line contains R.
The second line contains C.
The first line contains R.
The second line contains C.
Output Format:
The first line contains P.
The first line contains P.
Boundary Conditions:
1 <= R, C <= 100
1 <= R, C <= 100
Example Input/Output 1:
Input:
2
3
Input:
2
3
Output:
3
3
Example Input/Output 2:
Input:
3
3
Input:
3
3
Output:
6
6
Example Input/Output 3:
Input:
25
21
Input:
25
21
Output:
102758710
Code:
#include <iostream>
using namespace std;
int num(int m,int n)
{
int
count[m][n];
int
i,j;
for(i=0;i<m;i++)
count[i][0]=1;
for(int
j=0;j<n;j++)
count[0][j]=1;
for(int
i=1;i<m;i++)
{
for(int j=1;j<n;j++)
count[i][j]=count[i-1][j]+count[i][j-1];
}
return
count[m-1][n-1];
}
int main(int argc,
char** argv)
{
int r,c;
cin>>r>>c;
cout<<num(r,c);
}
or
int num(int m,int n)
{
if(m==1||n==1)
return
1;
return
num(m-1,n)+num(m,n-1);
}
Python:
Do Follow for More Solutions!!!!
Python:
import sys
if __name__ == '__main__':
n,m=int(input()),int(input())
c= [[0 for _ in range(m)] for _ in range(n)]
for i in range(n):
c[i][0]=1
for j in range(m):
c[0][j]=1
for i in range(1,n):
for j in range(m):
c[i][j] = c[i-1][j] + c[i][j-1]
print(c[n-1][m-1])
Java:
import
java.util.*;
import
java.math.BigInteger;
public
class
Hello {
public
static
void
main(String[] args) {
int
r,c,i,j;
Scanner sc=
new
Scanner(System.in);
r=sc.nextInt();
c=sc.nextInt();
BigInteger[][] m=
new
BigInteger[r][c];
for
(i=
0
;i<r;i++)
{
for
(j=
0
;j<c;j++)
{
m[i][
0
]=BigInteger.valueOf(
1
);
m[
0
][j]=BigInteger.valueOf(
1
);
}
}
for
(i=
1
;i<r;i++)
{
for
(j=
1
;j<c;j++)
{
m[i][j]=m[i-
1
][j].add(m[i][j-
1
]);
}
}
System.out.println(m[r-
1
][c-
1
]);
}
}
import java.util.*;
ReplyDeleteimport java.math.BigInteger;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int R = sc.nextInt();
int C = sc.nextInt();
BigInteger[][] matrix = new BigInteger[R][C];
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
{
matrix[i][0]=BigInteger.valueOf(1);
matrix[0][j]=BigInteger.valueOf(1);
}
for(int i=1;i<R;i++)
for(int j=1;j<C;j++)
matrix[i][j]=matrix[i-1][j].add(matrix[i][j-1]);
System.out.println(matrix[R-1][C-1]);
}
}
Thank you for sharing valuable informationNice post,I enjoyed reading this post.
ReplyDeleteหนังผี
How to print for 176103950070 if the input is given as 25 and 21
ReplyDelete