uva 10881(贪心)
来源:互联网 发布:php项目架构设计文档 编辑:程序博客网 时间:2024/06/03 17:20
题意:一个长度为L的木棍上有n只蚂蚁,给出了每只蚂蚁的初始位置和朝向,然后蚂蚁开始行走,如果两只蚂蚁相撞,就立刻掉头,直到掉下木棍,问t秒后每只蚂蚁的位置和状态。
题解:因为每只蚂蚁会因为碰撞而掉头,所以相对位置是不变的,为了更容易考虑计算,那么相撞的蚂蚁可以看做是对穿而过,得到输出结果时只要按初始的相对位置再对应上不同的蚂蚁就可以了。
#include <stdio.h>#include <algorithm>using namespace std;const int N = 10005;int l, T, n, order[N];struct Ant {int dir;int pos;int flag;}sta[N], aft[N];char s[3][10] = {"L", "Turning", "R"};int cmp(Ant a, Ant b) {return a.pos < b.pos;}int main() {int t, cas = 1;scanf("%d", &t);while (t--) {char c;scanf("%d%d%d", &l, &T, &n);for (int i = 0; i < n; i++) {scanf("%d %c", &sta[i].pos, &c);sta[i].flag = i;sta[i].dir = (c == 'L' ? -1 : 1);aft[i].pos = sta[i].pos + T * sta[i].dir;aft[i].dir = sta[i].dir;}sort(sta, sta + n, cmp);for (int i = 0; i < n; i++)order[sta[i].flag] = i;sort(aft, aft + n, cmp);for (int i = 0; i < n - 1; i++)if (aft[i].pos == aft[i + 1].pos)aft[i].dir = aft[i + 1].dir = 0;printf("Case #%d:\n", cas++);for (int i = 0; i < n; i++) {if (aft[order[i]].pos < 0 || aft[order[i]].pos > l)printf("Fell off\n");elseprintf("%d %s\n", aft[order[i]].pos, s[aft[order[i]].dir + 1]);}printf("\n");}return 0;}
0 0
- uva 10881(贪心)
- uva 11389(贪心)
- Uva 11100(贪心)
- uva 1388 - Graveyard(贪心)
- UVA 10037 - Bridge(贪心)
- uva 10037 - Bridge(贪心)
- Uva 10382 贪心
- UVa:10148 Advertisement(贪心)
- Commando War-uva 贪心
- UVA 10714 蚂蚁贪心
- uva 11292 贪心
- File Fragmentation+uva+贪心
- Add All +uva+贪心
- Bit Mask uva+贪心
- EDITOR NOTTOOBAD uva+贪心
- uva 1511 - Soju(贪心)
- UVA 1511 Soju(贪心)
- uva 311(贪心)
- C# .NET实现将多个pdf文件合并为一个
- raw socket数据发送接收UDP
- LRU 算法实现
- Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
- 我的git笔记
- uva 10881(贪心)
- 尽管去做 ——无压工作的艺术
- C++后台服务程序开发模式
- 转自知呼:努力了七年却依旧被中产阶级家庭子女完爆,我该如何调整心态?
- 【问底】伍艺:一种基于Rsync算法的数据库备份方案设计
- MyBatis中出现Mapped Statements collection does not contain value异常解决方案
- 异步操作AsyncTask(一)从网络下载图片
- 大气压传感器基础知识
- Android blueZ HCI(一):hciconfig实现及常用方法