Last time , Sally moved through her garden eating an apple in every square.
But this time Sally feels more enthusiastic. This time she's going to do the same but now, she can also move diagonally. Find the number of ways in which Sally can eat all 25 apples
Details and Assumptions
She has a 5 × 5 garden with total of 25 apples.
She can move forward, backward, up, down or diagonally as long as there is an apple in the cell she is moving to.
She is standing in the center of the garden, i.e. 3 rd row and 3 rd column.
If the garden was of size 3 × 3 , then she can eat all the apples in exactly 32 ways.
Warning
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.
@Arulx Z , Check this out?
Log in to reply
Nice job! The stack solution is really nice. Also, your recursion solution is almost 20 times faster than mine!
Log in to reply
Mainly because Java runs on a virtual machine whereas C++ is compiled code.
I guess the same algorithm would be faster in assembly and like 20 times slower in Python
I am not expert in C++, this is a rewrite of your code with no global variables.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Log in to reply
I am trying to brush up my C++ skills too.
How fast is your code?
Here is 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 34 35 36 37 |
|
Nice solution! By the way, what is the execution time? When I ran my solution, it took very long to compute the answer. (I have Python 2.7 installed so I can't test your code directly on my system without messing it up).
Log in to reply
In my machine,it takes 70 minutes.
A modified version could divide the execution time by 4 to 8.
Log in to reply
I am giving a try to nested loops as deep recursion probably makes the code slower.
Log in to reply
@Arulx Z – What time does it take for the fastest java version? My version takes a lot, like 175 seconds or so.
Log in to reply
@Sergio Tomás – Can you post your code? 175 secs is actually a very good time. My version takes almost 30-35 mins to execute.
Log in to reply
@Arulx Z – Sure, here it is: * OLD SOLUTION REMOVED, IMPROVED BELOW*
Log in to reply
@Sergio Tomás – It takes 90 secs to run on my machine (which is a really good speed). However, for some weird reasons, I am getting the answer as 25372384 instead of 25373376. I haven't analyzed your code yet so pardon me if I made any mistake in interpretation of the answer.
Log in to reply
@Arulx Z – You are totally right, something has broken with some last-minute change :) I will try to find the small bug polluting the result :)
Log in to reply
@Sergio Tomás – There seems to be an issue in the cache logic for big combinations. If you change the CACHE SUB NODE MAX SIZE constant to a smaller value, like 9 it gives you the proper 25373376 taking more time of course. It's something :)
@Arulx Z – I have made some modifications based on some things I have seen on other answers, to try to optimize the java solution. It runs on 160 seconds on my machine, so it should be pretty fast in your machine, if the previous one was running so fast.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
Result: 25373376 160133 ms. 160 s.
Log in to reply
@Sergio Tomás – Nice solution! Good Job! Why not post it as solution instead as a comment? I'm sure you'll get tons of upvotes.
Can you provide pseudo-code for the modified version?
Log in to reply
@Arulx Z – This a modified version 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
Output:
1 2 |
|
It takes around 95 secodnds on an online C++11 compiler.
If you can explain me how the solution works, may be we can improve on the time a bit.
Log in to reply
Ok. I have a slightly different solution in Java (I'll post it in 10 mins). We'll try improving it as even I'm unclear about how this solution works.
Log in to reply
@Arulx Z – You do not need to post your Java code. You already posted it on the other solution thread.
I would be obliged if you explained how it works instead?
Log in to reply
@Agnishom Chattopadhyay – Wait, no. i got it. i will work on optimizing the code
Log in to reply
@Agnishom Chattopadhyay – Ok. I have made already made a brief description so I'll post it anyways.
@Agnishom Chattopadhyay – Ok.
Here is the working -
I have created an 5 × 5 two dimensional array. The array is filled with 0's. 0's represent apple. 1's represent that apple has been eaten. Here's the numbering system -
1 2 3 4 5 |
|
The array numbering is indexed at
[0, 0]
at top left (ie,
1 = [0, 0]
,
8 = [1, 2]
).
The method
ntc
converts coordinates of form
nc
to
Cartesian
, so that I can feed
nc
to array.
The method
neighbours
takes a
nc
as the argument and then looks for the neighbours of
nc
. This works from the fact that, if subtract 4 from
nc
, we get the top-right neighbour. Similarly, here are the other results -
1 2 3 4 5 6 7 8 9 |
|
Additionally the method only checks for neighbours with 0 in it (ie, the cell which contain apple). Also, it checks if the neighbour fits in the 5 × 5 grid. For example, if pass the argument as 49, it will have no right neighbour as 4 9 + 1 > 4 9 . Also, 49 doesn't have bottom, bottom-right and bottom-left neighbour.
The neighbours which satisfy all the checks are added to a list and returned.
The method
recursion
solve the problem. Here's the working -
First I start in the center cell, ie 13. Then I look for neighbours of 13. I get a list containing the neighbours of 13. Then I look for the neighbours of neighbours of 13 and then so on. If a path has 25 steps, then it is a valid path for Sally. This way, I iterate over all the possible paths.
There is a slight change in my code from the previous thread. I have added some lines in the
neighbours
method.
Here's the new code -
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
Sorry for late reply. I'm in school so I can only work during free lectures.
Can you explain how the solution actually works?
My one works correctly and time 30.0009 s. I also used recursive functions. Almost forgot, great problem! I gave it a like.
Thanks! Can you post your solution?
Log in to reply
This would be my code for such solution.
#include <iostream>
using namespace std;
const int A = 5, A_2 = A * A;
bool Field[A + 2][A + 2];
void initialization()
{
int i, j;
for(i = 0; i <= A + 1; ++i)
{
for(j = 0; j <= A + 1; ++j)
{
if(i && j && i <= A && j <= A) Field[i][j] = true;
else Field[i][j] = false;
}
}
}
long long int solution = 0;
void Ike(int i, int j, int KO)
{
if(!Field[i][j]) return;
++KO;
Field[i][j] = false;
if(KO == A_2)
{
++solution;
Field[i][j] = true;
return;
}
else
{
Ike(i + 1, j, KO);
Ike(i - 1, j, KO);
Ike(i, j + 1, KO);
Ike(i, j - 1, KO);
Ike(i + 1, j + 1, KO);
Ike(i + 1, j - 1, KO);
Ike(i - 1, j + 1, KO);
Ike(i - 1, j - 1, KO);
}
Field[i][j] = true;
}
int main()
{
initialization();
int c = (A + 1) / 2;
Field[c][c] = false;
Ike(c, c + 1, 1);
Ike(c + 1, c + 1, 1);
solution *= 4;
cout << solution;
return 0;
}
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 |
|
What is the time taken?
Problem Loading...
Note Loading...
Set Loading...
Here is the modification of the depth first search using a stack as opposed to plain recursion.
Later Edit: Turns out recursion actually works better. See below.
Output:
It turns out that recursion is faster. This code is forked from Abdelhamid Saadi's solution to this problem .
If it were not for performance, I would say that this code has many practices that I do not like. Especially, the use of global variables.
Output: