NASSA’s Robot

NASSA的机器人降落到了火星,降落的地方可以用X-Y坐标表示。机器人最开始在(0, 0)。由于传输问题,部分指令可能会混淆,现在给出确定的命令与未知命令,请帮忙确认机器人的X、Y坐标最小最大值分别是多少。

输出格式为:

X最小 Y最小 X最大 Y最大

模拟一下数据就能明白,走到的位置和命令执行顺序没有关系。

例如:RLL与RLR走到的位置都是一样的。

因此,只要统计一下指令就好了。因为RL是一对相反的指令,UD是一对相反的指令。所以相减就能得到除不确定 指令外的最终位置。而不确定的命令在极端情况下可能全是RLUD四条命令中的任意一个。所以用最终位置的坐标加减不确定命令的数量就可以得到所要求的值。

下面附上代码:

/*
 * Problem: F
 *    Date: 2014-7-20
 *  Author: Wuhen
*/
#include <map>
#include <list>
#include <queue>
#include <string>
#include <vector>
#include <cstdarg>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define LL long long
#define Clean(a) memset(a, 0, sizeof(a))

using namespace std;

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        int x, y, n;
        x = y = n = 0;
        string s;
        cin >> s;
        for (int i = 0; i < s.size(); i++)
            switch(s[i])
            {
                case 'U': y += 1; break;
                case 'D': y -= 1; break;
                case 'L': x -= 1; break;
                case 'R': x += 1; break;
                case '?': n += 1; break;
            }
        printf("%d %d %d %d
", x-n, y-n, x+n, y+n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuhenqs/p/3856944.html