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程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
随机推荐
- SkylineGlobe TEPro 6.6.1 二次开发导出KML或者KMZ文件示例代码
其实Skyline的fly文件跟kml文件很像很像,只不过一个是编码加密的,另一个早已经成为OGC的通用标准: 喜欢Skyline的小伙伴们试试下面的代码吧,细心的人能发现彩蛋哦. <!DOCT ...
- char.IsLetter的使用
先看一下下面的代码,大家会觉得控制台输出什么? 输出:Chiantxt .对吗? 因为你看到char.IsLetter这个方法的文字的注释是这样写的: 但实际上输出的结果是这样的: ??? 怎么还输 ...
- python_第一章
从今天开始,正式开始学习python书籍:python 编程:从入门到实践. 感兴趣的读者可以去网上搜索这本书,适合读者入门,读下来,不会有任何 晦涩难懂的知识. 1.排序: 正排:sort() ...
- C#批量插入数据到Sqlserver中的四种方式 - 转
先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...
- Elasticsearch 系列文章汇总(持续更新...)
系列文章列表 Query DSL Query DSL 概要,MatchAllQuery,全文查询简述 Match Query Match Phrase Query 和 Match Phrase Pre ...
- .Net架构篇:思考如何设计一款实用的分布式监控系统?
前言 无论从最早期的unix操作系统,还是曾经大行其道的单体式应用,还是现在日益流行的微服务架构,始终都离不开监控的身影.如windows的任务管理器,linux的top命令,都可以看作是监控的面板. ...
- 支持自定义协议的虚拟仪器【winform版】
首先,这个程序的由来,额,工作以来,做的最久的就是上位机,对市面上的大部分组态软件都感到不满,不好用,LabView虽然用起来不错,但是入门还是不够简单,刚好现在工作比较闲(已经不再做上位机了),所以 ...
- Spring方法级别的验证
设置验证点及验证方式(1)Spring方法级别的验证有多种验证方式,比较常用的有 @NotBlank:主要是对字符串的验证,不为null且去除空白符之后长度大于0 @NotNull:主要是对对象的验证 ...
- 《Linux内核分析》实践2
<Linux及安全>实践2 一.Linux基本内核模块 1.1什么是内核模块 linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一 ...
- 基于SSH的高校网上选课系统的质量属性的实现
我对于基于SSH的高校网上选课系统的质量属性的实现是从可用性.性能.安全性.可维护性.易用性五个方面进行的实现. 可用性方面: 实现方式:(1)当系统试图超出限制范围来进行课程查询或选课时必须进行错误 ...