Petya has a rectangular Board of size n×mn×m. Initially, kk chips are placed on the board, ii-th chip is located in the cell at the intersection of sxisxi-th row and syisyi-th column.
In one action, Petya can move all the chips to the left, right, down or up by 11 cell.
If the chip was in the (x,y)(x,y) cell, then after the operation:
- left, its coordinates will be (x,y−1)(x,y−1);
- right, its coordinates will be (x,y+1)(x,y+1);
- down, its coordinates will be (x+1,y)(x+1,y);
- up, its coordinates will be (x−1,y)(x−1,y).
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 2nm2nm 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 2nm2nm actions.
Input
The first line contains three integers n,m,kn,m,k (1≤n,m,k≤2001≤n,m,k≤200) — the number of rows and columns of the board and the number of chips, respectively.
The next kk lines contains two integers each sxi,syisxi,syi (1≤sxi≤n,1≤syi≤m1≤sxi≤n,1≤syi≤m) — the starting position of the ii-th chip.
The next kk lines contains two integers each fxi,fyifxi,fyi (1≤fxi≤n,1≤fyi≤m1≤fxi≤n,1≤fyi≤m) — the position that the ii-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 L,R,D,UL,R,D,U respectively.
If the required sequence does not exist, print -1 in the single line.
Examples
Input
3 3 2 1 2 2 1 3 3 3 2
Output
3 DRD
Input
5 4 3 3 4 3 1 3 3 5 3 1 3 1 4
Output
9 DDLUUUURR
题意:给定一个n*m的棋盘,棋盘中有k个棋子,接下来k行输入棋子的位置(数组的行标、列标),再接下来的k行未希望棋子移动到的位置。你可以上下左右移动所有的棋子,完成棋子的移动以后输出移动轨迹,左L,右R,上U,下D。
题解:看完题目我以为要寻找出最优解,然后我默默地点开了下一题,这种图的遍历我确实还有点菜。直到我去找题解,看博客,我才发现我上当了,根本不是最优解!你甚至都不用管棋子的初始位置在哪,需要移动到哪。你只要将棋子认定在棋盘的最右下角,然后将其移动到最左上角,然后开启你的蛇形走位,将整个棋盘遍历完,这样能保证经过过题目要求的任何位置,题目的输出不唯一,而且还不会超时。简直无脑暴力就能解决,我感觉受到了很大欺骗和侮辱。
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, k, a, b, cnt;
char str[40005];
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i=0; i<2*k; i++)
scanf("%d%d", &a, &b);
for(int i=1; i<m; i++) str[cnt++] = 'L';
for(int i=1; i<n; i++) str[cnt++] = 'U';
for(int i=1; i<=n; i++){
if(i%2)
for(int i=1; i<m; i++)
str[cnt++] = 'R';
else
for(int i=1; i<m; i++)
str[cnt++] = 'L';
if(i!=n) str[cnt++] = 'D';
}
printf("%d\n%s", cnt, str);
return 0;
}