#P2493. [SDOI2011] 贪食蛇
[SDOI2011] 贪食蛇
Description
Everyone has probably played the Snake game. Now there is a modified version. As in the traditional Snake game, the snake moves within a play area and eats food, but this modified version has some special rules.
Play area:
The snake moves in an -by- grid and may not leave this grid. The cell in row , column is denoted . Each cell has an integer weight . . When , is forbidden to enter; when , is allowed to enter.
Directions:
For and , there are four basic directions:
- Left (L): if and , then is to the left of .
- Right (R): if and , then is to the right of .
- Up (U): if and , then is above .
- Down (D): if and , then is below .
Snake:
The snake is a shape occupying several cells. The number of occupied cells is the snake’s length, denoted . From head to tail, the snake is . Let be the snake’s configuration. If is at row , column , then . Initially, , and during movement the following constraints must always hold:
- For adjacent parts and (), must be in one of the L, R, U, D directions relative to .
- For any and (), with and , it must hold that or . In other words, no two parts of the snake’s body may overlap.
Food:
There is some food in the play area. Each piece of food is on a cell that is allowed to enter, and food pieces do not overlap. Each piece of food can be eaten at most once.
Snake movement:
If one of the cells in the L, R, U, or D direction from the head is enterable and there is no food on , the snake may move in that direction, and the new head will be at . Let be the new configuration of the snake. Then:
- for .
- for .
Snake eating:
If one of the cells in the L, R, U, or D direction from the head is enterable and there is food on , the snake may move there to eat. The new head will be at , and the new length is . Let be the new configuration. Then:
- for .
- for .
Note: After moving or eating, you only need to check whether the resulting configuration satisfies the constraints; you do not need to consider the intermediate process. In other words, it is allowed for a snake whose current configuration is legal to move its head into the previous position of the tail, because the head and tail still do not overlap after the transformation.
Time required for movement or eating:
Movement or eating consumes time. Let be the cell of the head before the action, and let be the cell of the head after the action. Then the time cost of this action is .
Before the game begins, the snake’s initial position and all food positions are given. Your task is to make the snake eat all the food in the least possible time.
Input Format
The first line contains two positive integers .
Then follow lines, each containing digits without spaces. In the -th line, the -th digit is .
Then follow lines, each containing positive integers. In the -th of these lines, the two integers indicate .
Then a positive integer follows, denoting the number of food items.
Then follow lines, each containing positive integers , indicating that there is a piece of food on .
Output Format
If the snake cannot eat all the food, output No solution..
Otherwise, output:
- The first line contains an integer, the minimum total time required.
- The second line contains a string consisting of characters L, R, U, and D, representing a movement plan for the snake. If multiple solutions exist, output any one of them.
5 5
11011
11011
11011
11011
11411
1 1
2 1
3 1
4 1
4
5 5
4 4
2 5
1 4
21
RDDDDRRRULURULU
Hint
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号