UVA 10881 Piotr's Ants (思维)

Problem D
Piotr's Ants
Time Limit: 2 seconds
"One thing is for certain: there is no stopping them;
the ants will soon be here. And I, for one, welcome our
new insect overlords."
Kent Brockman

Piotr likes playing with ants. He has n of them on a horizontalpoleL cm long. Each ant is facing either left or right and walksat a constant speed of 1 cm/s. When two ants bump into each other, theyboth turn around (instantaneously) and start walking in opposite directions.Piotr knows where each of the ants starts and which direction it is facingand wants to calculate where the ants will end upT seconds from now.

Input
The first line of input gives the number of cases, N. Ntest cases follow. Each one starts with a line containing 3 integers:L ,T and n (0 <= n <= 10000) .The nextn lines give the locations of the n ants (measuredin cm from the left end of the pole) and the direction they are facing(L or R).

Output
For each test case, output one line containing "Case #x:"followed byn lines describing the locations and directions of then ants in the same format and order as in the input. If two or moreants are at the same location, print "Turning" instead of "L" or "R" fortheir direction. If an ant falls off the pole before T seconds,print "Fell off" for that ant. Print an empty line after each test case.

Sample InputSample Output
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


Problemsetter: Igor Naverniouk

Alternate solutions: Frank Pok Man Chu and Yury Kholondyrev


题意:

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

思路:

1.根据相撞后掉头爬,可以推断出蚂蚁的相对位置不发生改变。

2.两只蚂蚁相撞后掉头的移动情况,可以看做两只蚂蚁“对穿而过”的移动情况。

发现前面两点规律,需要再注意蚂蚁的输入顺序和蚂蚁是否跌落。


/*************************************************************************
	> File Name: E.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年09月30日 星期一 13时31分05秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif



#define M 10010

struct node {
    int pos,dire,id;
    bool operator < (const node b) const {
        return pos < b.pos;
    }
}data1[M],data2[M];

int num[M];

int main(int argc, char** argv) {
    //read;
    int t,L,T,n,pos,d;
    char dire;
    scanf("%d",&t);
    for(int k=1; k<=t; k++) {
        printf("Case #%d:\n",k);
        scanf("%d%d%d",&L,&T,&n);
        for(int i=1; i<=n; i++) {
            scanf("%d %c\n",&pos,&dire);
            if(dire == 'L') d = -1;
            else d = 1;
            data1[i].id = i;
            data1[i].pos = pos;
            data2[i].pos = pos + d * T;
            data1[i].dire = data2[i].dire = d;
        }
        sort(data1 + 1,data1 + n + 1);
        for(int i=1; i<=n; i++) {
            num[data1[i].id] = i;
        }
        sort(data2 + 1,data2 + n + 1);
        for(int i=1; i<n; i++) {
            if(data2[i].pos == data2[i+1].pos) data2[i].dire = data2[i+1].dire = 0;
        }
        for(int i=1; i<=n; i++) {
            int a = num[i];
    //printf("%d\n",a);
            if(data2[a].pos < 0 || data2[a].pos > L) printf("Fell off");
            else {
                printf("%d ",data2[a].pos);
                if(data2[a].dire == -1) printf("L");
                else if(data2[a].dire == 0) printf("Turning");
                else printf("R");
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值