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程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
随机推荐
- linux调度器源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 上期文章linux调度器源码分析 - 概述(一)已经把调度器相关的数据结构介绍了一遍,本篇着重通过代码说明 ...
- 7-51单片机ESP8266学习-AT指令(8266TCP服务器,编写自己的C#TCP客户端发信息给单片机控制小灯的亮灭)
http://www.cnblogs.com/yangfengwu/p/8780182.html 自己都是现做现写,如果想知道最终实现的功能,请看最后 先把源码和资料链接放到这里 链接: https: ...
- Python写代码的时候为什么要注释?Sun因此被Oracle收购
导读: 此块分为:1.注释的重要性 2.如何正确注释 注释的重要性 在我们看代码的时候,会遇到很多看不懂得代码,特别是在做项目的时候,代码的注释以及命名习惯的重要性就有了为什么这么说呢? 因为在很多情 ...
- Luogu4886 快递员 点分治
传送门 淀粉质好题啊qaq 我们先考虑随便选择一个点作为邮递中心,通过移动邮递中心找到更优的位置.将路径最大值求出,并将路径最大值对应的那一些路径拿出来考虑.可以知道,如果说这些路径中存在一条经过当前 ...
- ng-include文件实现ng-repeat
Angularjs实现自由度很高.比如ng-repeat可以以包含的文件中实现数据循环. 如: 当我们把这html文件被ng-include包含时,它完全能正常呈现对应的数据: 创建应用app: 创建 ...
- Momenta电话面试笔记
- 并行管理工具——pdsh
1. pdsh安装2. pdsh常规使用2.1 pdsh2.2 pdcp 并行管理的方式有很多种: 命令行 一般是for循环 脚本 一般是expect+ssh等自编辑脚本 工具 pssh,pdsh,m ...
- 利用 John the Ripper 破解用户登录密码
一.什么是 John the Ripper ? 看到这个标题,想必大家都很好奇,John the Ripper 是个什么东西呢?如果直译其名字的话就是: John 的撕裂者(工具). 相比大家都会觉得 ...
- Centos下MooseFS(MFS)分布式存储共享环境部署记录
分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,分布式文件系统的实际基于客户机/服务器模式.目前 ...
- 《Linux内核设计与实现》第四章学习笔记
<Linux内核设计与实现>第四章学习笔记 ——进程调度 姓名:王玮怡 学号:20135116 一.多任务 1.多任务操作系统的含义 多任务操作系统就是能同时并发地交 ...