POJ 2632--Crashing Robots
来源:互联网 发布:阿里云域名优惠码口令 编辑:程序博客网 时间:2024/05/17 07:33
2017-10-07
原题链接
题目大意:
有一个东-西方向长A米,南-北方向长B米的仓库(0<=A,b<=100),仓库里有N个机器人,给出M条指令操纵它们(0<=N,M<=100)。输入数据第一行是一个整数K,给出测试数据的组数。接下来每组数据第一行给出A,B;第二行给出N,M;接下来N行每行两个整数表明一个机器人(编号1~N)的位置,还有一个字符表明机器人初始的朝向(W,N,E或S);再接着的M行每行第一个整数指出机器人编号,第二个字符给出指令类型(R:向当前方向右侧转90度;L:向当前方向左侧转90度;F:向当前方向前进1米),第三个整数指示这条指令的执行次数。只有一整行指令执行完才会执行下一行指令。对每一组测试数据,输出一行,如果没有机器人撞墙(试图移动到超出仓库范围的区域,输出“Robot x crashes into the wall”),或者撞到另一个机器人(总是记为正在移动的机器人撞上一个静止的机器人,输出“Robot x crashes into robot y”),输出为“OK”。
样例输入:
45 42 21 1 E5 4 W1 F 72 F 75 42 41 1 E5 4 W1 F 32 F 11 L 11 F 35 42 21 1 E5 4 W1 L 961 F 25 42 31 1 E5 4 W1 F 41 L 11 F 20
样例输出:
Robot 1 crashes into the wallRobot 1 crashes into robot 2OKRobot 1 crashes into robot 2
这题的思路没什么特别的,就是按照题意读取输入,初始化一个“仓库”,逐条模拟指令并判断机器人的状态。个人觉得有几点要注意:
1.一定要理清题目的“东西南北”与自己模拟仓库的二维数组维度之间的对应关系;
2.指令‘R’和‘L’不改变机器人位置,仅改变其朝向,‘F’会改变机器人位置。
概括起来,,就是:
读题一定要认真!!读题千万要认真!!读题绝对要认真!!
-_-||血的教训……
代码:
#include <iostream>#include <cstring>#include <cstdio>#define MAX 110using namespace std;//记录机器人初始朝向: W-0,N-1,E-2,S-3int warehouse[MAX][MAX];int robotNum[MAX][2];//四个方向对应的坐标变化(W,N,E,S),注意对应关系int dirNS[4] = { 0, 1, 0,-1};int dirWE[4] = {-1, 0, 1, 0};//判断执行一次'F'指令后机器人的状态: 0-正常, 1-撞墙, 2-撞上其他机器人int judState(int A, int B, int Xi, int& crash, int N) { int xx = robotNum[Xi][0]; int yy = robotNum[Xi][1]; int move = warehouse[xx][yy]; int Xmove = dirWE[move] + xx; int Ymove = dirNS[move] + yy; if(warehouse[Xmove][Ymove] == -1) { if(Xmove == 0 || Xmove == (A+1) || Ymove == 0 || Ymove == (B+1)) return 1; else { warehouse[Xmove][Ymove] = warehouse[xx][yy]; warehouse[xx][yy] = -1; robotNum[Xi][0] = Xmove; robotNum[Xi][1] = Ymove; } } else //撞上其他机器人 { for(int j = 1; j <= N; j++) { if(robotNum[j][0] == Xmove && robotNum[j][1] == Ymove) { crash = j; break; } } return 2; } return 0;}int main() { int K,A,B,N,M,Xi,Yi; char Dir; //K:输入样例组数 cin >> K; while(K > 0) { memset(warehouse, -1 ,sizeof(warehouse)); memset(robotNum, 0, sizeof(robotNum)); //A:每组中仓库的长(数组第1维); B:每组中仓库的宽(数组第2维) cin >> A >> B; //N:机器人数目; M:指令数目 cin >> N >> M; for(int i = 1; i <= N; i++) { //Xi,Yi:每个机器人的初始位置; Dir:每个机器人的初始朝向 cin >> Xi >> Yi >> Dir; switch(Dir) { case 'W' : warehouse[Xi][Yi] = 0; break; case 'N' : warehouse[Xi][Yi] = 1; break; case 'E' : warehouse[Xi][Yi] = 2; break; case 'S' : warehouse[Xi][Yi] = 3; break; } //robotNum:记录每个机器人的编号(按出现顺序) robotNum[i][0] = Xi; robotNum[i][1] = Yi; } //记录指令执行后机器人的状态: 0-正常, 1-撞墙, 2-撞上其他机器人 int state = 0; int tp1,tp3,crash; char tp2; for(int i = 0; i < M; i++) { cin >> tp1 >> tp2 >> tp3; if(state != 0) continue; //要完整地接收输入 Xi = tp1; Dir = tp2; Yi = tp3; for(; Yi > 0; Yi--) { if(state != 0) break; int xx = robotNum[Xi][0]; int yy = robotNum[Xi][1]; switch(Dir) { //* 注意:‘L’和‘R’,每步都需要转换90度 * //** 注意:只有‘F’操作才会改变机器人的位置,‘L’和‘R’只改变其方向 ** //********** 读题 千万/一定/必须 要 认真 ************* -_-|| case 'L': warehouse[xx][yy] = (warehouse[xx][yy] + 3) % 4; break; case 'R': warehouse[xx][yy] = (warehouse[xx][yy] + 1) % 4; break; case 'F': state = judState(A,B,Xi,crash,N); break; } } } if(state == 0) cout << "OK" << endl; else if(state == 1) cout << "Robot " << Xi << " crashes into the wall" << endl; else cout << "Robot " << Xi << " crashes into robot " << crash << endl; --K; } return 0;}
阅读全文
0 0
- Poj 2632 Crashing Robots
- poj 2632 Crashing Robots
- poj 2632 Crashing Robots
- poj-2632-Crashing Robots
- POJ 2632 Crashing Robots
- POJ 2632 Crashing Robots
- POJ 2632 Crashing Robots
- POJ 2632 Crashing Robots
- POJ-2632-Crashing Robots
- POJ 2632 Crashing Robots
- POJ 2632 Crashing Robots
- POJ 2632 Crashing Robots
- poj 2632 Crashing Robots
- POJ 2632 Crashing Robots
- poj -2632 Crashing Robots
- poj 2632 Crashing Robots
- POJ 2632 Crashing Robots
- poj 2632 Crashing Robots
- MySQL开发人员对缓存(Query Cache)下了一道处决令
- 同样25岁,为什么有的人事业小成,有的人却还一无所有?
- 解决Eclipse报错显示Unable to build: the file dx.jar was not loaded from the SDK folder
- 替换空格
- 16. 与设计模式相处
- POJ 2632--Crashing Robots
- [bzoj1217][贪心]消防局的设立
- 使用 ProxySQL 提高 MySQL SSL 的连接性能
- Android访问硬件的方法
- 【LeetCode】C# 5. Longest Palindromic Substring
- 蓝桥杯 算法提高 插入排序
- 创建套接字
- 英格尔斯论现代人
- Django入门学习笔记