Game with Chips

Game with Chips

Game with Chips 题目链接

思路

一开始以为还要用bfs或者dfs来写,然后仔细想一想,div2的C题应该没这么复杂吧。果然,这就是一道脑洞题,我们发现题目给的数据是步数小于等于 (2nm) 这足够走两遍完整的地图了,于是有了下面的做法。

我们把所有的点都移到四个角中其中任意一个,现在所有的点都在同一个角上了,所以我们只需要用一个蛇形来遍历整个地图就行了,总的步数是((n + m - 1) + (n * m - 1) < (2 * n * m))

代码

这里采用的做法是把所有的点都移到左上角。

#include<bits/stdc++.h>
using namespace std;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    int n, m, k, x, y;
    cin >> n >> m >> k;
    for(int i = 0; i < k; i++)
        cin >> x >> y;
    for(int i = 0; i < k; i++)
        cin >> x >> y;
    cout << ((n + m - 2) + (n * m - 1)) << endl;
    for(int i = 1; i < n; i++)  cout << "U";
    for(int i = 1; i < m; i++)  cout << "L";
    for(int i = 1; i <= n; i++) {
        if(i != 1)  cout << "D";
        for(int j = 1; j < m; j++)
            if(i & 1)   cout << "R";
            else cout << "L";
    }
    cout << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/lifehappiness/p/12802664.html