一、内容
Bashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going to quit programming after the national contest and he is going to become an actor (just like his father), so he should lose weight.
In order to lose weight, Bashar is going to run for k
kilometers. Bashar is going to run in a place that looks like a grid of n rows and m columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly (4nm−2n−2m)
roads.
Let's take, for example, n=3
and m=4. In this case, there are 34
roads. It is the picture of this case (arrows describe roads):
Bashar wants to run by these rules:
He starts at the top-left cell in the grid;
In one move Bashar may go up (the symbol 'U'), down (the symbol 'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he stands in the cell in the row i
and in the column j, i.e. in the cell (i,j) he will move to:
in the case 'U' to the cell (i−1,j)
;
in the case 'D' to the cell (i+1,j)
;
in the case 'L' to the cell (i,j−1)
;
in the case 'R' to the cell (i,j+1)
;
He wants to run exactly k
kilometers, so he wants to make exactly k
moves;
Bashar can finish in any cell of the grid;
He can't go out of the grid so at any moment of the time he should be on some cell;
Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times.
Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run.
You should give him a
steps to do and since Bashar can't remember too many steps, a should not exceed 3000. In every step, you should give him an integer f and a string of moves s of length at most 4 which means that he should repeat the moves in the string s for f
times. He will perform the steps in the order you print them.
For example, if the steps are 2
RUD, 3 UUL then the moves he is going to move are RUD + RUD + UUL + UUL + UUL =
RUDRUDUULUULUUL.
Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to k
kilometers or say, that it is impossible?
Input
The only line contains three integers n
, m and k (1≤n,m≤500, 1≤k≤109
), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run.
Output
If there is no possible way to run k
kilometers, print "NO" (without quotes), otherwise print "YES" (without quotes) in the first line.
If the answer is "YES", on the second line print an integer a
(1≤a≤3000) — the number of steps, then print a
lines describing the steps.
To describe a step, print an integer f
(1≤f≤109) and a string of moves s of length at most 4. Every character in s
should be 'U', 'D', 'L' or 'R'.
Bashar will start from the top-left cell. Make sure to move exactly k
moves without visiting the same road twice and without going outside the grid. He can finish at any cell.
We can show that if it is possible to run exactly k
kilometers, then it is possible to describe the path under such output constraints.
Examples
Input
3 3 4
Output
YES
2
2 R
2 L
二、思路
三、代码
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int f;
string s;
Node(int f, string s): f(f), s(s) {}
};
int n, m, k;
vector<Node> p;
void add(int mx, string s) {
if (k <= 0 || mx == 0) return; //不能移动
int len = s.length();
if (k <= mx * len) {
//代表可以执行k次操作
//这种情况 只需要UD UD不行那么后面U就可以补上
if (k / len <= 0 ) { len = 2, s = "UD";} //防止一次也用不上
if (k / len <= 0) return;
p.push_back(Node(k / len, s));
k -= k / len * len;
//防止UDL没有用完
if (k / len <= 0 ) { len = 2, s = "UD";} //防止用完UDL还剩2个
if (k / len <= 0) return;
p.push_back(Node(k / len, s));
k -= k / len * len;
//代表可以执行mx次操作
} else k -= mx * len, p.push_back(Node(mx, s));
}
int main() {
scanf("%d%d%d", &n, &m, &k);
if (k > 4 * n * m - 2 * n - 2 * m) {
printf("NO"); return 0;
}
//分成4个大方向
//1. 向右 R 2.再向左
add(m - 1, "R"); add(m - 1, "L");
//3. 向下 D
add(n - 1, "D");
//4. 这一行的 RUD 、L 、U
for (int i = 1; i < n; i++) add(m - 1, "R"), add(m - 1, "UDL"), add(1, "U");
cout << "YES" << endl << p.size() << endl;
for (int i = 0; i < p.size(); i++) cout << p[i].f << " " << p[i].s << endl;
return 0;
}