CodeForces 1327C - Game with Chips(思维)
C. Game with Chips
time limit per test 1 second
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,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
C.筹码游戏
每次测试的时限1秒
每个测试的内存限制256 MB
输入标准输入
输出标准输出
Petya具有大小为n×m的矩形板。最初,将k个芯片放置在板上,第i个芯片位于单元中的第sxi行和syi-th列相交处。
在一次动作中,Petya可以将所有筹码向左,向右,向下或向上移动1个单元。
如果芯片在(x,y)单元中,则在操作之后:
左,其坐标将为(x,y−1);
正确,其坐标将为(x,y + 1);
向下,其坐标将为(x + 1,y);
向上,其坐标将为(x-1,y)。
如果芯片位于板壁的旁边,并且Petya选择的动作将其移向壁,则芯片将保持在其当前位置。
请注意,几个芯片可以位于同一单元中。
Petya为每个芯片选择了应该访问的位置。请注意,芯片不必最终停留在该位置。
由于Petya没有太多的空闲时间,因此他准备做的动作不超过2nm。
您必须找出Petya应该采取的行动,以便每个筹码至少访问一次Petya为其选择的位置。或确定不可能在2nm动作中执行此操作。
输入值
第一行包含三个整数n,m,k(1≤n,m,k≤200)-电路板的行数和列数以及芯片数。
接下来的k行包含两个整数,每个sxi,syi(1≤sxi≤n,1≤syi≤m)—第i个芯片的起始位置。
接下来的k行包含两个整数,每个整数fxi,fyi(1≤fxi≤n,1≤fyi≤m)— i芯片应至少访问一次的位置。
输出量
在第一行中,打印操作数,以便每个芯片至少访问一次Petya为其选择的位置。
在第二行输出操作顺序。要指示左,右,下和上的操作,请分别使用字符L,R,D,U。
如果所需的序列不存在,则在单行中打印-1。
例子
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 给出n x m大小的空间,然后接下来有2*k行,表示第 i 行的点要在2nm步之内访问到 i + k 行的点,求k-2k行的点全部访问后的步数以及操作方向。
解题思路:这个题我一开始没有读懂,他所指的移动是第1行到第k行的所有点一起移动,还有就是关于移动的方向,L,R,U,D,这里的 x±1 和 y±1 指的不是坐标,而是行标和列标,比如 left, its coordinates will be (x,y−1); 向左,这里的y-1不是说坐标的 y - 1,而是表示列标减一,也就是说坐标变化应该是(x-1,y),其实正常来说就是我们所说的上下左右(大坑),还有比较坑的一点是,这个题还有一句话,就是当某个点到达边界以后就停止运动了。比如到最左端以后就不能继续往左了,但是能向上向下。而且点与点是可以重合的,思路就是先把所有点都移到地图的左上方,然后蛇形遍历地图,就一定可以在2nm步之内访问到所有要访问的点了。AC代码:
#include <iostream>
#include <cstring>
using namespace std;
string s;
int main()
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=2*k;i++)
{
int a,b;
cin>>a>>b;
}
for(int i=1;i<=m-1;i++)
s+='L';
for(int i=1;i<=n-1;i++)
s+='U';//先移动到左上方
for(int i=1;i<=n;i++)
{
if(i%2)//蛇形遍历地图,判断向左还是向右
for(int j=1;j<=m-1;j++)
s+='R';
else
for(int j=1;j<=m-1;j++)
s+='L';
if(i!=n)//走到边界要向下
s+='D';
}
cout<<s.length()<<endl;
cout<<s<<endl;
return 0;
}