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.
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.
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.
3 3 2 1 2 2 1 3 3 3 2
3 DRD
5 4 3 3 4 3 1 3 3 5 3 1 3 1 4
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。