Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
A classic numeric keypad looks like this
<br />
1  2  3<br />
4  5  6<br />
7  8  9<br />
  0<br />

A horse in chess can move horizontally or vertically two fields and then one to the left or right:
Example:
From the number 3 on the keypad the horse can move to 8 or 4
From the number 6 on the keyboard the horse can move to 7, 1 or 0

If you start on the number 1, and pick next number the way a horse moves in chess,

How many unique paths within 10 steps exists?
   A path can visit the same number more than once.

Example path: 1 6 1 6 1 6 1 6 1 8



Ok. So the thing that confuses me is the fact that I have to do 10 steps and they can repeat. If it was a simple case of finding all possible moves I think I could find all combinations.

I identified a connected graph but I think it is redundant to the solution.

any ideas or pseudo code appreciated!

all allowed steps
1, 6, 8<br />
2, 7, 9<br />
3, 4, 8<br />
4, 3, 9, 0<br />
6, 1, 7, 0<br />
7, 2, 6<br />
8, 1, 3<br />
9, 4, 2<br />
0, 4, 6


Choosing this strategy to find legal nodes
C#
public static int Find(int node, int index)
{
    if (index == 0)
    {
        switch (node)
        {
            case 1 : return 6;
            case 2 : return 7;
            case 3 : return 4;
            case 4 : return 3;
            case 6 : return 1;
            case 7 : return 2;
            case 8 : return 1;
            case 9 : return 4;
            case 0 : return 4;
        }
    }
    else if (index == 1)
    {
        switch (node)
        {
            case 1 : return 8;
            case 2 : return 9;
            case 3 : return 8;
            case 4 : return 9;
            case 6 : return 7;
            case 7 : return 6;
            case 8 : return 3;
            case 9 : return 2;
            case 0 : return 6;
        }
    }
    else if (index == 2)
    {
        switch (node)
        {
            case 4 : return 0;
            case 6 : return 0;
        }
    }
    return -1;
}


First variation, but where to go after 1 6 1 6 1 6 1 6 1 6

I need to find all permutations of this pattern with the next node, I think I can move left in the node tree and ignore all right nodes and simply multiply answer with 2 to get the right nodes. But then I have an issue with the 0 node (I think)

C#
HashSet<string> paths = new HashSet<string>();
paths.Add("1 6 1 6 1 6 1 6 1 6");

int start = 1;
//int depth = 1;
int k = start;
for (int j = 0; j < 3; j++)
{
string path = "";
for(int i = 0; i < 10; i++)
{
    int n = k;
    path += string.Format("{0} ", n);
    k = Find(n, j);
}
Console.WriteLine(path);
}
Posted
Updated 17-Dec-14 9:55am
v7
Comments
Kornfeld Eliyahu Peter 17-Dec-14 9:28am    
42
Member 11295711 17-Dec-14 14:50pm    
Good one! It is wrong though! ;)

 
Share this answer
 
Comments
Member 11295711 17-Dec-14 15:57pm    
I'm not sure I get very far with shortest path, because repetition is allowed.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 11295711 17-Dec-14 14:08pm    
@OriginalGriff It's not homework. I will update my question with my attempt so far.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900