UVA 10881 Piotr's Ants

本文通过一个蚂蚁碰撞模拟问题,介绍了一种解决多蚂蚁在一条直线上相向而行时如何判断其最终位置的方法。使用C++实现,涉及排序、碰撞检测等关键技术。

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

思路:大白例题


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 10000
#define LL long long
int cas=1,T;
struct Ant
{
	int id;
	int p;
	int d;
	bool operator<(const Ant&a)const
	{
		return p<a.p;
	}
}before[maxn],after[maxn];
const char dirName[][10]={"L","Turning","R"};
int order[maxn];
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		int L,T,n;
		printf("Case #%d:\n",cas++);
		scanf("%d%d%d",&L,&T,&n);
        for (int i = 0;i<n;i++)
		{
			int p,d;
			char c;
			scanf("%d %c",&p,&c);
			d=(c=='L'?-1:1);
			before[i]=(Ant){i,p,d};
			after[i]=(Ant){0,p+T*d,d};
		}
		sort(before,before+n);
		for (int i = 0;i<n;i++)
			order[before[i].id]=i;
		sort(after,after+n);
		for (int i = 0;i<n-1;i++)
			if (after[i].p==after[i+1].p)
				after[i].d=after[i+1].d=0;
		for (int i = 0;i<n;i++)
		{
			int a = order[i];
			if (after[a].p<0 || after[a].p>L)
				printf("Fell off\n");
			else
				printf("%d %s\n",after[a].p,dirName[after[a].d+1]);
		}
		printf("\n");
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值