#P8693. Maze Diet Challenge

    ID: 21859 Type: Default 1000ms 256MiB

Maze Diet Challenge

Maze Diet Challenge

Little Ming is exceptionally fat – so fat that whereas a normal person occupies a $1\times 1$ area, Little Ming occupies a $5\times 5$ area. His position is defined as the very center of his occupied block.

A maze has been designed as an $n\times n$ grid with walls along its boundary. The maze has a fixed start and end. The start is set at cell (3, 3) and the end at cell (n-2, n-2) (cells are 1-indexed). Note that the maze itself (the area of motion) is the entire grid, and the walls exist only outside the grid; hence, a move is valid as long as the whole block that Little Ming occupies is inside the maze.

At time 0, Little Ming starts at the beginning of the maze in his fat form, occupying a $5\times 5$ block (with his position being the center cell). Being very fat, his movement is constrained because his center must be such that his block is entirely within the maze. Therefore, when he is fat, his center must satisfy:

$$r-2 \ge 1 \quad \text{and} \quad r+2 \le n, \quad \text{and similarly for the column.} $$

At each unit time, he may move one unit up, down, left, right, or simply wait. However, his shape changes as time passes:

  • For time t with t < k, he remains fat and occupies a $5\times 5$ block. (His center must satisfy \(r-2 \ge 1\) and \(r+2 \le n\).)
  • At time k, he becomes slightly slimmer, turning into a "chubby" form that occupies a $3\times 3$ block. (Now his center must satisfy \(r-1 \ge 1\) and \(r+1 \le n\).)
  • At time 2k and beyond, he becomes a normal person, occupying a $1\times 1$ area.

Note that the start and end points (3, 3) and (n-2, n-2) remain unchanged regardless of his shape. Little Ming wants to complete the maze as quickly as possible. Determine the minimum time required for him to reach the end cell from the start cell, taking into account the movement restrictions imposed by his changing form.

Observation: When Little Ming is fat (before time k), his center can only be at cells such that the entire $5\times 5$ block is within the maze; this forces his center to lie in the range [3, n-2] in both dimensions. Notice that the straight Manhattan path from (3,3) to (n-2, n-2) lies completely within that range. Hence, if he has enough time to complete the maze while remaining in his fat form (i.e. if the Manhattan distance is less than or equal to k-1), he finishes before transforming. Otherwise, he must continue in later phases. It turns out that the minimum time required is given by:

d=2×(n5),d=2\times (n-5),

with one small adjustment: if k = 1 (meaning he instantly becomes chubby before making any move) and the start and end coincide only if n=5, then an extra unit of time is required. In summary, let

d=2×(n5).d=2\times (n-5).

If d = 0 (i.e. n = 5 and the start equals the end), output 0. Otherwise, output d+1 if k = 1, and output d if k > 1.

inputFormat

The input consists of two space‐separated integers n and k.

Constraints:

  • n (the maze size) is at least 5.
  • k is a positive integer.

outputFormat

Output a single integer representing the minimum time Little Ming needs to reach the cell (n-2, n-2) from (3, 3) under the movement restrictions of his current form.

sample

7 1
5