Educational Codeforces Round 84 (Rated for Div. 2) C. Game with Chips(思维题)

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

Copy

3 3 2
1 2
2 1
3 3
3 2

Output

Copy

3
DRD

Input

Copy

5 4 3
3 4
3 1
3 3
5 3
1 3
1 4

Output

Copy

9
DDLUUUURR这题只要不被样例和题干迷惑就很好想了。注意这么几个点:1.一个chip如果到了边界,再把它往边界怼的话直接原地不动。2.多个chip可以堆在一起。3.题目只是限制了操作步数,并没有让求最小值,所以我们有理由相信这个题也用了SPJ。这样就好考虑了,一开始先别管chips的位置以及要移动到哪里,直接移动n+m-2步,把所有薯片移到右下角。即n-1次D和m-1次R。这样所有薯片就怼到一起了,再把这一撮薯片蛇形移动遍历整个地图即可。这样能保证至少经过一次,同时总次数为mn+n+m-3一定不会超过2mn,因此一定有解。
#include <bits/stdc++.h>
using namespace std;
int n,m,k;
int main()
{
    cin>>n>>m>>k;
    int i;
    for(i=1;i<=k;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
    }
    for(i=1;i<=k;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
    }
    vector<char>v;
    for(i=1;i<=n-1;i++)
    {
        v.push_back(‘D‘);
    }
    for(i=1;i<=m-1;i++)
    {
        v.push_back(‘R‘);
    }//已经到了右下角了
    int x,y;
    //走完整个棋盘!!
    if(m%2!=0)
    {
        int num=(m-1)/2;
        int j,k;
        for(i=1;i<=num;i++)
        {
            for(j=1;j<=n-1;j++)v.push_back(‘U‘);
            v.push_back(‘L‘);
            for(j=1;j<=n-1;j++)v.push_back(‘D‘);
            v.push_back(‘L‘);
        }
        for(j=1;j<=n-1;j++)v.push_back(‘U‘);

    }
    else
    {
        int num=(m-2)/2;
        int j,k;
        for(i=1;i<=num;i++)
        {
            for(j=1;j<=n-1;j++)v.push_back(‘U‘);
            v.push_back(‘L‘);
            for(j=1;j<=n-1;j++)v.push_back(‘D‘);
            v.push_back(‘L‘);
        }
        for(j=1;j<=n-1;j++)v.push_back(‘U‘);
        v.push_back(‘L‘);
        for(j=1;j<=n-1;j++)v.push_back(‘D‘);
    }
    cout<<v.size()<<endl;
    for(i=0;i<v.size();i++)cout<<v[i];
} 

这个题的点坐标并没有用,也是nb。

原文地址:https://www.cnblogs.com/lipoicyclic/p/12563488.html

时间: 2024-09-30 00:41:24

Educational Codeforces Round 84 (Rated for Div. 2) C. Game with Chips(思维题)的相关文章

C - Game with Chips.Educational Codeforces Round 84 (Rated for Div. 2)

http://codeforces.com/contest/1327/problem/C 题意 给你一个图和一堆点,然后问你这一堆点和一堆目标点怎么才能到达这些目标点至少一次. 做法 其实题目已经给你提示了,上面说移动次数不大于2nm. 其实在2nm内就能把图上所有位置遍历一遍. 简单来说就是不管你脑洞开的有多大,没有用.该暴力还是得暴力. 先把最右上角的点移动到左下角,再按照s型移动到原始位置 就能保证所有点都经历过这个图上别的所有点了. 代码 #include <iostream> usi

Educational Codeforces Round 84 (Rated for Div. 2)

原题链接 目录 题外话 A A题意 A思路 A代码 B 题意 思路 代码 C 题意 思路 代码 E 题意 思路 代码 题外话 被教育了,读题不认真,明明能四题的(靠),竟然打不过jy,很烦 A A题意 给你n,m(m是奇数的数量)问你是否可以使用m个奇数(不相同的)构成n A思路 自己上来以为是判断奇偶就死了 其实还有点其他的东西,比如k个互不相同的奇数最小就是k*k(记录一下) A代码 #include <bits/stdc++.h> #include <ext/pb_ds/assoc

Educational Codeforces Round 84 (Rated for Div. 2), problem: (A) Sum of Odd Integers

A. 被卡了好久,样例的第一感觉n%k==0则YES 后来写式子,交了发n>=k*k 虽然写的时候也注意到了n-k必须是偶数(k个奇数和的奇偶性与k相同,故n与k奇偶性相同) 最后才想到,可以构造 前k-1个数为1~k-1 剩下的即为第k个数 #include<bits/stdc++.h> #define ll long long using namespace std; //const int N=1e4+5; int main(){ int T; ios::sync_with_std

Educational Codeforces Round 84 (Rated for Div. 2) A-E题解

A. Sum of Odd Integers 首先可以算出从1开始到第k个奇数之和.如果和大于n,则不可能存在k个奇数加和等于n,否则用n减去前k个奇数的和,这个差值若是偶数,直接加到最大的奇数上,就可以满足题意要求,否则输出no. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int main(){ 5 int t; 6 cin>>t; 7 while(t--){ 8 ll n

Educational Codeforces Round 84 (Rated for Div. 2)E(组合数学)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 const int mod = 998244353; 5 long long p[200007]; 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(NULL); 9 cout.tie(NULL); 10 int n; 11 cin>>n; 12 p[0]=1;

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.