【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁

本文探讨了蚂蚁在特定条件下沿直线移动的问题,通过输入蚂蚁的数量、移动距离和初始位置等参数,计算并输出每只蚂蚁在指定时间后的最终位置。文章详细介绍了问题的输入输出格式、解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

</pre></center><center style="font-family: Simsun;font-size:14px;"><span style="font-size:32px;"><strong>Problem D</strong></span><span style="font-size:24px;"><strong>Piotr's Ants</strong></span><span style="font-size:18px;">Time Limit: 2 seconds</span></center><p style="font-family: Simsun;font-size:14px;"></p><table border="0"><tbody><tr><td><small><em>"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one, welcome ournew insect overlords."</em></small></td></tr></tbody></table><p align="right" style="font-family: Simsun;font-size:14px;"><tt>Kent Brockman</tt></p><p class="paragraph" style="font-family: Simsun;font-size:14px;">Piotr likes playing with ants. He has <strong>n</strong> of them on a horizontal pole <strong>L</strong> 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 up <strong>T</strong> seconds from now.</p><p class="paragraph" style="font-family: Simsun;font-size:14px;"><span style="font-size:24px;"><strong>Input</strong></span>The first line of input gives the number of cases, <strong>N</strong>. <strong>N</strong> test cases follow. Each one starts with a line containing 3 integers: <strong>L</strong> , <strong>T</strong> and <strong>n</strong> <nobr>(0 <= <strong>n</strong> <= 10000)</nobr>. The next <strong>n</strong> lines give the locations of the <strong>n</strong> ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).</p><p class="paragraph" style="font-family: Simsun;font-size:14px;"><span style="font-size:24px;"><strong>Output</strong></span>For each test case, output one line containing "Case #<strong>x</strong>:" followed by <strong>n</strong> lines describing the locations and directions of the <strong>n</strong> 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 pole <em>before</em> <strong>T</strong>seconds, print "Fell off" for that ant. Print an empty line after each test case.</p><table width="100%" cellspacing="0" bordercolor="black" border="1" cellpadding="5" style="font-family: Simsun;"><tbody><tr valign="TOP"><td><span style="font-size:24px;"><strong>Sample Input</strong></span></td><td><span style="font-size:24px;"><strong>Sample Output</strong></span></td></tr><tr bgcolor="#D0D0D0" valign="TOP"><td><pre>2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R



题目大意:

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

(n<=10000)





    思路:

          如果不思考的去模拟的话,,显然要在10000的数据面前跪  

             要发现到二个性质:

                            1.如果两个蚂蚁相撞可以看做擦肩而过。

                            2.蚂蚁从左端开始的排列顺序永远不变。


              这样就可以根据性质一直接算出所有蚂蚁最终状态(但不知道那个蚂蚁的编号)

              再根据性质二推算出每个蚂蚁应该在的位置(即知道蚂蚁是哪只初始状态的)


              一道很巧妙的模拟


很可耻的看了题解,浪费的一道好题

#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct node
{
	int pos;
	char direct;
	int rank;
}node;
node ant[10100];
int RANK[10100];
int cmp(const void *i,const void *j)
{
	node *ii=(node *)i,*jj=(node *)j;
	return ii->pos-jj->pos;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	int K,KK;
	int L,T,n;
	while(scanf("%d",&K)!=EOF)
	{
	KK=K;
	while(K--)
	{
		scanf("%d%d%d",&L,&T,&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d %c",&ant[i].pos,&ant[i].direct);
			ant[i].rank=i;
		}
		qsort(ant+1,n,sizeof(ant[1]),cmp);
		for(int i=1;i<=n;i++)
		{
		RANK[ant[i].rank]=i;
		if(ant[i].direct=='R')
		ant[i].pos+=T;
		else ant[i].pos-=T;
		}
		qsort(ant+1,n,sizeof(ant[1]),cmp);
		for(int i=1;i<=n;i++)
		if(ant[i].pos==ant[i-1].pos) ant[i-1].direct=ant[i].direct='T';
		printf("Case #%d:\n",KK-K);
		for(int i=1;i<=n;i++)
		{
			if(ant[RANK[i]].pos>=0&&ant[RANK[i]].pos<=L)
				if(ant[RANK[i]].direct=='T')
				  printf("%d Turning\n",ant[RANK[i]].pos);
			    else 
			      printf("%d %c\n",ant[RANK[i]].pos,ant[RANK[i]].direct);
		    else printf("Fell off\n");
		}
		printf("\n");
	}
   }
	return 0;
}



转载于:https://www.cnblogs.com/zy691357966/p/5480465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值