Petya has a rectangular Board of size 𝑛×𝑚. Initially, 𝑘 chips are placed on the board, 𝑖-th chip is located in the cell at the intersection of 𝑠𝑥𝑖-th row and 𝑠𝑦𝑖-th column.
In one action, Petya can move all the chips to the left, right, down or up by 1 cell.
If the chip was in the (𝑥,𝑦) cell, then after the operation:
left, its coordinates will be (𝑥,𝑦−1); right, its coordinates will be (𝑥,𝑦+1); down, its coordinates will be (𝑥+1,𝑦); up, its coordinates will be (𝑥−1,𝑦). If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it’s not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than 2𝑛𝑚 actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2𝑛𝑚 actions.
Input The first line contains three integers 𝑛,𝑚,𝑘 (1≤𝑛,𝑚,𝑘≤200) — the number of rows and columns of the board and the number of chips, respectively.
The next 𝑘 lines contains two integers each 𝑠𝑥𝑖,𝑠𝑦𝑖 (1≤𝑠𝑥𝑖≤𝑛,1≤𝑠𝑦𝑖≤𝑚) — the starting position of the 𝑖-th chip.
The next 𝑘 lines contains two integers each 𝑓𝑥𝑖,𝑓𝑦𝑖 (1≤𝑓𝑥𝑖≤𝑛,1≤𝑓𝑦𝑖≤𝑚) — the position that the 𝑖-chip should visit at least once.
Output In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters 𝐿,𝑅,𝐷,𝑈 respectively.
If the required sequence does not exist, print -1 in the single line.
Examples inputCopy 3 3 2 1 2 2 1 3 3 3 2 outputCopy 3 DRD inputCopy 5 4 3 3 4 3 1 3 3 5 3 1 3 1 4 outputCopy 9 DDLUUUURR
思路: 全部移到原点再逐个遍历
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include <set> #include <map> #include <queue> using namespace std; typedef long long ll; const int maxn = 2e5 + 7; const int mod = 998244353; struct Node { int x,y; }a[maxn]; int main() { int n,m,k;scanf("%d%d%d",&n,&m,&k); int mx = 0,my = 0; for(int i = 1;i <= k;i++) { scanf("%d%d",&a[i].x,&a[i].y); mx = max(mx,a[i].x); my = max(my,a[i].y); } for(int i = 1;i <= k;i++) { int x,y;scanf("%d%d",&x,&y); } printf("%d\n",mx + my - 2 + n * m - 1); for(int i = 1;i < my;i++) { printf("L"); } for(int i = 1;i < mx;i++) { printf("U"); } int flag = 1; for(int i = 1;i <= n;i++) { for(int j = 1;j < m;j++) { if(flag) printf("R"); else printf("L"); } if(i != n) printf("D"); flag ^= 1; } return 0; }