UVA 10881 Piotr's Ants

本文介绍了一个蚂蚁碰撞模拟程序,通过跟踪蚂蚁在一条线上相向而行并发生碰撞后的状态变化,展示了如何使用C++进行简单的碰撞模拟及排序算法的应用。

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

蚂蚁的位置,我们都是可以得到的,因为蚂蚁相撞掉头可以看做是对穿而过,位置和方向

都是确定的就是第几个输出他们,确定谁是谁,蚂蚁从左到右拍个序,每个蚂蚁从左到右

排在第几位是不会变得,只要按输入顺序输出他们就行了。

#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
//ios::sync_with_stdio(false);
const int maxn=10009;
struct ant
{
    int id;
    int p;
    int d;
}before[maxn],after[maxn];
bool cmp(ant a,ant b)
{
    return a.p<b.p;
}
const char dir[][10]={"L","Turning","R"};
int order[maxn];
int main()
{
   int t;
   scanf("%d",&t);
   for(int cnt=1;cnt<=t;cnt++)
   {
       int L,T,n;
       printf("Case #%d:\n",cnt);
       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,cmp);
       for(int i=0;i<n;i++)
       {
           order[before[i].id]=i;
       }
       sort(after,after+n,cmp);
       for(int i=0;i<n-1;i++)
        if(after[i].p==after[i+1].p)after[i].d=after[i+1].d=0;

       //print
       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,dir[after[a].d+1]);
       }
       printf("\n");
   }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值