Maximizing the amount of spiral paths on the grid

Allows you to define a spiral path as one who moves in a cycle [right, down, left, up, right, down, left, up, ...] (note that you do not have to start from the right, only this right should appear after how it ended, after the right, etc.). However, the path should not intersect with itself.

Given the grid, how would you find the maximum amount that can be achieved by going this way?

So, for example, if the grid

-5 -4 -3

3 2 1

-1 2 4

The spiral path maximizing the amount is

-5 -4 -3

3 2 1

-1 2 4

The path is as follows: {3, 2, 1, 4, 2}


, - , , . , , .

+4
1

( ). :

  • (, , , )

:

  • . ,

O (n ^ 4), , O (n ^ 5).

, , , , , , f (x1, y1, x2, y2, d) (x1, y1), (x2, y2), , d.

:

  • ,

O (1), . , . O (n ^ 4).

+1

Source: https://habr.com/ru/post/1539355/


All Articles