LogoLogo
  • CP Gymnasium
  • Week 3 Math / Number Theory
    • Week 3 Math / Number Theory
      • Problem 1: Tetration
      • Problem 2: Peragrams
      • Problem 3: Veci
      • Problem 4: Architecture
      • Problem 5: Joint Attack
      • Problem 6: How Many Digits?
      • Problem 7: Abstract Painting
  • Week 4 Array / Greedy
    • Week 4 Array / Greedy
      • Problem 1: Vaccine Efficacy
      • Problem 2: Frosh Week
      • Problem 3: Inquiry
      • Problem 4: Bank Queue
      • Problem 5: Log Land
  • Week 6 Sorting / Binary Search
    • Week 6 Sorting / Binary Search
      • Problem 1: Falling Apart
      • Problem 2: Synchronizing Lists
      • Problem 3: Distributing Ballot Boxes
      • Problem 4: Financial Planning
      • Problem 5: Big Boxes
  • Week 7 Dynamic Programming
    • Week 7 Dynamic Programming
      • Problem 1: Ocean's Anti-11
      • Problem 2: Batmanacci
      • Problem 3: Radio Commercials
      • Problem 4: Welcome to Code Jam (Hard)
      • Problem 5: Honeycomb Walk
  • Week 8 Graph Traversals
    • Week 8 Graph Traversals
      • Problem 1: Reachable Roads
      • Problem 2: Money Matters
      • Problem 3: Squawk Virus
      • Problem 4: Beehives
      • Problem 5: Running MoM
      • Problem 6: Amanda Lounges
Powered by GitBook
On this page
  1. Week 7 Dynamic Programming
  2. Week 7 Dynamic Programming

Problem 5: Honeycomb Walk

https://open.kattis.com/problems/honey

PreviousProblem 4: Welcome to Code Jam (Hard)NextWeek 8 Graph Traversals

Last updated 4 years ago

Given a numbernnnbetween 1 and 14, find the number of paths of lengthnnnon a hexagonal grid from the origin to itself.

Use a hexagonal coordinate system to keep track of the number of paths from each point to the origin:

Let's denote C[x][y][n] as the number of paths of length n from point (x,y)(x, y)(x,y) to the origin.

Base cases:

  • C[0][0][0] = 1

    • this means that from all other coordinates on the graph, there are 0 paths of length 0 to the origin

Recurrence relation:

C[x][y][n] = C[x-1][y-1][n-1] + C[x-1][y][n-1] + C[x][y-1][n-1] + C[x+1][y+1][n-1] + C[x+1][y][n-1] + C[x][y+1][n-1]

C[x][y][0] = 0 for all (x,y)(x, y)(x,y) not equal to (0,0)(0, 0)(0,0)

Now consider n=1n = 1n=1:

For n=2n = 2n=2 :

Notice the pattern: from any point (x,y)(x, y)(x,y), the number of paths to the origin of length nnn is the sum of number of paths of length n−1n - 1n−1 from its six neighboring points to the origin.

The solution we want is C[0][0][n]: the number of paths of length nnn from the origin to itself.

Note that our largest given nnn is 14, so the farthest points from the origin are (±7,0),(0,±7),(±7,±7)(±7, 0), (0, ±7), (±7, ±7)(±7,0),(0,±7),(±7,±7). To account for array indexing, we use a matrix of size, say, 17 x 17 x 17. Put the origin at C[8][8].

example hexagonal coordinate system
(each grid shows the number of paths of length 1 to the origin)
(each grid shows the number of paths of length 2 to the origin)