Curious George plays around on a planar grid. George can move one space at a time: left, right, up or down.
That is, from George can go to , , , or .
George can access any point where the sum of the digits of the sum of the digits of is .
How many points can George access if he starts at including itself?
Explicit Examples
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.
I solved this problem with a blind Depth First Search.Let us define a function Visit(x,y) . Here is what Visit(x,y) does when it is called:
1. Check if ( x , y ) has previously been visited,if so return Nothing
2. If it hasn't been previously visited,do the below :
Check if ( x , y + 1 ) is accessible.If it is Visit(x,y+1)
Check if ( x , y − 1 ) is accessible.If it is Visit(x,y-1)
Check if ( x + 1 , y ) is accessible. If it is Visit(x+1,y)
Check if ( x − 1 , y ) is accessible. If its is Visit(x-1,y)
3. Add ( x , y ) to the list of visited coordinates.
When Visit(0,0) is called, it recursively checks all possible paths and stops when nothing is no more accessible.
Python:
Since consistently inter-converting between strings and integers to find the digits sum is costly, I used a dictionary (Table) to speed things up.
The code prints out the answer 1 0 2 4 8 5 in around 3 seconds on my machine.