走格子收获
2016-03-16 18:49
162 查看
eg:poj2632 code: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a,b; int queue[101][101]; int xx[4]= {-1,0,1,0}; //!!!!!!!注意这里方向的顺序必须和0、1、2、3保持一致!!!!WA了好多遍都不知道哪错了 int yy[4]= {0,1,0,-1}; struct spot { int x,y,d; } robot[101]; bool forward(int s,int t) { int x=robot[s].x; int y=robot[s].y; int d=robot[s].d; queue[x][y]=0; for(int i=0; i<t; i++) { x+=xx[d]; y+=yy[d]; if(queue[x][y]) { cout<<"Robot "<<s<<" crashes into robot "<<queue[x][y]<<endl; return true; } if(x<1||x>a||y>b||y<1) { cout<<"Robot "<<s<<" crashes into the wall"<<endl; return true; } } robot[s].x=x; robot[s].y=y; queue[x][y]=s; return false; } bool action(int s,char dir,int t) { switch (dir) { case 'F': return forward(s,t); case 'L': robot[s].d=(robot[s].d-t%4+4)%4; //是编号始终为0、1、2、3 对于绕桌子一圈编号问题一样 break; case 'R': robot[s].d=(robot[s].d+t%4)%4; break; } return false; } int main() { int k; int n,m,t,s; int xi,yi; char dir; cin>>k; bool f=false; while(k--) { memset(queue,0,sizeof(queue)); memset(robot,0,sizeof(struct spot)*101); cin>>a>>b>>n>>m; for(int i=1; i<=n; i++) { cin>>xi>>yi>>dir; queue[xi][yi]=i; //这里注意,通过编号找位置,通过位置找编号 robot[i].x=xi; robot[i].y=yi; switch(dir) //可以用0、1、2、3来表示方向 { N : 1 case 'N': W:0 + E :2 robot[i].d=1; S : 3 break; case 'S': robot[i].d=3; break; case 'W': robot[i].d=0; break; case 'E': robot[i].d=2; break; } } f=false; for(int i=0; i<m; i++) { cin>>s>>dir>>t; if(!f) f=action(s,dir,t); } if(!f) cout<<"OK"<<endl; } }
相关文章推荐
- php面向对象编程
- build模式小例子
- Android 开发过程中总计的一些经验教z
- 阿里云服务器怎么去掉tomcat的8080端口
- Firemonkey Button 颜色
- NSMutableAttributedString
- PC端(基于Linux系统)通过串并网线口连接ARM开发板,烧写Bootloader并启用Bootp和Tftp等服务刷写Linux系统。
- iOS开发:XIB之AutoLayout添加约束
- 是输入字符矩阵时
- 通用型 控制层接受参数方法
- MySQL 源码安装
- [ShareSDK] QuickIntegrater.jar文件各种打不开
- POJ2299 分治法
- 学习TensorFlow,线性回归模型
- 字符串输入处理
- poj1426
- 扒扒数据库长长知识(下载资源组合看)之05 (多行函数_分组数据_分组过滤)
- 学习TensorFlow,线性回归模型
- 杭电1392-凸包
- Windows上Eclipse远程调用Hadoop2.6.0