The google treasure hunt is on at http://treasurehunt.appspot.com/
Here is the solution to the robots problem
The problem is to fins the total no. of paths from one corner to another in a grid
The logic behind my solution lies as:
Total no of paths from the top corner to any given corner is the sum of the two boxes one on left and the other on top of that box.
like
So I have made two programs to do the same.Run them by changing the limits as per given in your question.
the first one in java:
class Google
{
public static void main(String srgs[])
{
long a[][]=new long[5][8];
for(int i=0;i<5;i++)
a[i][0]=1;
for(int i=0;i<8;i++)
a[0][i]=1;
for(int i=1;i<5;i++)
{
for(int j=1;j<8;j++)
{
a[i][j]=0;
a[i][j]=a[i-1][j]+a[i][j-1];
}
}
for(int i=0;i<5;i++)
{
for(int j=0;j<8;j++)
{
System.out.print(a[i][j]+ ” “);
}
System.out.println();
}
}
}
The other is in C
#include
#include
void main()
{
int m=3;
int n=7;
int A[3][7];
for(int i=1;i<=m;i++)
A[i][1]=1;
for(int j=1;j<=n;j++)
A[1][j]=1;
for(i=2;i<=m;i++)
for(j=2;j<=n;j++)
A[i][j]=A[i-1][j]+A[i][j-1] ;
for(i=1;i<=m;i++)
{
cout<< “\n \n”;
for(j=1;j<=n;j++)
cout<<a[i][j]<<” “;
}
}
RSS Feed
Twitter
November 9th, 2008
Amol Gupta
Posted in
Tags:

