UVa 10881 - Piotr's Ants - 水题

题目描述:lrj后白书第一章第五道例题

题目分析:1)从宏观上看,蚂蚁碰撞并没有影响它们的爬行; 2) 蚂蚁的相对位置是不会变的。所以只需要处理初始状态和最后的状态即可。

下面是代码:

#include 
#include 
#include 
using namespace std;

const int maxn = 10100;

struct ant 
{
	int id;
	int dir;
	int pos;
	ant() {}
	ant(int a,int b,int c): id(a),pos(b),dir(c) {}
} before[maxn],after[maxn];

int T;
int l,t,n;
int order[maxn];
char direction[][10] = {"L","Turning","R"};
bool cmp(ant a,ant b)
{
	return a.pos < b.pos;
}
int main()
{
	int cas = 0;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&l,&t,&n);
		for(int i = 0; i < n; i++)
		{
			int pos,dir;
			char ch;
			scanf("%d %c",&pos,&ch);
			dir = (ch == 'L') ? -1 : 1;
			before[i] = ant(i,pos,dir);
			after[i] = ant(0,pos+dir*t,dir);
		}
		//for(int i = 0; i < n; i++) printf("%d ",before[i].pos); printf("\n");
		//for(int i = 0; i < n; i++) printf("%d ",after[i].pos); printf("\n");
		sort(before,before+n,cmp);
		for(int i = 0; i < n; i++) order[before[i].id] = i;
		sort(after,after+n,cmp);
		for(int i = 0; i < n-1; i++)
			if(after[i].pos == after[i+1].pos) after[i].dir = after[i+1].dir = 0;
		printf("Case #%d:\n",++cas);
		for(int i = 0; i < n; i++)
		{
			int id = order[i];
			if(after[id].pos > l || after[id].pos < 0) printf("Fell off\n");
			else printf("%d %s\n",after[id].pos,direction[after[id].dir+1]);
		}
		printf("\n");
	}
	return 0;
}



你可能感兴趣的:(Uva解题报告,uva)