Sally loves apples, thus, she has an apple garden in her backyard, where she has grown 2 5 apples in the shape of a square of side 5 . She is standing in the center of the garden, i.e. 3 rd row and 3 rd column. Now, she wants to move through the garden eating apple in every square, and she won't move into square without any apple. How many ways are there in which she can eat all the 2 5 apples?
Example:
If it were a 3 × 3 garden, and Sally would have started from center, i.e. ( 2 , 2 ) there would be 8 solutions as follows
1 . ( 2 , 2 ) , ( 3 , 2 ) , ( 3 , 3 ) , ( 2 , 3 ) , ( 1 , 3 ) , ( 1 , 2 ) , ( 1 , 1 ) , ( 2 , 1 ) , ( 3 , 1 ) 2 . ( 2 , 2 ) , ( 3 , 2 ) , ( 3 , 1 ) , ( 2 , 1 ) , ( 1 , 1 ) , ( 1 , 2 ) , ( 1 , 3 ) , ( 2 , 3 ) , ( 3 , 3 ) 3 . ( 2 , 2 ) , ( 2 , 3 ) , ( 3 , 3 ) , ( 3 , 2 ) , ( 3 , 1 ) , ( 2 , 1 ) , ( 1 , 1 ) , ( 1 , 2 ) , ( 1 , 3 ) 4 . ( 2 , 2 ) , ( 2 , 3 ) , ( 1 , 3 ) , ( 1 , 2 ) , ( 1 , 1 ) , ( 2 , 1 ) , ( 3 , 1 ) , ( 3 , 2 ) , ( 3 , 3 ) 5 . ( 2 , 2 ) , ( 1 , 2 ) , ( 1 , 3 ) , ( 2 , 3 ) , ( 3 , 3 ) , ( 3 , 2 ) , ( 3 , 1 ) , ( 2 , 1 ) , ( 1 , 1 ) 6 . ( 2 , 2 ) , ( 1 , 2 ) , ( 1 , 1 ) , ( 2 , 1 ) , ( 3 , 1 ) , ( 3 , 2 ) , ( 3 , 3 ) , ( 2 , 3 ) , ( 1 , 3 ) 7 . ( 2 , 2 ) , ( 2 , 1 ) , ( 1 , 1 ) , ( 3 , 2 ) , ( 3 , 3 ) , ( 2 , 3 ) , ( 1 , 3 ) , ( 1 , 2 ) , ( 1 , 1 ) 8 . ( 2 , 2 ) , ( 2 , 1 ) , ( 1 , 1 ) , ( 1 , 2 ) , ( 1 , 3 ) , ( 2 , 3 ) , ( 3 , 3 ) , ( 3 , 2 ) , ( 3 , 1 )
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Great job on the timing. It would be helpful to other users to illustrate your algorithm at work on a smaller grid for several starting positions.
0.09 seconds is quite nice for this!
Log in to reply
Thanks! Earlier I was assuming that Sally can pass through diagonals too but I fixed it after reading the example.
Log in to reply
The DFS I wrote is 0.003 seconds. Although it says sys time is 0.000 but I don't know what to make of that.
Log in to reply
@Jorge Fernández – Basic functionality of code is same though. The difference is possibly due to C++.
My solution in python 3.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
We can solve this using Depth First Search. Here is the C++ code I wrote:
#include <cstdio>
int grid[5][5];
int a,b,k,r;
void find(int x,int y){
grid[x][y]=1;
k++;
if(k==25){
r++;
}
if(x-1>=0 && grid[x-1][y]==0){
find(x-1,y);
}
if(x+1<5 && grid[x+1][y]==0){
find(x+1,y);
}
if(y-1>=0 && grid[x][y-1]==0){
find(x,y-1);
}
if(y+1<5 && grid[x][y+1]==0){
find(x,y+1);
}
grid[x][y]=0;
k--;
}
int main(){
int m,n;
find(2,2);
printf("%d\n",r);
}
I can't help but notice the declaration of unused variables. Why'd you do that?
Log in to reply
When I wrote it it solved for a more general case and then I manually substituted the values of the variables into the code, so those variables disappeared from the body.
Problem Loading...
Note Loading...
Set Loading...
I used recursion to solve the problem. The code might look a bit scary so here's the working -
For simplicity, consider this array. Each individual members of the array are called cell (for no special reason) -
The method
neighbours
takes an cell as input and returns the neighbors of that cell. If you add 5 to a cell, you will get the cell to the bottom of the original cell. If you add -5, then you get the top cell. For 1 and -1, it's right and left respectively.In the method
recursion
, I look for neighbors of a cell. Let say array N has neighbours of any cell x . First I look at neighbors of N then neighbours of neighbours of N and then neighbours of neighbours of neighbours of N and so on. When the recursion method is called exactly 25 times by and cell, then I count that cell because that cell satisfies the condition. This way, I count the number of ways.Java code:
Time taken is about 0.09 secs.