poj2632模拟法
来源:互联网 发布:手机淘宝怎样看到退货 编辑:程序博客网 时间:2024/06/07 06:50
又是一次ac哈哈哈,虽然题目本身不难,但是看上去很烦的样子。
不知为何我有一种高中做综合题的感觉。
但是这道题有一个陷阱,就是在输入的时候,程序crash之后应该跳过所有剩下的input执行下一个case,这里需要人工检验。
感谢Xcode的断点查错,让我明白了程序里面正在发生什么。
vector可以用自己定义的class type这点十分便利,说实话我也是第一次用。
另外要巧用bool返回的函数,如果状态比较多的话也可以考虑int返回。
#include <iostream>#include <vector>using namespace std;struct robot{ int x; int y; char direction; robot(int X,int Y,char Direction){ x=X; y=Y; direction=Direction; }};vector<robot> v;int length,side;bool test_show(int no_robot){ //check if crashes the wall if(v[no_robot-1].x>length||v[no_robot-1].y>side||v[no_robot-1].x<=0||v[no_robot-1].y<=0){ cout<<"Robot "<<no_robot<<" crashes into the wall"<<endl; return false; } int x=v[no_robot-1].x; int y=v[no_robot-1].y; for(int i=0;i<v.size();i++){ if(i!=no_robot-1) if(x==v[i].x&&y==v[i].y){ cout<<"Robot "<<no_robot<<" crashes into robot "<<i+1<<endl; return false; } } return true;}bool operation(int no_robot, char instruct, int repeat){ switch (instruct) { case 'F': for(int i=0;i<repeat;i++){ char d=v[no_robot-1].direction; if(d=='E'){ v[no_robot-1].x++; } else if(d=='W'){ v[no_robot-1].x--; } else if(d=='N'){ v[no_robot-1].y++; } else if(d=='S'){ v[no_robot-1].y--; } if(!test_show(no_robot)) return false; } return true; break; case 'L': for(int i=0;i<repeat;i++){ char d=v[no_robot-1].direction; if(d=='E') v[no_robot-1].direction='N'; else if(d=='W') v[no_robot-1].direction='S'; else if(d=='N') v[no_robot-1].direction='W'; else if(d=='S') v[no_robot-1].direction='E'; } return true; break; case 'R': for(int i=0;i<repeat;i++){ char d=v[no_robot-1].direction; if(d=='E') v[no_robot-1].direction='S'; else if(d=='W') v[no_robot-1].direction='N'; else if(d=='N') v[no_robot-1].direction='E'; else if(d=='S') v[no_robot-1].direction='W'; } return true; break; default: return true; break; }}bool receive_instructions(int instructions){ int j=0; bool r=true; for(;j<instructions;j++){ int no_robot,repeat; char instruct; cin>>no_robot>>instruct>>repeat; if(r) r=operation(no_robot, instruct, repeat); } return r; }int main(int argc, const char * argv[]) { // insert code here... int num_case; cin>>num_case; for(int i=0;i<num_case;i++){ if(!v.empty()) v.clear(); cin>>length>>side; int num_robot,instructions; cin>>num_robot>>instructions; for(int j=0;j<num_robot;j++){ int x,y; char direction; cin>>x>>y>>direction; robot r(x,y,direction); v.push_back(r); } if(receive_instructions(instructions)){ cout<<"OK"<<endl; } } return 0;}
0 0
- poj2632模拟法
- poj2632--模拟
- poj2632(模拟)
- POJ2632,Crashing Robots,模拟法,繁
- POJ2632--Crashing Robots--模拟
- poj2632(模拟题)
- POJ2632-Crashing Robots(模拟)
- POJ2632《Crashing Robots》方法:模拟
- (模拟)poj2632 Crashing Robots
- POJ2632 Crashing Robots 模拟题
- POJ2632--Crashing Robots(模拟)
- POJ2632 Crashing Robots(模拟)
- POJ2632
- POJ2632
- poj2632
- POJ2632
- POJ2632
- poj2632
- volley总结
- <context:component-scan/>
- SCOPE_IDENTITY和@@IDENTITY
- WEB前端学习 Day 6(Javascript 初识+变量+逻辑判断+数据类型)
- sqlserver日期(时间)格式化
- poj2632模拟法
- Unable to execute dex: Multiple dex files define Landroid/support/annotation/AnimRes;
- 在Webstorm中使用Autoprefixer
- Monkey之Log分析
- Arrays.asList() 返回的list不能add,remove
- 用brk实现sbrk,关于brk的返回值
- swift 单例模式
- SPRING 简介
- Linux using Beyon compare4 as Git diff tool