https://ac.nowcoder.com/acm/contest/330/B

C++版本一

题解:

std

这道题的做法比较开放。只要按题意构造就好了。
大体思路参照下图。

根据 n 和 m 的奇偶分类讨论。

特殊的边界数据:1行2列或2行1列的情况。

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n, m;
    cin >> n >> m;
    if (n == 1)
    {
        if (m == 2)
            cout << "RL" << endl;
        else
            cout << -1 << endl;
    }
    else if (m == 1)
    {
        if (n == 2)
            cout << "DU" << endl;
        else
            cout << -1 << endl;
    }
    else if (n % 2 == 0)
    {
        cout << "R";
        for (int i = 0; i < n; i += 2)
        {
            if (i)
                cout << "D";
            for (int j = 1; j < m - 1; j++)
                cout << "R";
            cout << "D";
            for (int j = 1; j < m - 1; j++)
                cout << "L";
        }
        cout << "L";
        for (int i = 0; i < n - 1; i++)
            cout << "U";
        cout << endl;
    }
    else if (m % 2 == 0)
    {
        cout << "D";
        for (int i = 0; i < m; i += 2)
        {
            if (i)
                cout << "R";
            for (int j = 1; j < n - 1; j++)
                cout << "D";
            cout << "R";
            for (int j = 1; j < n - 1; j++)
                cout << "U";
        }
        cout << "U";
        for (int i = 0; i < m - 1; i++)
            cout << "L";
        cout << endl;
    }
    else
        cout << -1 << endl;
    return 0;
}

’C++版本二

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[N][N];
string str;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d",&n,&m);
    //scanf("%d",&t);
    //while(t--){}
    if(m==2&&n==1){
        cout<<"RL"<<endl;
        return 0;
    }
    if(n==2&&m==1){
        cout<<"DU"<<endl;
        return 0;
    }
    if((n%2&&m%2)||n==1||m==1){
        cout<<-1<<endl;
    }else if(n%2==0){
        for(int i=1;i<n;i++)
            str+='D';
        str+='R';
        for(int i=1;i<=n/2;i++){
            for(int j=1;j<m-1;j++)
                str+='R';
            str+='U';
            for(int j=1;j<m-1;j++)
                str+='L';
            if(i!=n/2)
                str+='U';
        }
        str+='L';
        cout<<str<<endl;
    }else if(m%2==0){
        for(int i=1;i<m;i++)
            str+='R';
        str+='D';
        for(int i=1;i<=m/2;i++){
            for(int j=1;j<n-1;j++)
                str+='D';
            str+='L';
            for(int j=1;j<n-1;j++)
                str+='U';
            if(i!=m/2)
                str+='L';
        }
        str+='U';
        cout<<str<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}