Dr. Sukthankar's Robot

水题:

  给定初始行动序列,和两个目标点A,B;

  要求从原点出发,按目标序列行动完后,

  然后按照先沿X轴行动(如果有必要),再沿Y轴行动到达A点(如果有必要),

  再然后按照先沿X轴行动(如果有必要),再沿Y轴行动到达B点(如果有必要),

  有必要的意思是:例如“按照先沿X轴行动(如果有必要)”指的是目标点和当前点的X坐标不相同。假如目标点和当前点的X坐标相同,那就直接延Y轴移动。

坑:

  不考虑初始行动序列经过目标点的境况。(垃圾)

垃圾代码:

  

#include <iostream>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
//D 方向,0左,1上,2右,3下
struct Dir{//笛卡尔坐标系向量
    int x,y;
    Dir operator+(const Dir &b)const{
        Dir ans;
        ans.x=x+b.x;
        ans.y=y+b.y;
        return ans;
    }
    bool operator==(const Dir &b)const{
        if(x==b.x&&y==b.y)return 1;
        else return 0;
    }
    Dir operator=(const Dir b){
        x=b.x;
        y=b.y;
        return *this;
    }
};
struct posion{
    int x,y;
    int d;
    Dir operator-(const posion&b)const{
       Dir ans;
       ans.x=x-b.x;
       ans.y=y-b.y;
       return ans;
    }
    void operator+=(int c){
        if(d==0)x-=c;
        if(d==1)y+=c;
        if(d==2)x+=c;
        if(d==3)y-=c;
    }
    posion operator=(const posion&b){
        x=b.x;
        y=b.y;
        d=b.d;
        return *this;
    }
}s1,s2;
bool visted[2];
bool isPass(const posion a,const posion b,const posion c){
    if(a.x==b.x&&a.x==c.x){
        if(c.y>=a.y&&c.y<=b.y)return 1;
        if(c.y>=b.y&&c.y<=a.y)return 1;
    }
    else if(a.y==b.y&&a.y==c.y){
        if(c.x>=a.x&&c.x<=b.x)return 1;
        if(c.x>=b.x&&c.x<=a.x)return 1;
    }
    return 0;
}
void preMove(posion &p,string move,int len=0){
    if(move=="MOVE"){
        posion tmp=p;
        p+=len;
        /*if(isPass(tmp,p,s1))visted[0]=1;
        if(visted[0]){
            if(isPass(tmp,p,s2))visted[1]=1;
        }*/
    }
    else{
     if(move=="LEFT")p.d=(p.d-1+4)%4;
     else if(move=="RIGHT")p.d=(p.d+1)%4;
     else p.d=(p.d+2)%4;
    }
}

void turn(int &d,int D){
    if(d==D)return ;
    if((d-1+4)%4==D)cout<<"LEFT"<<endl;
    else if((d+1)%4==D)cout<<"RIGHT"<<endl;
    else cout<<"UTURN"<<endl;
    d=D;
}
void moveX(posion &p,const posion dst){
    int D;
    if(dst.x-p.x>0)D=2;
    else D=0;
    turn(p.d,D);
    int dif=dst.x-p.x;
    p+=abs(dif);
    cout<<"MOVE "<<abs(dif)<<endl;
}
void moveY(posion &p,const posion dst){
    int D;
    if(dst.y-p.y>0)D=1;
    else D=3;
    turn(p.d,D);
    int dif=dst.y-p.y;
    p+=abs(dif);
    cout<<"MOVE "<<abs(dif)<<endl;
}
string record[300];
int toint(string tmp){
    int ans=0;
    int len =tmp.length();
    for(int i=0;i<len;i++){
        ans*=10;
        ans+=tmp[i]-'0';
    }
    return ans;
}
int main() {
    //freopen("out.txt","w",stdout);
    int T;
    cin>>T;
    for(int tt=1;tt<=T;tt++){
        printf("Robot Program #%d:
",tt);
        int n;
        cin>>n;
        for(int i=0,t=0;i<n;i++){
            cin>>record[t];
            if(record[t]=="MOVE")cin>>record[++t];
            t++;
        }
        cin>>s1.x>>s1.y>>s2.x>>s2.y;
        posion p;
        p.x=p.y=0;
        p.d=2;
        visted[0]=visted[1]=0;
        for(int i=0,t=0;i<n;i++){
            if(record[t]=="MOVE"){
                preMove(p,record[t],toint(record[t+1]));
                t++;
            }
            else preMove(p,record[t]);
            t++;
        }
        printf("The robot is at (%d,%d)
",p.x,p.y);
        if(!visted[0]){
            if(p.x!=s1.x)moveX(p,s1);
            if(p.y!=s1.y)moveY(p,s1);
        }
        if(!visted[1]){
            if(p.x!=s2.x)moveX(p,s2);
            if(p.y!=s2.y)moveY(p,s2);
        }
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dialectics/p/12482774.html