You Are Given a WASD-string...

C. You Are Given a WASD-string...

主要看的还是思维,分别求出在上下左右四个方向移动的最大幅度( mov_up, mov_down, mov_right, mov_left )然后对于垂直方向如果 mov_up == mov_down 那么 robot 最后的位置的垂直方向上的位置与其初始位置是相同的所以不能够或者不必再添加移动的操作,同理水平方向。

// Created by CAD on 2019/8/9.
#include <bits/stdc++.h>

using namespace std;
using pii=pair<int, int>;
using piii=pair<pair<int, int>, int>;
using ll=long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int max_up=0,max_down=0,max_right=0,max_left=0;
        int mov_up=0,mov_down=0,mov_right=0,mov_left=0;
        int x=0,y=0;
        for(auto i:s)
        {
            if(i=='W') y++;
            else if(i=='S') y--;
            else if(i=='D') x++;
            else if(i=='A') x--;
            max_up=max(max_up,y);
            max_down=min(max_down,y);
            max_right=max(max_right,x);
            max_left=min(max_left,x);

            mov_up=max(mov_up,y-max_down);
            mov_down=max(mov_down,max_up-y);
            mov_right=max(mov_right,x-max_left);
            mov_left=max(mov_left,max_right-x);
        }
        int a,b,c,d;
        a=max(mov_up,mov_down);
        b=max(int(mov_up||mov_down), a - (mov_up != mov_down));
        c=max(mov_left,mov_right);
        d=max(int(mov_left||mov_right), c - (mov_left != mov_right));
        cout<<min(1ll*(a+1)*(d+1),1ll*(b+1)*(c+1))<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CADCADCAD/p/11329100.html