D - Long Trail 解説 by evima
First, let us rephrase the trail condition. We view the edges of the complete graph as a grid. Specifically, we consider the edge connecting vertices \(i\) and \(j\) \((i < j)\) as the cell \((i,j)\) on the grid. Then, the condition of being a trail can be rephrased as a movement on the grid where horizontal moves and vertical moves alternate.
For example, for \(N=5\), the edges can be represented by the following grid:
Furthermore, the condition \(|v_i-v_{i+2}|=1\) translates to the condition that each move goes to a horizontally or vertically adjacent cell. Based on the above, the problem can be rephrased as follows:
Consider movements in a triangular grid as shown in the image, where horizontal moves to adjacent cells and vertical moves to adjacent cells alternate. The same cell may not be visited more than once. Find a path that makes at least \(\displaystyle \frac{(N-2)^2}2-1\) moves.
From here, we seek an LRUD string representing a valid sequence of moves starting from \((1,2)\), alternating between horizontal and vertical moves.
1. When \(N\le 5\)
Repeat RD \(N-2\) times. This sequence of moves goes from \((1,2)\) to \((N-1,N)\).
2. When \(N= 6\)
Move as RDRURDRDLDRD. This sequence of moves goes from \((1,2)\) to \((N-1,N)\).
3. When \(N\) is even
First, find a path from \((1,2)\) to \((N-5,N-4)\) using the top-left \((N-5)\times (N-5)\) cells. This is expressed as the problem with \(N\) replaced by \(N-4\). Then, by repeating the following operations, we can move to \((N-1,N)\):
- Go up in a zigzag using columns \(N-3\) and \(N-2\).
- Go down in a zigzag using columns \(N-1\) and \(N\).
4. When \(N\) is odd
Proceed as follows:
- Repeat
RDto move to \((N-3,N-1)\). - Go up in a zigzag using columns \(N-1\) and \(N\), moving to \((2,N-1)\).
- Go left in a zigzag using rows \(1\) and \(2\), moving to \((2,5)\).
- Move to \((3,6)\).
- Considering rows \(3\) through \(N-5\) and columns \(5\) through \(N-2\), this reduces to the case where \(N\) is replaced by \(N-6\), so solve the problem recursively.
By solving the above, we can find an LRUD string satisfying the conditions. From this, a trail satisfying the conditions can be easily constructed.
By implementing the above appropriately, you can solve this problem.
投稿日時:
最終更新: