UVa 10881 - Piotr's Ants

题目:有一些蚂蚁在一根棍子上爬行,每个蚂蚁有一个初始位置和方向,如果蚂蚁相撞则掉头,

            蚂蚁的速度为1,问时间T后,各个蚂蚁的位置和方向。

分析:模拟。如果蚂蚁没有区别,那么相撞可以忽略,认为各自继续前行。

            因为有上面的结论,在考虑编号的时候,只要在运动后,按照初始的顺序给予编号即可;

            首先,计算初始时每个蚂蚁的位置顺序编号记录在id中;

            然后,无视碰撞计算每个蚂蚁的位置,计算T时刻的每个蚂蚁的位置顺序记录在order中;

            最后,按照输入顺序查询输出,用T时刻第id[i]]的蚂蚁,替换初始第i只蚂蚁的位置方向;

说明:有点绕,(⊙v⊙)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int  order[10001];
int  id[10001];
int  now[10001];
char dir[10001];

int cmp(const void *a, const void *b)
{
	return now[*(int *)a] - now[*(int *)b];
}

int main()
{
	int c, L, T, n;
	while (~scanf("%d",&c)) {
		for (int t = 1; t <= c; ++ t) {
			scanf("%d%d%d",&L,&T,&n);
			for (int i = 0; i < n; ++ i) {
				scanf("%d %c",&now[i],&dir[i]);
				order[i] = i;
			}
			qsort(order, n, sizeof(int), cmp);
			for (int i = 0; i < n; ++ i) {
				id[order[i]] = i;
			}
			
			for (int i = 0; i < n; ++ i) {
				order[i] = i;
				if (dir[i] == 'L') {
					now[i] -= T;
				}else {
					now[i] += T;
				}
			}
			qsort(order, n, sizeof(int), cmp);
			
			printf("Case #%d:\n",t);
			for (int i = 0; i < n; ++ i) {
				int index = id[i];
				if (now[order[index]] < 0 || now[order[index]] > L) {
					puts("Fell off");
				}else {
					printf("%d ",now[order[index]]);
					if (index > 0 && now[order[index-1]] == now[order[index]] || 
						index < n-1 && now[order[index+1]] == now[order[index]]) {
						puts("Turning");
					}else {
						printf("%c\n",dir[order[index]]);
					}
				}
			}
			puts("");
		}
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值