poj 2632 Crashing Robots 模拟题 测试数据+AC代码
2016-07-21 17:00
387 查看
据说 这是一道简单的模拟题,
据说 这道题并没有涉及算法。
但是很考验耐心因为需要考虑很多情况,我一直WA,所以不停的debug找出之前忽略的情况。
尤其需要注意的是,坐标是变换的,注意坐标的变换规律。
这道题目大体需要注意这几点:
1.坐标变换
将坐标系用二维矩阵的形式表示,但需要注意,“向北走”就是二维矩阵中该点的行数加1,“向南走”就是在二维矩阵中该点的行数减1,“向东走”就是列数加1,“向西走”就是列数减1。
2.取余出现负数的情况
坐标变换的过程中,取余可能出现负数的情况,注意将其转换成整数
附上在网上看到的测试数据:
Sample Input
8
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
9 5
5 19
2 4 E
4 3 N
6 2 E
9 5 S
9 1 W
4 F 1
4 R 1
4 F 6
4 L 5
4 F 3
4 L 1
5 R 1
5 F 3
5 L 1
5 F 2
5 L 1
5 F 3
5 R 5
5 F 2
5 R 1
5 F 2
4 F 2
4 L 1
4 F 3
9 5
2 6
9 5 S
9 1 W
1 F 1
1 R 1
1 F 2
2 F 2
2 R 1
2 F 3
5 4
2 2
1 1 E
5 4 W
1 R 1
1 F 2
5 4
2 2
1 1 E
5 4 W
1 L 1
1 F 2
Sample Output
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
Robot 4 crashes into robot 5
Robot 2 crashes into robot 1
Robot 1 crashes into the wall
OK
AC代码:
据说 这道题并没有涉及算法。
但是很考验耐心因为需要考虑很多情况,我一直WA,所以不停的debug找出之前忽略的情况。
尤其需要注意的是,坐标是变换的,注意坐标的变换规律。
这道题目大体需要注意这几点:
1.坐标变换
将坐标系用二维矩阵的形式表示,但需要注意,“向北走”就是二维矩阵中该点的行数加1,“向南走”就是在二维矩阵中该点的行数减1,“向东走”就是列数加1,“向西走”就是列数减1。
2.取余出现负数的情况
坐标变换的过程中,取余可能出现负数的情况,注意将其转换成整数
附上在网上看到的测试数据:
Sample Input
8
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
9 5
5 19
2 4 E
4 3 N
6 2 E
9 5 S
9 1 W
4 F 1
4 R 1
4 F 6
4 L 5
4 F 3
4 L 1
5 R 1
5 F 3
5 L 1
5 F 2
5 L 1
5 F 3
5 R 5
5 F 2
5 R 1
5 F 2
4 F 2
4 L 1
4 F 3
9 5
2 6
9 5 S
9 1 W
1 F 1
1 R 1
1 F 2
2 F 2
2 R 1
2 F 3
5 4
2 2
1 1 E
5 4 W
1 R 1
1 F 2
5 4
2 2
1 1 E
5 4 W
1 L 1
1 F 2
Sample Output
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
Robot 4 crashes into robot 5
Robot 2 crashes into robot 1
Robot 1 crashes into the wall
OK
AC代码:
#include<iostream> #include<cstdio> #include<cstring> #include<map> using namespace std; int w[105][105];//二维矩阵表示warehouse中的各个位置的状态,状态为-1表示该位置无机器人,为i表示该位置有机器人i int dr[4] = { 1,0,-1,0}; //北,东,南,西 int dc[4] = { 0,1,0,-1}; map<char,int> directionMap; typedef struct robot { int number; int r; int c; int d; }; robot rbt[110]; void initDirectionMap() { directionMap['N'] = 0; directionMap['E'] = 1; directionMap['S'] = 2; directionMap['W'] = 3; } //机器人的移动动作 void action(char c,int i) { switch(c) { case 'L': { rbt[i].d = (rbt[i].d-1+4)%4; break; } case 'R': { rbt[i].d = (rbt[i].d+1)%4; break; } case 'F': { w[rbt[i].r][rbt[i].c] = -1; rbt[i].r+=dr[rbt[i].d]; rbt[i].c+=dc[rbt[i].d]; } } } int main() { freopen("in.txt","r",stdin); int ncase; int a,b; int n,m; char direction; int flag; cin >> ncase; initDirectionMap(); for(int nc=0;nc<ncase;nc++) { for(int i=0;i<105;i++) for(int j=0;j<105;j++) w[i][j] = -1; cin >> a >> b; cin >> n >> m; //记录warehouse的状态,即各机器人的位置及移动方向 for(int i=1;i<=n;i++) { rbt[i].number = i; cin >> rbt[i].c >> rbt[i].r; cin >> direction; rbt[i].d = directionMap[direction]; w[rbt[i].r][rbt[i].c] = i; } flag = 1; int num; char ac; int step; for(int i = 0;i<m;i++) { cin >> num; cin >> ac; cin >> step; flag = 1; for(int j=0;j<step;j++) { action(ac,num); if(w[rbt[num].r][rbt[num].c]==-1||w[rbt[num].r][rbt[num].c]==num) { w[rbt[num].r][rbt[num].c] = num; } else { flag = 2; break; } if(rbt[num].c>a||rbt[num].r>b||rbt[num].c<=0||rbt[num].r<=0) { flag = 3; break; } } //如果撞墙或机器人相撞,退出循环,将剩下的m-i-1组数据存入临时变量numx,acx,stepx中 if(flag!=1) { int numx,stepx; char acx; for(int k=0;k<m-i-1;k++) cin >> numx >> acx >> stepx; break; } } if(flag==1) cout << "OK" << endl; else if(flag==2) cout << "Robot " << num << " crashes into robot " << w[rbt[num].r][rbt[num].c] << endl; else if(flag==3) cout << "Robot " << num << " crashes into the wall" << endl; } return 0; }
相关文章推荐
- Linux 自检和 SystemTap
- Python 七步捉虫法
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 路由器的配置与调试
- C#算法之大牛生小牛的问题高效解决方法
- 对于技术人员的出现了运行时间错误,是否要进行调试的解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法