codeforces Round #389(Div.2)C Santa Claus and Robot(思维题)

题目链接:http://codeforces.com/contest/752/problem/C

题意:给出一系列机器人的行动方向(机器人会走任意一条最短路径),问最少标记几个点能让机器人按这个

路径走下去。

显然最后位置肯定要标记,然后怎么使得点最少呢。首先标记一下起点由于机器人走的是最短路一旦第i个点到

标记点的距离小于第i-1个点到标记点的距离是肯定不能这么走,于是这时就要标记一下上个点的位置,统计数

加1。

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int M = 2e5 + 10;
int a[M];
int main() {
    string s;
    int n;
    cin >> n;
    cin >> s;
    int len = s.size();
    int count = 0;
    int x = 0 , y = 0 , stax = 0 , stay = 0;
    int cp = 0;
    for(int i = 0 ; i < n ; i++) {
        if(s[i] == 'U') {
            y++;
            int gg = abs(y - stay) + abs(x - stax);
            if(gg >= cp) {
                cp = gg;
            }
            else {
                count++;
                stax = x;
                stay = y - 1;
                cp = 1;
            }
        }
        if(s[i] == 'D') {
            y--;
            int gg = abs(y - stay) + abs(x - stax);
            if(gg >= cp) {
                cp = gg;
            }
            else {
                count++;
                stax = x;
                stay = y + 1;
                cp = 1;
            }
        }
        if(s[i] == 'L') {
            x--;
            int gg = abs(y - stay) + abs(x - stax);
            if(gg >= cp) {
                cp = gg;
            }
            else {
                count++;
                stax = x + 1;
                stay = y;
                cp = 1;
            }
        }
        if(s[i] == 'R') {
            x++;
            int gg = abs(y - stay) + abs(x - stax);
            if(gg >= cp) {
                cp = gg;
            }
            else {
                count++;
                stax = x - 1;
                stay = y;
                cp = 1;
            }
        }
    }
    cout << count + 1 << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/TnT2333333/p/6224191.html