题解
用DFS跑了一些结论
当n和m有一个为1的时候 只有另一个为2有答案 就是出去再回来 当n和m同时为奇数的时候无答案
当n和m有一个为偶数的时候先沿着偶数的方向走到底(比如n=4先向下3次然后向右)然后蛇形回来即可
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
#ifdef LOCAL
//freopen("C:/input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
if (n % 2 && m % 2)
cout << -1 << endl, exit(0);
if (n == 1 || m == 1)
{
if (n == 2)
cout << "DU" << endl;
else if (m == 2)
cout << "RL" << endl;
else
cout << -1 << endl;
return 0;
}
if (n % 2 == 0)
{
for (int i = 1; i < n; i++)
cout << "D";
cout << "R";
for (int i = 0; i < n; i++)
{
for (int j = 2; j < m; j++)
cout << (i & 1 ? "L" : "R");
cout << (i != n - 1 ? "U" : "L");
}
}
else
{
for (int i = 1; i < m; i++)
cout << "R";
cout << "D";
for (int i = 0; i < m; i++)
{
for (int j = 2; j < n; j++)
cout << (i & 1 ? "U" : "D");
cout << (i != m - 1 ? "L" : "U");
}
}
cout << endl;
return 0;
}