UVA 10881 Piotr's Ants
来源:互联网 发布:月度m2数据 编辑:程序博客网 时间:2024/05/16 15:12
Piotr's Ants
Piotr likes playing with ants. He hasn of them on a horizontal poleL cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end upT seconds from now.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing 3 integers:L ,T andn
Output
For each test case, output one line containing "Case #x:" followed byn lines describing the locations and directions of then ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the polebeforeT seconds, print "Fell off" for that ant. Print an empty line after each test case.
210 1 41 R5 R3 L10 R10 2 34 R5 L8 R
Case #1:2 Turning6 R2 TurningFell offCase #2:3 L6 R10 R
【思路分析】
一个花了好几天都没有思路的问题。。。题意是在一个木棍上有若干个蚂蚁,它们会往左或者往右每秒移动一个单位,当两只蚂蚁相撞时,立即发生“弹性碰撞”,求在时刻T时每一只蚂蚁的状态。没有思路的原因是无法将其抽象为一个具体的模型。最后在大白书上看到“两只蚂蚁相撞时视为对穿而过”便迎刃而解了,即忽视每一步的具体细节,每一个蚂蚁的相对顺序是保持不变的,只需考虑初态和终态,最后根据其移动的距离判断其位置。除此之外,还需要记录每一个蚂蚁的初始位置,以便按序输出结果。
代码如下:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 1e5 + 5;int L,T,n;struct Ant{ int index; int pos; int dir; Ant(){} Ant(int i,int p,int d) : index(i),pos(p),dir(d){} bool operator < (const Ant& a) const { return pos < a.pos; }}before[maxn],last[maxn];int order[maxn];char dirName[3][10] = {"L","Turning","R"};void init(){ scanf("%d %d %d",&L,&T,&n); for(int i = 0;i < n;i++) { int pos,dir; char c; scanf("%d %c",&pos,&c); dir = c == 'L' ? -1 : 1; before[i] = Ant(i,pos,dir); last[i] = Ant(0,pos + T * dir,dir); //相撞时视为相穿而过,暂时不计算顺序、不改变方向 }}void solve(int t){ printf("Case #%d:\n",t); sort(before,before + n); for(int i = 0;i < n;i++) { order[before[i].index] = i; } sort(last,last + n); for(int i = 0;i < n - 1;i++) { if(last[i].pos == last[i + 1].pos)//判断T时刻是否有位置相同的,即相撞时的情况 { last[i].dir = last[i + 1].dir = 0; } } for(int i = 0;i < n;i++) { int temp = order[i]; if(last[temp].pos < 0 || last[temp].pos > L) printf("Fell off\n"); else { printf("%d %s\n",last[temp].pos,dirName[last[temp].dir + 1]); } } printf("\n");}int main(){ int t; scanf("%d",&t); for(int i = 1;i <= t;i++) { init(); solve(i); } return 0;}
- UVa - 10881 - Piotr's Ants
- uva 10881 - Piotr's Ants
- Uva-10881-Piotr's Ants
- uva 10881 - Piotr's Ants
- UVa 10881 Piotr's Ants
- UVA 10881 Piotr's Ants
- Piotr's ants UVA 10881
- UVa 10881 - Piotr's Ants
- UVa 10881 Piotr's Ants
- UVA - 10881 Piotr's Ants
- uva 10881 Piotr's Ants
- UVA - 10881 Piotr's Ants
- UVa 10881 - Piotr's Ants
- UVa 10881 - Piotr's Ants
- Uva 10881 - Piotr's Ants
- UVA 10881 Piotr's Ants
- UVA 10881 - Piotr's Ants
- 【UVA】10881-Piotr's Ants
- 约瑟夫环-递归算法
- property的使用
- 设计模式_门面模式
- Majority Element
- webService客户端传输xml中文乱码
- UVA 10881 Piotr's Ants
- Linux kernel (without create_proc_entry func) Proc文件系统的实例
- fedora双系统问题
- HDU3364 Lanterns【高斯消元法】
- 哈夫曼编/译码器
- LeetCode 10 正则表达式匹配
- 对实体—联系模型的基本概念的认识
- JLabel类的使用
- 正则表达式学习之邮箱匹配