模拟 POJ 2632 Crashing Robots
题目地址:http://poj.org/problem?id=2632
1 /* 2 题意:几个机器人按照指示,逐个朝某个(指定)方向的直走,如果走过的路上有机器人则输出谁撞到;如果走出界了,输出谁出界 3 如果以上都没发生,则输出OK 4 模拟题:无算法,按照条件编写几个函数和判断条件 5 注意:1. 关于绕转,只要模拟人的转圈方向,比如向右转就向右手边转(优化:当超过4次则对4取模) 6 2. 有先撞墙还是先撞机器人的问题(每一次'F'后都要及时判断是否OK) 7 3. 撞哪个机器人也有先来后到的顺序('F'后以这个机器人(除自己以外)遍历一边是否OK) 8 */ 9 #include <cstdio> 10 #include <iostream> 11 #include <algorithm> 12 #include <cstring> 13 #include <cmath> 14 #include <string> 15 #include <map> 16 #include <queue> 17 #include <vector> 18 using namespace std; 19 20 const int MAXN = 1e6 + 10; 21 const int INF = 0x3f3f3f3f; 22 struct NODE 23 { 24 int x, y; 25 int dir; 26 }node[MAXN]; 27 struct OP 28 { 29 int name, t; 30 char move; 31 }op[MAXN]; 32 bool flag; 33 34 bool ok(int k, int N, int M, int n) 35 { 36 for (int i=1; i<=n; ++i) 37 { 38 if (node[i].x < 1 || node[i].x > N || node[i].y < 1 || node[i].y > M) 39 { 40 printf ("Robot %d crashes into the wall\n", i); 41 flag = false; 42 return false; 43 } 44 } 45 46 for (int j=1; j<=n; ++j) 47 { 48 if (k != j && node[k].x == node[j].x && node[k].y == node[j].y) 49 { 50 printf ("Robot %d crashes into robot %d\n", k, j); 51 flag = false; 52 return false; 53 } 54 } 55 56 flag = true; 57 return true; 58 } 59 60 void forward(NODE *a, OP b, int N, int M, int n) 61 { 62 while (b.t--) 63 { 64 if (a->dir == 'N') a->x -= 1; 65 else if (a->dir == 'S') a->x += 1; 66 else if (a->dir == 'W') a->y -= 1; 67 else a->y += 1; 68 if (!ok (b.name, N, M, n)) break; 69 } 70 } 71 72 NODE turn_l(NODE a, OP b) 73 { 74 b.t = (b.t >= 4) ? (b.t % 4) : b.t; 75 while (b.t--) 76 { 77 if (a.dir == 'N') a.dir = 'W'; 78 else if (a.dir == 'S') a.dir = 'E'; 79 else if (a.dir == 'E') a.dir = 'N'; 80 else a.dir = 'S'; 81 } 82 83 return a; 84 } 85 86 NODE turn_r(NODE a, OP b) 87 { 88 b.t = (b.t >= 4) ? (b.t % 4) : b.t; 89 while (b.t--) 90 { 91 if (a.dir == 'N') a.dir = 'E'; 92 else if (a.dir == 'S') a.dir = 'W'; 93 else if (a.dir == 'W') a.dir = 'N'; 94 else a.dir = 'S'; 95 } 96 97 return a; 98 } 99 100 void work(int N, int M, int n, int m) 101 { 102 int i; 103 for (i=1; i<=m; ++i) 104 { 105 if (op[i].move == 'F') 106 forward (&node[op[i].name], op[i], N, M, n); 107 else if (op[i].move == 'L') 108 node[op[i].name] = turn_l (node[op[i].name], op[i]); 109 else if (op[i].move == 'R') 110 node[op[i].name] = turn_r (node[op[i].name], op[i]); 111 if (flag) continue; 112 else break; 113 } 114 if (i == m + 1) puts ("OK"); 115 } 116 117 int main(void) //POJ 2632 Crashing Robots 118 { 119 //freopen ("G.in", "r", stdin); 120 121 int t; 122 scanf ("%d", &t); 123 while (t--) 124 { 125 int N, M, n, m; 126 scanf ("%d%d", &M, &N); 127 scanf ("%d%d", &n, &m); 128 for (int i=1; i<=n; ++i) 129 { 130 scanf ("%d %d %c", &node[i].y, &node[i].x, &node[i].dir); 131 node[i].x = N + 1 - node[i].x; 132 } 133 for (int i=1; i<=m; ++i) 134 { 135 scanf ("%d %c %d", &op[i].name, &op[i].move, &op[i].t); 136 } 137 138 flag = true; 139 work (N, M, n, m); 140 } 141 142 return 0; 143 } 144 145 /* 146 Robot 1 crashes into the wall 147 Robot 1 crashes into robot 2 148 OK 149 Robot 1 crashes into robot 2 150 */
转载于:https://www.cnblogs.com/Running-Time/p/4372497.html
模拟 POJ 2632 Crashing Robots相关推荐
- POJ 1573 POJ 2632(两道有趣的Robot)实例
/* ** POJ 2632 Crashing Robots ** Created by Rayn @@ 2014/04/16 ** 坑爹的模拟题,脑壳不清晰的就要被坑惨了 */ #include & ...
- HDU 2300 Crashing Robots
Crashing Robots 题意 模拟多个机器人在四个方向的移动,检测crash robot, crash wall, OK这些状态 这是个模拟题需要注意几点: 理解转变方向后移动多少米,和转动方 ...
- 【POJ - 2632】Crashing Robots(模拟)
题干: In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ens ...
- 台州学院acm:Crashing Robots
描述 In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensu ...
- NKU 专题一 题解
A - Flip Game 总的情况数只有2^16次方种,显然直接bfs就可以了 1 #include<iostream> 2 #include<queue> 3 #inclu ...
- hdu与poj题目分类
POJ 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(po ...
- HOJ题目分类//放这儿没事刷刷学算法!嘻嘻!
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- Winter Camp I (下)
M题: Stones 代码: #include<iostream> #include<cstdio> #include<cstring> #include<a ...
- 杭电oj题目题型分类(转)
1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...
最新文章
- Linux追加文件内容并在内容前加上该文件名(awk, FILENAME功能妙用)
- [PHP] 自动加载的实现
- DOS批处理延时技术
- python定义一个类描述数字时钟_python自定义时钟类、定时任务类
- 采购季:云服务不断进化为企业带来更多选择
- 创建java普通工程 ( 4 )
- webpack使用笔记
- 2013数学建模B题
- 金士顿u盘数据恢复软件推荐
- php人民币转换,人民币大小写转换(PHP版)
- 【JAVA今法修真】 第七章 洞天风云起,索引混乱平
- 关于用python爬取自如网信息的价格问题(已解决)
- Java算法实现 BAT公司为什么要考算法 github
- pyhton———使用urllib 下载文件~~过掉岁月过掉风
- 重采样 上采样 下采样
- 服务器centos 内网代理上网- tinyproxy
- EVA2.0:大规模中文开放域对话预训练模型
- WebStorm2016.2 注册码及激活,2018.6.14亲测有效
- 如何用EXCEL求一组数的方差…
- 【uni-app】H5的返回拦截经验分享