poj2632 【模拟】
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 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
- 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 【模拟】的更多相关文章
- poj2632 模拟
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8388 Accepted: 3631 D ...
- POJ-2632 Crashing Robots模拟
题目链接: https://vjudge.net/problem/POJ-2632 题目大意: 在一个a×b的仓库里有n个机器人,编号为1到n.现在给出每一个机器人的坐标和它所面朝的方向,以及m条指令 ...
- POJ2632 Crashing Robots(模拟)
题目链接. 分析: 虽说是简单的模拟,却调试了很长时间. 调试这么长时间总结来的经验: 1.坐标系要和题目建的一样,要不就会有各种麻烦. 2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a ...
- App开发:模拟服务器数据接口 - MockApi
为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- HTML 事件(四) 模拟事件操作
本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4. ...
- 模拟AngularJS之依赖注入
一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...
- webapp应用--模拟电子书翻页效果
前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
随机推荐
- android 环境的配置
经过了长达好几天的她探索,一直出现各种问题,然后,也是一个一个的解决,但最后,解决烦了,就觉得重新开始配置android的环境了. 原来一直都是版本的问题,因为我之前下载的都是2014的版本,而这个版 ...
- LOJ500 ZQC的拼图 二分答案、DP
传送门 题意:给出$N$个直角三角形拼图和$M \times M$的网格,第$i$个直角三角形水平直角边边长为$\frac{1}{a_i}$,垂直直角边边长为$\frac{1}{b_i},$规定直角三 ...
- 重装系统之win10不能进入bios界面
原因 自Win10发布以来,新出厂的预装Win10的电脑都默认在UEFI模式下启动操作系统.UEFI启动是一种新的主板引导项,正被看做是有近20多年历史的BIOS 的继任者.顾名思义,快速启动是可以提 ...
- Idea Live Template代码片段总结
目录 Idea Live Template总结 一.演示 二.详细介绍 2.1 类型 2.2设置(win默认快捷键win+alt+s) 2.3 快捷键 2.4 实战 Idea Live Templat ...
- [BZOJ2125]最短路[圆方树]
题意 给定仙人掌,多次询问两点之间的最短路径. \(n\le 10000, Q\le 10000\) 分析 建出圆方树,分路径 lca 是圆点还是方点讨论. 预处理出根圆点到每个圆点的最短距离 \( ...
- Python下操作Memcache/Redis/RabbitMQ说明
一.MemcacheMemcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访 ...
- 理解使用static import 机制
J2SE 1.5里引入了“Static Import”机制,借助这一机制,可以用略掉所在的类或接口名的方式,来使用静态成员.本文介绍这一机制的使用方法,以及使用过程中的注意事项. 在Java程序中,是 ...
- Lotto HDU
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1342] 题意 分析 DFS 代码 #include<cstdio> #include< ...
- Liinux 学习心得
Linux 内核学习心得 姬梦馨 原创作品 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 反汇编一个简 ...
- Linux内核分析作业第五周
系统调用的三个层次(下) 一.给MenuOS增加time和time-asm命令 1.克隆并自动编译 MenuOS rm menu -rf 强制删除原menu文件 git clone https://g ...