C. Game with Chips
题目链接-C. Game with Chips
题目大意
n
×
m
n×m
n×m 的网格图上有 k 个棋子,坐标为 (
s
x
i
,
s
y
i
sxi,syi
sxi,syi) ,每个棋子都有一个对应的必经点坐标 (
f
x
i
,
f
y
i
fxi,fyi
fxi,fyi) 。现在你有最多
2
n
m
2nm
2nm次移动机会,每次移动能够将棋子整体向上下左右四个方向中的一个移动一个单位(如果棋子到达边界将不再移动,棋子可以重叠),给出一种能够让每个棋子到达必经点的方案
解题思路
贪心
因为把整个图遍历一遍也只要
n
×
m
−
1
n×m−1
n×m−1次移动,所以可以先把所有点移至一角,然后遍历整个方块,这样一定不会超过
2
m
n
2mn
2mn次,且一定能经过每个点
坑点:
- L (x,y−1); R (x,y+1); D (x+1,y); U (x−1,y)
附上代码
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
string s;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m,k,x,y;
cin>>n>>m>>k;
for(int i=1;i<=(2*k);i++)
cin>>x>>y;
s+=string(n-1,'D');
s+=string(m-1,'R');
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(i&1) s+="R";
else s+="L";
}
cout<<"D";
}
for(int j=1;j<m;j++){
if(n&1) s+="R";
else s+="L";
}
cout<<s.size()<<endl;
cout<<s<<endl;
return 0;
}