题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1822

题目意思:有一条 L 厘米长的杆,上面有 n 只蚂蚁,给出每只蚂蚁的朝向以及离杆上最左端的距离,问 T 秒之后每只蚂蚁到达的位置,如果 T 秒后某个位置有多只蚂蚁同时到达,那么这堆蚂蚁处于的位置 + Turning,如果超过这条杆的长度,输出Fell off,其余情况是:蚂蚁位置+朝向

突发奇想,想做下这题,学习了 lrj 写法。

他的before 和 after 处理,使得蚂蚁在杆子上依次从左到右处理,而且这样做的好处是,不需要对当前的蚂蚁T秒后的位置与已经处理的蚂蚁作比较(检查是否有Turning 情况),大大节省了时间,否则有可能是10000 × 10000 呢~~~order 数组则记下原来最开始时蚂蚁的编号,是为了输出按回原输入啦。

好简洁,好好向他学习^_^

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = + ; struct Ant
{
int id; // 蚂蚁编号
int p; // 蚂蚁位置
int d; // 蚂蚁朝向 左: -1 转向:0 右:1
bool operator < (const Ant& a) const
{
return p < a.p;
}
}before[maxn], after[maxn]; int order[maxn];
char dirname[][] = {"L", "Turning", "R"}; int main()
{
int N, L, T, n;
while (scanf("%d", &N) != EOF)
{
for (int cas = ; cas <= N; cas++)
{
scanf("%d%d%d", &L, &T, &n);
int dir;
char pos;
for (int i = ; i < n; i++)
{
cin >> dir >> pos;
int d = (pos == 'R' ? : -);
before[i] = (Ant){i, dir, d};
after[i] = (Ant){, dir+d*T, d};
}
sort(before, before+n);
for (int i = ; i < n; i++)
order[before[i].id] = i;
sort(after, after+n);
for (int i = ; i < n-; i++)
{
if (after[i].p == after[i+].p)
after[i].d = after[i+].d = ; // 碰撞
}
printf("Case #%d:\n", cas);
for (int i = ; i < n; i++)
{
int a = order[i];
if (after[a].p < || after[a].p > L)
printf("Fell off\n");
else
printf("%d %s\n", after[a].p, dirname[after[a].d+]); // +1是因为数组下标没有-1
}
puts("");
}
}
return ;
}

uva 10881 Piotr's Ants 解题报告的更多相关文章

  1. UVA.10881 Piotr's Ants (思维题)

    UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...

  2. 思维题 UVA 10881 Piotr's Ants

    题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...

  3. cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁

    1456. [UVa 10881,Piotr's Ants]蚂蚁 ★   输入文件:Ants.in   输出文件:Ants.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述 ...

  4. POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题

    两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...

  5. UVA 10881 Piotr's Ants(等效变换 sort结构体排序)

    Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...

  6. [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]

    "One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...

  7. UVA 10881 - Piotr's Ants【模拟+思维】

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. Uva 10881 Piotr’s Ants 蚂蚁

    一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...

  9. uva 10881 - Piotr's Ants

    这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变: 另外,又可以把蚂蚁看成运动方向不变: 代码: #include<cstdio> #include<algorithm> ...

随机推荐

  1. iOS Mobile Development: Using Xcode Targets to Reuse the Code 使用xcode targets来实现代码复用

    In the context of iOS mobile app development, a clone is simply an app that is based off another mob ...

  2. 奇酷手机显示Log

    1.在桌面点击拨号,在拨号盘输入“*20121220#”,进入工程模式;2.看到日志输出等级,点进去 Log print enable 选 enable Java log level 选 LOGV C ...

  3. SilverLight:基础控件使用(3)-DataGrid控件

    ylbtech-SilverLight-Basic-Control:基础控件使用(3)-DataGrid控件 DataGrid控件-后台绑定 自动生成表列 不自动生成表列 1.A,返回顶部Person ...

  4. vue Syntax Error: Unexpected token {

    > music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...

  5. SpringMVC同时支持多视图(JSP,Velocity,Freemarker等)的一种思路实现

    在基于SpringMVC的项目中有时需要同时使用多种视图格式,如jsp,velocity及freemarker等,通过不同的请求路径配置规则,映射到不同的视图文件.下面我提供一种思路,通过视图模板文件 ...

  6. jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径

    遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...

  7. 4.php整合Memcached

    用法: nginx响应请求时,直接请求memcached, 如果没有相应的内容,再回调PHP页面,去查询database,并写入memcached. 分析: memcached是k/v存储, key- ...

  8. python(39)- 网络编程socket练习

    基于tcp的套接字实现远程执行命令的操作 #服务端 import socket import subprocess phone=socket.socket(socket.AF_INET,socket. ...

  9. windows xp下mysql5.0安装

    安装注意要点: 1.不要安装在带有中文的安装路径 2.之前若有安装过mysql,请一定要卸载干净       MySQL安装的图解5.0.28 - CSDN

  10. 代码运行时间 检测锁及死锁详细信息,及sql语句 平台转化

    代码运行时间   System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 ...