In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving.
A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.

Input

The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 <= A, B <= 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction.
The second line contains two integers, 1 <= N, M <= 100, denoting the numbers of robots and instructions respectively. 
Then follow N lines with two integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position. 
 
Figure 1: The starting positions of the robots in the sample warehouse
Finally there are M lines, giving the instructions in sequential order. 
An instruction has the following format: 
< robot #> < action> < repeat> 
Where is one of

  • L: turn left 90 degrees,
  • R: turn right 90 degrees, or
  • F: move forward one meter,

and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.

Output

Output one line for each test case:

  • 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.

Only the first crash is to be reported.

Sample Input

4
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

Sample Output

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2 思路:
直接暴力模拟这些操作。 ps:写的时候没想太多,直接强行模拟,结果发现有的地方写错了,强行改回来最后发现把代码改得巨丑无比,心态炸了。还好ac了,要不实在没法改了。
代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<list>
using namespace std;
#define ll long long
const int Mod = 1e9+;
const int inf = 1e9;
const int Max = 1e5+;
vector<int>vt[Max];
//void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
//ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;} ��Ԫ
//int gcd(int a,int b) { return (b>0)?gcd(b,a%b):a; } ��С��Լ
//int lcm(int a, int b) { return a*b/gcd(a, b); } ������
int fun1(char c){
if(c == 'N') return ;
if(c == 'E') return ;
if(c == 'S') return ;
if(c == 'W') return ;
}
int main()
{
int t,l,r,n,m,k[],cnt[],i,j,mp[][],x[],y[],v[];
char p[],z;
cin>>t;
while(t--){
int ans1=,ans2=,ans3=,flag=;
memset(mp,,sizeof(mp));
memset(v,,sizeof(v));
cin>>l>>r;
cin>>n>>m;
for(i=;i<=n;i++){
cin>>x[i]>>y[i]>>z;
mp[x[i]][y[i]] = i;
v[i] = fun1(z);
}
for(i=;i<=m;i++)
cin>>k[i]>>p[i]>>cnt[i];
for(i=;i<=m;i++){
if(p[i]=='L'){
v[k[i]] -= cnt[i];
while(v[k[i]]<)
v[k[i]] += ;
}
if(p[i]=='R'){
v[k[i]] += cnt[i]; v[k[i]] %= ;
}
if(p[i]=='F'){
if(v[k[i]]==){
for(j=;j<=cnt[i];j++){
if(mp[x[k[i]]][y[k[i]]+j]!=){
ans1 = k[i];ans2 = mp[x[k[i]]][y[k[i]]+j];
flag = ; break;
}
if(y[k[i]]+j>r){
flag = ; ans3 = k[i];break;
}
}
if(flag==){
mp[x[k[i]]][y[k[i]]+cnt[i]] = k[i];
mp[x[k[i]]][y[k[i]]] = ;
y[k[i]] += cnt[i];
}
}
if(v[k[i]]==){
for(j=;j<=cnt[i];j++){
if(mp[x[k[i]]+j][y[k[i]]]!=){
ans1 = k[i];ans2 = mp[x[k[i]]+j][y[k[i]]];
flag = ; break;
}
if(x[k[i]]+j>l){
flag = ; ans3 = k[i];break;
}
}
if(flag==){
mp[x[k[i]]+cnt[i]][y[k[i]]] = k[i];
mp[x[k[i]]][y[k[i]]] = ;
x[k[i]] += cnt[i];
}
}
if(v[k[i]]==){
for(j=;j<=cnt[i];j++){
if(mp[x[k[i]]][y[k[i]]-j]!=){
ans1 = k[i];ans2 = mp[x[k[i]]][y[k[i]]-j];
flag = ; break;
}
if(y[k[i]]-j<=){
flag = ; ans3 = k[i];break;
}
}
if(flag==){
mp[x[k[i]]][y[k[i]]-cnt[i]] = k[i];
mp[x[k[i]]][y[k[i]]] = ;
y[k[i]] -= cnt[i];
}
}
if(v[k[i]]==){
for(j=;j<=cnt[i];j++){
if(mp[x[k[i]]-j][y[k[i]]]!=){
ans1 = k[i];ans2 = mp[x[k[i]]-j][y[k[i]]];
flag = ; break;
}
if(x[k[i]]-j<=){
flag = ; ans3 = k[i];break;
}
}
if(flag==){
mp[x[k[i]]-cnt[i]][y[k[i]]] = k[i];
mp[x[k[i]]][y[k[i]]] = ;
x[k[i]] -= cnt[i];
}
}
}
if(flag!=)
break;
}
if(flag == ) cout<<"OK"<<endl;
else if(flag == ) cout<<"Robot "<<ans1<<" crashes into robot "<<ans2<<endl;
else if(flag == ) cout<<"Robot "<<ans3<<" crashes into the wall"<<endl;
}
return ;
}

打完后看了下室友的代码,越发觉得自己是个智障了。。。。

