Applese 走方格
生活随笔
收集整理的這篇文章主要介紹了
Applese 走方格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;elsecout << -1 << endl;}else if (m == 1){if (n == 2)cout << "DU" << endl;elsecout << -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;}elsecout << -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 DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifscanf("%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; }?
總結
以上是生活随笔為你收集整理的Applese 走方格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Applese 的取石子游戏
- 下一篇: Applese 走迷宫