+ import std.math; import std.typecons; - import std.math; /* - - Given a grid with N rows and N columns (N >= 2) and a positive integer k, ? ---- + Given a grid with N rows and N columns (N >= 2) and a positive integer k, - each cell of the grid contains a value. Every integer in the range [1, N * N] ? ---- + each cell of the grid contains a value. Every integer in the range [1, N * N] - inclusive appears exactly once on the cells of the grid. ? ---- + inclusive appears exactly once on the cells of the grid. - - You have to find the minimum path of length k in the grid. You can start ? ---- + You have to find the minimum path of length k in the grid. You can start - from any cell, and in each step you can move to any of the neighbor cells, ? ---- + from any cell, and in each step you can move to any of the neighbor cells, - in other words, you can go to cells which share an edge with you current ? ---- + in other words, you can go to cells which share an edge with you current - cell. + cell. - Please note that a path of length k means visiting exactly k cells (not ? ---- + Please note that a path of length k means visiting exactly k cells (not - necessarily distinct). ? ---- + necessarily distinct). - You CANNOT go off the grid. ? ---- + You CANNOT go off the grid. - A path A (of length k) is considered less than a path B (of length k) if ? ---- + A path A (of length k) is considered less than a path B (of length k) if - after making the ordered arrays of the values on the cells that A and B go ? ---- + after making the ordered arrays of the values on the cells that A and B go - through (let's call them lst_A and lst_B), lst_A is lexicographically less ? ---- + through (let's call them lst_A and lst_B), lst_A is lexicographically less - than lst_B, in other words, there exist an integer index i (1 <= i <= k) ? ---- + than lst_B, in other words, there exist an integer index i (1 <= i <= k) - such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have ? ---- + such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have - lst_A[j] = lst_B[j]. ? ---- + lst_A[j] = lst_B[j]. - It is guaranteed that the answer is unique. ? ---- + It is guaranteed that the answer is unique. - Return an ordered array of the values on the cells that the minimum path go through. ? ---- + Return an ordered array of the values on the cells that the minimum path go through. - - Examples: ? ---- + Examples: - >>> minPath([[1L, 2L, 3L], [4L, 5L, 6L], [7L, 8L, 9L]], 3L) ? ---- + >>> minPath([[1L, 2L, 3L], [4L, 5L, 6L], [7L, 8L, 9L]], 3L) - [1L, 2L, 1L] ? ---- + [1L, 2L, 1L] - - >>> minPath([[5L, 9L, 3L], [4L, 1L, 6L], [7L, 8L, 2L]], 1L) ? ---- + >>> minPath([[5L, 9L, 3L], [4L, 1L, 6L], [7L, 8L, 2L]], 1L) + [1L] - [1L] - */ long[] minPath(long[][] grid, long k)