Competitive Foraging
Learn this problemProblem statement
You and a deterministic rival forage for food on a 15-by-15 grid. You move first in every round, then the rival moves if food remains. Each food cell is worth one point to whichever forager reaches it first. The game ends as soon as all food has been eaten.
Grid cells
.is an empty cell.#is a wall.Pis your starting cell.Ois the rival's starting cell.Fis a food cell.
Your move
Choose one of U, D, L, R, or S for up, down, left, right, or stay. A move is valid only when its destination remains on the grid, is not a wall, and is not the rival's current cell. If you enter a food cell, you eat that food and score one point.
The rival's move
If food remains after your move, the rival takes one step toward the nearest food it can reach without entering a wall or your current cell. Distances are unweighted BFS distances. Among legal adjacent steps that attain the smallest distance to any remaining food, the rival chooses the first in this order: up, down, left, right. If no food is reachable, the rival stays. If the rival enters a food cell, it eats that food and scores one point.
Required result
Return the shortest valid move string that ends the game with your score strictly greater than the rival's score. Among equal-length winning strings, return the first string under the move-priority order U, D, L, R, S. Do not include moves after the game ends.
For this exercise, assume the game stops immediately when you eat the final food, before the rival would move in that round. A winning sequence is guaranteed to exist.
Function
competitiveForaging(grid: String[]) → StringExamples
Example 1
grid = ["###############","#PFO###########","###############","###############","###############","###############","###############","###############","###############","###############","###############","###############","###############","###############","###############"]return = "R"Move right onto the only food cell. Your score becomes 1, the rival's score remains 0, and the game ends before the rival moves. No winning string can be shorter than one move.
Example 2
grid = ["###############","#..F.P#########","#....O#########","#...F.#########","#FF...#########","###############","###############","###############","###############","###############","###############","###############","###############","###############","###############"]return = "LDLSUDDDLL"The rival eats the food at row 3, column 4 after the second player move. The stay move on turn 4 lets the rival leave the route to the upper food. You then eat the upper food and both lower foods, finishing with scores 3 to 1. Exhaustive breadth-first search finds no shorter winning string, and this is the first winning string at length 10 under the required move order.
Constraints
grid.length == 15.grid[r].length == 15for every rowr.- Every cell is one of
.,#,P,O, orF. - There is exactly one
P, exactly oneO, and between1and6food cells. - There are at most
30non-wall cells. - At least one valid winning move string exists.