UVA10881:

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822

 

题意:给出每个蚂蚁的初始状态,求时间T之后每只蚂蚁的状态

解法:如果忽略编号的话,可以把每只蚂蚁的掉头看成对穿而过,而最终的状态下,最左边的绝对是1号蚂蚁了

 

#include <stdio.h>
#include <algorithm>
using namespace std;

const int maxn = 10005;

struct NODE
{
    int no;
    int s;
    int t;//朝向,-1为左,0中,1右
}start[maxn],end[maxn];

int cmp(NODE x,NODE y)
{
    return x.s < y.s;
}

char turn[][10] = {"L","Turning","R"};
int order[maxn];

int main()
{
    int t,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        int L,T,N,i;
        scanf("%d%d%d",&L,&T,&N);
        for(i = 0;i<N;i++)
        {
            char c;
            start[i].no = i;
            scanf("%d %c",&start[i].s,&c);
            start[i].t = (c == 'L'?-1:1);
            end[i].no = 0;
            end[i].s = start[i].s + T*start[i].t;
            end[i].t = start[i].t;
        }
        sort(start,start+N,cmp);
        for(i = 0;i<N;i++)
        order[start[i].no] = i;
        sort(end,end+N,cmp);
        for(i = 0;i<N-1;i++)
        {
            if(end[i].s == end[i+1].s)
            end[i].t = end[i+1].t = 0;
        }
        printf("Case #%d:\n",cas++);
        for(i = 0;i<N;i++)
        {
            int a = order[i];
            if(end[a].s < 0 || end[a].s>L)
            printf("Fell off\n");
            else
            printf("%d %s\n",end[a].s,turn[end[a].t+1]);
        }
        printf("\n");
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值