Educational Codeforces Round 84 (Rated for Div. 2) - Codeforces 1327 C. Game with Chips - 简单思维+暴力

C. Game with Chips

time limit per test :1 seconds                                memory limit per test :256 megabytes

input :standard input                                         output :standard output
 

Petya has a rectangular Board of size n×m. Initially, k chips are placed on the board, i-th chip is located in the cell at the intersection of sxi-th row and syi-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 (x,y) cell, then after the operation:

  • left, its coordinates will be (x,y−1);
  • right, its coordinates will be (x,y+1);
  • down, its coordinates will be (x+1,y);
  • up, its coordinates will be (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 2nm 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 2nm actions.

Input

The first line contains three integers n,m,k (1≤n,m,k≤200) — the number of rows and columns of the board and the number of chips, respectively.

The next k lines contains two integers each sxi,syi(1≤sxi≤n,1≤syi≤m) — the starting position of the i-th chip.

The next k lines contains two integers each fxi,fyi (1≤fxi≤n,1≤fyi≤m) — the position that the i-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,Urespectively.

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

题目大意

有k个芯片在n×m的矩形网格中,同时有k个目标,每次移动会同时移动所有的芯片,问如何让每个目标点至少被某个芯片访问一次并且每个芯片至少访问一个目标点,不需要操作数最小化,操作数超过2nm输出-1

思路

首先注意看题,如果移动时某些芯片已经到墙壁,那么该芯片不动,且所有芯片可以在一个格子里,那就意味着我们可以用U和L把所有芯片移到点(1,1),再去依次暴力的遍历每个目标点,也可以把整个矩形网格遍历一遍,因为超过2nm才输出-1,所以不存在输出-1的情况。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
struct node{
    int x, y;
}s[N], f[N];
bool cmp(node a, node b){
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
vector<char > ans;
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    int maxx = 0, maxy = 0;
    for (int i = 1; i <= k; ++i){
        scanf("%d %d", &s[i].x, &s[i].y);
        maxx = max(maxx, s[i].x);
        maxy = max(maxy, s[i].y);
    }
    int x = 1, y = 1;
    for (int i = 1; i <= k; ++i){
        scanf("%d %d", &f[i].x, &f[i].y);
    }
    sort(f + 1, f + 1 + k, cmp);
    int num = maxx + maxy - 2;
    for (int i = 1; i <= k; ++i){
        while (f[i].x > x){
            ans.push_back('D');
            x ++;
            num ++;
        }
        while (f[i].x < x){
            ans.push_back('U');
            x --;
            num ++;
        }
        while (f[i].y > y){
            ans.push_back('R');
            y ++;
            num ++;
        }
        while (f[i].y < y){
            ans.push_back('L');
            y --;
            num ++;
        }
    }
    printf("%d\n", num);
    for (int i = 1; i < maxx; ++ i) printf("U");
    for (int i = 1; i < maxy; ++ i) printf("L");
    for (int i = 0; i < ans.size(); ++ i) printf("%c", ans[i]);
    printf("\n");
    return 0;
}
发布了21 篇原创文章 · 获赞 0 · 访问量 817

猜你喜欢

转载自blog.csdn.net/tourist1412/article/details/105077120