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;
    }

转载于:https://www.cnblogs.com/fridayfang/p/9508176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值