4

Can anyone help me with this problem? We have a grid of MxN characters from some specific aplhabet, S={A,B,C,D} for example. The cursor is positioned on (1,1) position, and we can move cursor using the arrow keys, up, down, left, right, and press enter to select the character ( just like selecting nick in old games ). What is minimum cost of operations where they are weighted same, (e.g. move right is equally costly as selecting the char) given some input string from aplhabet S? There can also be multiple occurences of the same character in the matrix.

Example:

alphabet S={A,B,C,D}

matrix :

ABDC CADB ABAA

and input string ADCABDA.

My incomplete solution would be: Construct directed grid graph and find shortest path from 1,1 to end character, with inbetween characters similar to towns in TSP, and from optimal subpaths construct optimal final path using dynamic programming. Problem is that you could end with many possible end characters, and I totally have no idea how to construct longer optimal path from smaller optimal subpaths.

martinerk0
  • 403
  • 1
  • 4
  • 17

2 Answers2

0

You should construct a graph with nodes something like this:

       A1          A1          A1
       A2  D1  C1  A2  B1  D1  A2
Start  A3  D2  C2  A3  B2  D2  A3  End
       A4          A4  B3      A4
       A5          A5          A5

where there are edges connecting each node in a column to each node in the next column. Start is (1,1) and End is wherever. The edge weights are the "taxicab" distances between each pair of keys.

Now it's a fairly straightforward dynamic programming problem. You can start at either end; it's probably conceptually simpler to start at Start. Keep track of the minimum cost so far to reach each node.

Edward Doolittle
  • 4,002
  • 2
  • 14
  • 27
0

You could use 3D dynamic programming, where each state is (x, y, l) - (x, y) representing current position and l representing what letter you are at.

To explain further. You start at position (0, 0, 0). First letter is "A". You can try all A's and we know that distance will be Manhattan distance (http://en.wikipedia.org/wiki/Taxicab_geometry). Solution for (0, 0, 0) would be minimum of all possibilities.

At each step repeat the above process. Note that importance of memorising each step. In the below sample code memo acts as function, you would use array in reality.

Here is sample pseudo-code:

f(x, y, l):
  if memo(x, y, l) != -1: 
    return memo(x, y, l)   # Check if already calculated.
  if l == length(word): 
    return memo(x, y, l) = 0   # Ending condition.

  memo(x, y, l) = inf
  next_letter = word[l]
  for each (x2, y2) in grid that contains next_letter:
    distance = |x2 - x| + |y2 - y|
    next_calc = f(x2, y2, l+1)
    memo(x, y, l) = min(memo(x, y, l), distance + next_calc)

  return memo(x, y, l)


Set all memo to -1, so we know that no states are calculated.

Solution is f(0, 0, 0).

Let me know which steps I need to clarify further.

Neithrik
  • 2,002
  • 2
  • 20
  • 33