poj-2632-Crashing Robots
2016-03-28 14:54
316 查看
传送门
大意:一个仓库里面有几个机器人,给你机器人的初始位置及方向,然后让机器人执行一些指令,输出指令执行结果
如:
Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
OK, if no crashing occurs.
直接模拟机器人就行了,
tip:用map把方向转换成数字,这样每次转方向的时候只要取模就行了
大意:一个仓库里面有几个机器人,给你机器人的初始位置及方向,然后让机器人执行一些指令,输出指令执行结果
如:
Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
OK, if no crashing occurs.
直接模拟机器人就行了,
tip:用map把方向转换成数字,这样每次转方向的时候只要取模就行了
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <string> #include <map> #include <algorithm> #define N 110 #define ll long long using namespace std; struct robot{ int x,y; int dir; }r ; int Map , a, b, n, m; map<char, int> mm; bool fun(int num, int rep){ int i, x = r[num].x, y = r[num].y; if (r[num].dir == 0){ for (i = 1; i <= rep; i++){ if (x+i > a){ printf("Robot %d crashes into the wall\n", num); return true; } if (Map[x+i][y]){ printf("Robot %d crashes into robot %d\n", num, Map[x+i][y]); return true; }else{ Map[x+i-1][y] = 0; Map[x+i][y] = num; r[num].x = x+i; } } }else if (r[num].dir == 1){ for (i = 1; i <= rep; i++){ if (y-i < 1){ printf("Robot %d crashes into the wall\n", num); return true; } if (Map[x][y-i]){ printf("Robot %d crashes into robot %d\n", num, Map[x][y-i]); return true; }else{ Map[x][y-i+1] = 0; Map[x][y-i] = num; r[num].y = y-i; } } }else if (r[num].dir == 2){ for (i = 1; i <= rep; i++){ if (x-i < 1){ printf("Robot %d crashes into the wall\n", num); return true; } if (Map[x-i][y]){ printf("Robot %d crashes into robot %d\n", num, Map[x-i][y]); return true; }else{ Map[x-i+1][y] = 0; Map[x-i][y] = num; r[num].x = x-i; } } }else{ for (i = 1; i <= rep; i++){ if (y+i > b){ printf("Robot %d crashes into the wall\n", num); return true; } if (Map[x][y+i]){ printf("Robot %d crashes into robot %d\n", num, Map[x][y+i]); return true; }else{ Map[x][y+i-1] = 0; Map[x][y+i] = num; r[num].y = y+i; } } } return false; } int main(){ #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif int i, j, T; int num, rep; char act; bool flag; mm.insert(pair<char, int>('E', 0)); mm.insert(pair<char, int>('S', 1)); mm.insert(pair<char, int>('W', 2)); mm.insert(pair<char, int>('N', 3)); scanf("%d", &T) ; while(T--){ flag = false; memset(Map, 0, sizeof(Map)); scanf("%d%d", &a, &b); scanf("%d%d", &n, &m); for (i = 1; i <= n; i++){ cin >> r[i].x >> r[i].y >> act; r[i].dir = mm[act]; Map[r[i].x][r[i].y] = i; } for (i = 0; i < m; i++){ cin >> num >> act >> rep; if (flag) continue; switch(act){ case 'L': r[num].dir = ((r[num].dir-rep)%4+4)%4; break; case 'R': r[num].dir = (r[num].dir+rep)%4; break; case 'F': flag = fun(num, rep); break; } } if (!flag){ printf("OK\n"); } } return 0; }
相关文章推荐
- ASP.NET 5已终结,迎来ASP.NET Core 1.0和.NET Core 1.0
- CSS 同时具备两个class的样式
- Oracle内使用游标实现分页
- POJ2308 Dearboy's Puzzle(DFS+BFS)
- 3D Touch
- Java中的数组
- php 高迸发下订单号的生成
- Python 多线程4-死锁
- 动态规划-完全背包问题
- Linux下利用backtrace追踪函数调用堆栈以及定位段错误
- 计算机视觉牛人博客和代码汇总(全)
- Windows环境下的NodeJS+NPM+Bower安装配置
- Mac 可设置环境变量的位置、查看和添加PATH环境变量
- exists、in的运用场景
- Leetcode - Add Two Numbers
- 【CxImage 702】VS2010 编译和使用。。。
- ElasticSearch Bulk 源码解析
- getApplicationContext()、getApplication()之间的区别
- IOS开发-OC学习-kvc,kvo
- java中HashSet详解