TopCoder SRM 570 Div1 250 RobotHerb

可以发现执行四次之后方向一定和原来相同,有了这个性质就很容易处理了。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
int n;
ll x,y;
int d;

class RobotHerb {
public:
    long long getdist( int T, vector <int> a );
};

void go(vector<int> a){
    for(int i=0;i<n;i++){
        x+=dx[d]*a[i];y+=dy[d]*a[i];
        d=(d+a[i])%4;
    }
}

long long RobotHerb::getdist(int T, vector <int> a) {
    x=y=d=0;
    n=a.size();
    for(int i=0;i<4;i++) go(a);
    x*=T/4;y*=T/4;
    d*=T/4;d%=4;
    for(int i=0;i<T%4;i++) go(a);
    return abs(x)+abs(y);
}

猜你喜欢

转载自blog.csdn.net/ymzqwq/article/details/81665231
今日推荐