Loading

HDU 2300 Crashing Robots

Crashing Robots

题意

  • 模拟多个机器人在四个方向的移动,检测crash robot, crash wall, OK这些状态
  • 这是个模拟题需要注意几点:
    • 理解转变方向后移动多少米,和转动方向多少次的区别,这里后一种,在于自己审题
    • crash robot 需要区别哪一个是最先找到的

代码(自己写的比较乱)

int move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
//1..A or B
std::map<char,int> mp={{'E',0},{'N',1},{'W',2},{'S',3}};
struct node{
    int x,y;
    int dir;
};//record position
int xb,yb;//x,y边界
node pos[105];//记录当前robot 位置
int ros,ins;//robots num and instruction nums
bool cr(int x1,int x2,int x){
    return (x1<=x&&x<=x2)||(x2<=x&&x<=x1);
}
bool crash_robot(node b,node e,node r){
    if(cr(b.x,e.x,r.x)&&cr(b.y,e.y,r.y)) return true;
    else return false;
}
int check(int a, char ins,int len){
    //return 1 crash robot 2 crash wall 3 OK temparily
    //
    node tmp=pos[a];
    if(ins=='L'){
        pos[a].dir=(pos[a].dir+len)%4;
    }
    else if(ins=='R'){
        pos[a].dir=(pos[a].dir-len+400)%4;
    }
    //
    else if(ins=='F'){
    tmp.x=pos[a].x+move[pos[a].dir][0]*len;
    tmp.y=pos[a].y+move[pos[a].dir][1]*len;
    tmp.dir=pos[a].dir;
    int near_rob=-1;
    for(int j=1;j<=ros;j++){
        //需要判断谁先撞上
        
        if(j!=a){
            if(crash_robot(pos[a],tmp,pos[j])){
                if(near_rob==-1) near_rob=j;
                else {
                    if(abs(pos[j].x-pos[a].x+pos[j].y-pos[a].y)<abs(pos[near_rob].x+pos[near_rob].y-pos[a].x-pos[a].y)) near_rob=j;
                }
                //printf("Robot %d crashes into robot %d\n",a,j);
                
                
            }
        }
    }
    pos[a]=tmp;
    if(near_rob!=-1){
        printf("Robot %d crashes into robot %d\n",a,near_rob);
        return 1;
    }
    //no crash robot
    //check crash wall
    if(tmp.x<=0||tmp.x>=xb+1||tmp.y<=0||tmp.y>=yb+1){
        printf("Robot %d crashes into the wall\n",a);
        pos[a]=tmp;
        return 2;
    }
    pos[a]=tmp;
    }

    return 0;
}
posted @ 2018-08-20 21:18  fridayfang  阅读(138)  评论(0编辑  收藏  举报