4

I'm practicing up for a programming competition, and i'm going over some difficult problems that I wasn't able to answer in the past. One of them was the King's Maze. Essentially you are given an NxN array of numbers -50<x<50 that represent "tokens". You have to start at position 1,1(i assume that's 0,0 in array indexes) and finish at N,N. You have to pick up tokens on cells you visit, and you can't step on a cell with no token (represented by a 0). If you get surrounded by 0's you lose. If there is no solution to a maze you output "No solution". Else, you output the highest possible number you can get from adding up the tokens you pick up.

I have no idea how to solve this problem. I figured you could write a maze algorithm to solve it, but that takes time, and in programming contests you are only given two hours to solve multiple problems. I'm guessing there's some sort of pattern i'm missing. Anyone know how I should approach this?

Also, it might help to mention that this problem is meant for high school students.

false
  • 10,264
  • 13
  • 101
  • 209

3 Answers3

3

This type of problem is typically solved using dynamic programming or memoization.

Basically you formulate a recursive solution, and solve it bottom up while remembering and reusing previously computed results.

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

The simple approach (i.e. simplest to code) is try all the possible paths - try each first step; for each first step try each second step; for each first step/second step combination try each third step; and so on. However depending on how big the maze is this may take too long to run (or it may not).

Your next step is to think about how you can do this faster. The first step is usually to eliminate moves that you know can't lead to a finish, or can't lead to a finish with higher points than the one you already have. Since this is practice for a competition we'll leave you to do this work yourself.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
0

Think "graph" algorithms: The Algorithm Design Manual

duffymo
  • 305,152
  • 44
  • 369
  • 561