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

最新文章

  1. python安装后推荐的安装两款文本编辑器
  2. ftp如何预览图片 解决方案
  3. 移植UE4的Spline与SplineMesh组件到Unity5
  4. ORACLE 常用字符函数
  5. [HDU 1806] Frequent values
  6. UITextView 动态高度计算(iOS7版)
  7. linux中bin和xbin下可执行程序的区别
  8. 各种Web漏洞测试平台
  9. mooc上学习acllib后写的包含背景音乐的小涂鸦板(初入江湖,大佬勿喷)
  10. 好用的SQLAlchemy
  11. 修改linux下yum镜像源为国内镜像
  12. 13-事务&amp;数据库连接池&amp;DBUtiles
  13. lldb调试mysql 插件命令
  14. 最短路径问题---Dijkstra算法详解
  15. appium学习记录1
  16. Unity NavMesh导航网格 初级教程
  17. MyBatis sql语句使用总结
  18. 峰Redis学习(6)Redis 数据结构(sorted-set的操作)
  19. Windows下sbt安装配置
  20. Linux 文件缓存 (二)

热门文章

  1. Python笔记8----DataFrame(二维)
  2. Spring 的IOC和DI
  3. 2019-03-29 Vagrant Docker Toolbox 下载安装
  4. js严格模式下判断数据类型
  5. 【ACM-ICPC 2018 沈阳赛区网络预赛 K】Supreme Number
  6. C/C++ 在处理文件所在路径下创建子目录
  7. MySQL 触发器 -1
  8. Ruby中使用patch HTTP方法
  9. 菜鸟的mongoDB学习---(六)MongoDB 索引
  10. Java測试覆盖率工具----Cobertura,EclEmma