题解:CF1468K The Robot

· · 题解

观察题目,可以发现如果把障碍放在机器人没有经过的地方是无意义的,所以只能把障碍放在机器人一定会走到的地方上,暴力枚举障碍的位置即可。

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 1e6 + 1;

int t, x, y, a[MAXN];
vector<pair<int, int>> c;
map<char, pair<int, int>> mp;
string s;

int main() {
    mp['L'] = {-1, 0}, mp['R'] = {1, 0};
    mp['D'] = {0, -1}, mp['U'] = {0, 1};
    cin >> t;
    while(t--) {
        bool q = false;
        cin >> s;
        int x = 0, y = 0;
        for(int i = 0; i < s.size(); i++) {
            x += mp[s[i]].first, y += mp[s[i]].second;
            if(x != 0 || y != 0) {
                c.push_back({x, y});
            }
        }
        for(int i = 0; i < c.size(); i++) {
            x = 0, y = 0;
            for(int j = 0; j < s.size(); j++) {
                int newx = x + mp[s[j]].first, newy = y + mp[s[j]].second;
                if(c[i].first != newx || c[i].second != newy) {
                    x = newx, y = newy;
                }
            }
            if(x == 0 && y == 0) {
                cout << c[i].first << " " << c[i].second << "\n";
                q = true;
                break;
            }
        }
        if(!q) {
            cout << "0 0\n";
        }
    }
    return 0;
}