poj2632--模拟
来源:互联网 发布:mediawiki 数据库配置 编辑:程序博客网 时间:2024/05/21 07:01
/** \brief poj 2632 * * \param date 2014/8/3 * \param state AC * \return memory 776k time 16ms * */#include <iostream>#include <fstream>#include <cstring>using namespace std;const int MAXN=101;int Map[MAXN][MAXN];int A,B;int N,M;int xf[4]={-1,0,1,0};int yf[4]={0,1,0,-1};struct Robot{ int x; int y; //char dir; int dir; //int num;};Robot robots[MAXN];//将方向数字化int charDir2Int(char cDir){ switch(cDir) { case 'E': return 2; case 'S': return 3; case 'W': return 0; case 'N': return 1; };}//模拟搜索bool Forward(int s,int t){ int x,y; int d=robots[s].dir; x=robots[s].x; y=robots[s].y; Map[x][y]=0;//走过为0 for(int i=0;i<t;i++) { x=x+xf[d]; y=y+yf[d]; //边界 -- wall判断 if(x<1 || x>A || y<1 || y>B) { cout<<"Robot "<<s<<" crashes into the wall"<<endl; return true; } if(Map[x][y]) { cout<<"Robot "<<s<<" crashes into robot "<<Map[x][y]<<endl; return true; } } robots[s].x=x; robots[s].y=y; Map[x][y]=s;//到达新点,赋上序号 return false;}//指令--行动bool Action(int s,char Dir,int t){ switch(Dir) { case 'F': return Forward(s,t); case 'L': robots[s].dir=(robots[s].dir-t%4+4)%4; break; case 'R': robots[s].dir=(robots[s].dir+t%4)%4; break; } return false;}int main(){ //cout << "Hello world!" << endl; freopen("input.txt","r",stdin); int K; cin>>K; for(int k=0;k<K;k++) { memset(Map,0,sizeof(Map)); cin>>A>>B; cin>>N>>M; for(int i=1;i<=N;i++) { int x,y; char dir; cin>>x>>y>>dir; robots[i].x=x; robots[i].y=y; robots[i].dir=charDir2Int(dir); //robots[i].num=i; Map[x][y]=i; } bool f=false;//it means that OK for(int j=0;j<M;j++) { int num,repeat; char op; cin>>num>>op>>repeat; // if(!f)f=Action(num,op,repeat); } if(!f)cout<<"OK"<<endl; } return 0;}
参考:http://blog.csdn.net/tiantangrenjian/article/details/6827710
模拟算法:根据题目所述移动步骤逐步进行,利用数组array[i][j]代表(i,j)位置处的robot编号,没有则为0.
利用robot结构体记录下每个机器人当前的位置和方向。
对于'F'指令,需判断前进是否出界和前进的位置是否已有机器人。
对于'L'和'R'转向指令,只需修改机器人的方向值,注意同一方向转四次等于没转。
0 0
- poj2632--模拟
- poj2632(模拟)
- POJ2632--Crashing Robots--模拟
- poj2632(模拟题)
- 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
- poj2632
- Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-1.xml, reason: Connecti
- 输入3个学员的姓名,动态分配内存保存学员姓名,并在最后输出.
- java之抽象类和接口理解
- 【快速排序、合并排序与分治思想】
- [BetterExplained]书写是为了更好的思考
- poj2632--模拟
- 多线程简单示例
- Mysql 安装 错误 记录
- C#winform设置窗体自动缩放适应不同尺寸的电脑屏幕大小
- POJ 3009Curling 2.0(基础题)
- Java集合框架面试问题集锦
- HDU 2377 基础dijkstra,spfa算法
- Django学习笔记(六)Django上传文件
- 拓扑排序(AOV)---判断图中是否有环---《数据结构》严蔚敏