Uva10881 - Piotr's Ants(蚂蚁)

这是一个等效变换问题,感觉解法还是蛮叼的。

应注意记录输入次序;

在按照Pos从大到小排序后,用一个数组存储输入次序;

在对Pos进行T的+/-后,重新按照Pos排序并将存储的输入次序附回原位(结构体中的位置)——以此表示变换后的该位置的结构体依然是第一次排序后的对象)

最后按输入次序输入。

#include <iostream>
using namespace std;
#include <cstdlib>
typedef struct insect
{
    int inputord;
    int pos;
    char str;
}INS;
int cmp1(const void*a,const void*b)
{
    INS x=*(INS*)a;
    INS y=*(INS*)b;
    return x.pos-y.pos;
}
int cmp2(const void*a,const void*b)
{
    INS x=*(INS*)a;
    INS y=*(INS*)b;
    return x.inputord-y.inputord;
}
INS ants[10050];
int order[10050];
int main()
{

    int L,T,n,i;
    int m;
    cin>>m;
    for(i=0;i<m;i++)
    {
        int j;

        cin>>L>>T>>n;
        for(j=0;j<n;j++)
        {
            ants[j].inputord=j;
            cin>>ants[j].pos>>ants[j].str;
        }
        qsort(ants,n,sizeof(INS),cmp1);
        for(j=0;j<n;j++)
            {
            order[j]=ants[j].inputord;
            if (ants[j].str=='R')
                ants[j].pos+=T;
            else if (ants[j].str=='L')
                ants[j].pos-=T;

            }
        qsort(ants,n,sizeof(INS),cmp1);
        for(j=0;j<n;j++)
            {
                ants[j].inputord=order[j];
                if(ants[j].pos==ants[j+1].pos&&j<n-1)
            {
                ants[j].str='T';
                ants[j+1].str='T';
            }
            }
        qsort(ants,n,sizeof(INS),cmp2);
        cout<<"Case #"<<i+1<<":"<<endl;
        for(j=0;j<n;j++)
        {
            if(ants[j].pos>L||ants[j].pos<0)
                cout<<"Fell off"<<endl;
            else
            {
                if(ants[j].str=='T')
                    cout<<ants[j].pos<<" Turning"<<endl;
                else
                    cout<<ants[j].pos<<' '<<ants[j].str<<endl;
            }
        }
        cout<<endl;

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值