#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 RR-by-CC grid AA and may not leave this grid. The cell in row ii, column jj is denoted Ai,jA_{i, j}. Each cell has an integer weight w(Ai,j)w(A_{i, j}). 0w(Ai,j)80 \leq w(A_{i, j}) \leq 8. When w(Ai,j)=0w(A_{i, j}) = 0, Ai,jA_{i, j} is forbidden to enter; when w(Ai,j)>0w(A_{i, j}) > 0, Ai,jA_{i, j} is allowed to enter.

Directions:

For P=(X0,Y0)P = (X_0, Y_0) and Q=(X1,Y1)Q = (X_1, Y_1), there are four basic directions:

  • Left (L): if X0=X1X_0 = X_1 and Y0=Y11Y_0 = Y_1 - 1, then PP is to the left of QQ.
  • Right (R): if X0=X1X_0 = X_1 and Y0=Y1+1Y_0 = Y_1 + 1, then PP is to the right of QQ.
  • Up (U): if X0=X11X_0 = X_1 - 1 and Y0=Y1Y_0 = Y_1, then PP is above QQ.
  • Down (D): if X0=X1+1X_0 = X_1 + 1 and Y0=Y1Y_0 = Y_1, then PP is below QQ.

Snake:

The snake BB is a shape occupying several cells. The number of occupied cells is the snake’s length, denoted mm. From head to tail, the snake is B1,B2,,BmB_1, B_2, \dots, B_m. Let pp be the snake’s configuration. If BiB_i is at row XiX_i, column YiY_i, then p(Bi)=(Xi,Yi)p(B_i) = (X_i, Y_i). Initially, m=4m = 4, and during movement the following constraints must always hold:

  • For adjacent parts BiB_i and Bi+1B_{i+1} (1i<m1 \leq i < m), BiB_i must be in one of the L, R, U, D directions relative to Bi+1B_{i+1}.
  • For any BiB_i and BjB_j (1i<jm1 \leq i < j \leq m), with p(Bi)=(Xi,Yi)p(B_i) = (X_i, Y_i) and p(Bj)=(Xj,Yj)p(B_j) = (X_j, Y_j), it must hold that XiXjX_i \neq X_j or YiYjY_i \neq Y_j. 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 Ai,jA_{i, j} in the L, R, U, or D direction from the head B1B_1 is enterable and there is no food on Ai,jA_{i, j}, the snake may move in that direction, and the new head will be at Ai,jA_{i, j}. Let pp' be the new configuration of the snake. Then:

  • p(Bk)=p(Bk1)p'(B_k) = p(B_{k - 1}) for 2km2 \leq k \leq m.
  • p(Bk)=(i,j)p'(B_k) = (i, j) for k=1k = 1.

Snake eating:

If one of the cells Ai,jA_{i, j} in the L, R, U, or D direction from the head B1B_1 is enterable and there is food on Ai,jA_{i, j}, the snake may move there to eat. The new head will be at Ai,jA_{i, j}, and the new length is m=m+1m' = m + 1. Let pp' be the new configuration. Then:

  • p(Bk)=p(Bk1)p'(B_k) = p(B_{k - 1}) for 2km2 \leq k \leq m'.
  • p(Bk)=(i,j)p'(B_k) = (i, j) for k=1k = 1.

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 PP be the cell of the head before the action, and let QQ be the cell of the head after the action. Then the time cost of this action is w(P)w(Q)+1|w(P) - w(Q)| + 1.

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 R,CR, C.

Then follow RR lines, each containing CC digits without spaces. In the ii-th line, the jj-th digit is w(Ai,j)w(A_{i, j}).

Then follow 44 lines, each containing 22 positive integers. In the ii-th of these lines, the two integers Xi,YiX_i, Y_i indicate p(Bi)=(Xi,Yi)p(B_i) = (X_i, Y_i).

Then a positive integer NN follows, denoting the number of food items.

Then follow NN lines, each containing 22 positive integers i,ji, j, indicating that there is a piece of food on Ai,jA_{i, j}.

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 20%20\% of the testdata, N1N \leq 1.
  • For 30%30\% of the testdata, R×C36R \times C \leq 36.
  • For 40%40\% of the testdata, N2N \leq 2.
  • For 60%60\% of the testdata, N3N \leq 3.
  • For 100%100\% of the testdata, N4N \leq 4, R12R \leq 12, C12C \leq 12.

Translated by ChatGPT 5