室友代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int MAX=1e2+;
const double eps=1e-;
#define INF 0x7fffffff
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define mst(a) memset(a,0,sizeof(a))
#define mstn(a,n) memset(a,n,sizeof(a))
struct num{int x,y,fac;}a[MAX];
//bool cmp(const num &x, const num &y){return x.l==y.l?x.r<y.r:x.l<y.l;}
int flag,mx,my,n,m; void wrong(int x,int y,int i)
{
if(x==||x==mx+||y==||y==my+)
printf("Robot %d crashes into the wall\n",i),flag=;
else
{
int d=;
FOR(j,,n)
{
if(j==i) continue;
else if(a[j].x==x&&a[j].y==y)
d=j;
}
if(d)
{
printf("Robot %d crashes into robot %d\n",i,d);
flag=;
}
}
} void turn(int i,char ch,int t)
{
if(!flag) return;
if(ch=='L')
a[i].fac=(a[i].fac-t%+)%;
else if(ch=='R')
a[i].fac=(a[i].fac+t%)%;
else
while(t--)
{
if(a[i].fac==)
a[i].y++;
if(a[i].fac==)
a[i].x++;
if(a[i].fac==)
a[i].y--;
if(a[i].fac==)
a[i].x--;
wrong(a[i].x,a[i].y,i);
if(!flag)
break;
}
} int main()
{
int T,x,y;
char ch;
cin>>T;
while(T--)
{
cin>>mx>>my;
flag=;
cin>>n>>m;
FOR(i,,n)
{
cin>>x>>y>>ch;
a[i].x=x,a[i].y=y;
if(ch=='N') a[i].fac=;
if(ch=='E') a[i].fac=;
if(ch=='S') a[i].fac=;
if(ch=='W') a[i].fac=;
}
FOR(i,,m)
{
int num,t;
cin>>num>>ch>>t;
if(flag)
turn(num,ch,t);
}
if(flag)
cout<<"OK"<<endl;
}
return ;
}

耗时都是16ms,但空间复杂度和可读性差很多啊。。。

poj2632 【模拟】的更多相关文章

  1. poj2632 模拟

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8388   Accepted: 3631 D ...

  2. POJ-2632 Crashing Robots模拟

    题目链接: https://vjudge.net/problem/POJ-2632 题目大意: 在一个a×b的仓库里有n个机器人,编号为1到n.现在给出每一个机器人的坐标和它所面朝的方向,以及m条指令 ...

  3. POJ2632 Crashing Robots(模拟)

    题目链接. 分析: 虽说是简单的模拟,却调试了很长时间. 调试这么长时间总结来的经验: 1.坐标系要和题目建的一样,要不就会有各种麻烦. 2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a ...

  4. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  7. HTML 事件(四) 模拟事件操作

    本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4.  ...

  8. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

  9. webapp应用--模拟电子书翻页效果

    前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...

随机推荐

  1. 车轮升级PHP7踩过的一些坑

    社区php7升级记录 社区服务器已经全部完成升级,这里记录一下社区升级php7所遇到的问题,可以分为四个类型 扩展支持的变化,导致需要修改配置甚至调整替换操作的类库 php7语法检查比之前变得严格,部 ...

  2. vue2.0中使用less

    第一部分:Less语言 与上一篇<vue2.0中使用sass>介绍的Sass语言一样,Less语言也是一种CSS的扩展语言,增加了变量.混合(minin).函数等功能,让CSS更易维护.方 ...

  3. C# SignalR 即时通信

    MSDN教程:https://docs.microsoft.com/en-us/aspnet/signalr/ 个人博客:http://www.cnblogs.com/zhili/p/SignalRQ ...

  4. 请允许我转载一篇关于套接字的博客:Socket

    这一篇文章,我将图文并茂地介绍Socket编程的基础知识,我相信,如果你按照步骤做完实验,一定可以对Socket编程有更好地理解. 本文源代码,可以通过这里下载 http://files.cnblog ...

  5. python基础学习笔记(九)

    python异常 python用异常对象(exception object)来表示异常情况.遇到错误后,会引发异常.如果异常对象并未被处理或捕捉,程序就会用所谓的 回溯(Traceback, 一种错误 ...

  6. Individual P1: Preparation

    Individual Project - Word frequency program tally the frequency of words under a directory (2 modes) ...

  7. Beta阶段爬取数目预估

    预计于12月29号能进行Beta版本发布. Beta阶段我们的爬取动作应该更有针对性,在爬取期间如若数据处理小组有需求,会优先爬取数据处理小组提供的种子链接.预估在项目展示之前能够爬取的数目: 普通网 ...

  8. 《Linux内核设计与实现》第三章读书笔记

    一.进程(任务)描述 1.进程是处于执行期的程序:除了可执行程序代码,还包括打开的文件.挂起的信号.内核内部数据.一个或者多个执行线程等多种资源 线程是在进程活动中的对象:内核调度的对象是线程而不是进 ...

  9. Linux内核分析 笔记六 进程的描述和进程的创建 ——by王玥

    一.知识点总结 (一)进程的描述 1.操作系统内核里有三大功能: 进程管理 内存管理 文件系统 2.进程描述符:task_struct 2.进程描述符——struct task_struct 1. p ...

  10. Linux内核设计期中总结

    Linux内核设计期中总结 ● 知识点 一.计算机是如何工作的 计算机是按照冯·诺依曼存储程序的原理. 在执行程序时须先将要执行的相关程序和数据放入内存储器中,在执行程序时CPU根据当前程序指针寄存器 ...