UVA 10881 - Piotr‘s Ants

题目大意:一根L长的木板上有n只蚂蚁,每只蚂蚁初始有一个朝向,当两只蚂蚁相遇,会掉头走。求走了T步以后所有蚂蚁的状况。

解题思路:每只蚂蚁相遇后,相当于对穿过,只是对穿过的蚂蚁不是原来那只。但整个图所有蚂蚁的位置就是看作对穿过求得的。只是最初蚂蚁的相对位置与最后蚂蚁的相对位置的一样的,因为相遇就掉头,所以不会改变蚂蚁的相对位置。就是要求初始相对位置。。。

ac代码:

#include <iostream>
#include <algorithm>
using namespace std;
int n, L, T, num, order[10005], temp, cnt=1;
char c;
struct node
{
	int pos;
	int d;
	int init;
};
bool compare(node a, node b)
{
return a.pos < b.pos;
}
int main()
{
	node before[10005], after[10005];
	scanf("%d", &n);
	while (n--){
		scanf("%d%d%d", &L, &T, &num);
		for (int i=0; i<num; i++){
			scanf("%d %c", &before[i].pos, &c);
			before[i].d = (c == 'L'?-1:1);	
			before[i].init = i;
			after[i].d = before[i].d;
			after[i].pos = before[i].pos + before[i].d * T;
		}
		sort(before, before+num, compare);
		for (int i=0; i<num; i++)
			order[ before[i].init ] = i;
		sort(after, after+num, compare);
		for (int i=0; i<num-1; i++)
			if (after[i].pos == after[i+1].pos)
				after[i].d = after[i+1].d = 0;
		printf("Case #%d:\n", cnt++);
		for (int i=0; i<num; i++){
			temp = order[i];
			if (after[temp].pos > L || after[temp].pos < 0)
				printf("Fell off\n");
			else if (!after[temp].d)
				printf("%d Turning\n", after[temp].pos);
			else if (after[temp].d == 1)
				printf("%d R\n", after[temp].pos);
			else 
				printf("%d L\n", after[temp].pos);
		}
		printf("\n");
	}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值