Piotr's Ants UVA - 10881 蚂蚁

版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。 https://blog.csdn.net/qq_42835910/article/details/89671128

题目链接 

 \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 horizontal pole L cm long. Each ant is facingeither left or right and walks at 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 eachof the ants starts and which direction it is facing and wants to calculate where the ants will end up Tseconds from now.

分析:当蚂蚁因碰撞掉头时,从总体上来看实际是对穿而过(因为两者都反向),则独立计算每只蚂蚁T时间后的位置就是全体蚂蚁T时间后的位置。这样计算之后,就还需要找到,单只蚂蚁到底在哪个位置(因为只是整体看待是对穿)。而所有蚂蚁的相对顺序是保持不变的。

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10000 + 5;
int order[N]; // 记录初始态前序号为id的蚂蚁的排序位置order[id] ; 
struct Ant{
	int id, pos;
	char dir;
	bool operator < (const Ant &a) const {
		return pos < a.pos;
	}
}ant[N];

int main(int argc, char** argv) {
	int K;
	scanf("%d",&K);
	for(int kase = 1; kase <= K; kase++){
		int L , T, n;
		scanf("%d%d%d",&L,&T,&n);
		for(int i = 0; i < n; i++){
			scanf("%d %c",&ant[i].pos,&ant[i].dir);
			ant[i].id = i;
		}
		sort(ant, ant+n);
		for(int i = 0; i < n; i++) //记录order 
			order[ant[i].id] = i;
		for(int i = 0; i < n; i++) //计算蚂蚁位值 
			if(ant[i].dir == 'R')
				ant[i].pos += T;
			else
				ant[i].pos -= T;
		sort(ant, ant+n);
		for(int i = 1; i < n; i++){
			if(ant[i].pos == ant[i-1].pos)//标记蚂蚁正在转向。 
				ant[i].dir = ant[i-1].dir = '\0';
		}
		printf("Case #%d:\n",kase);
		for(int i = 0; i < n; i++){
			int ord = order[i];
			if(ant[ord].pos < 0 || ant[ord].pos > L){
				printf("Fell off\n");
				continue;
			}			
			printf("%d ",ant[ord].pos,ant[ord].dir);
			if(ant[ord].dir == '\0') 
				printf("Turning\n");
			else 
				printf("%c\n",ant[ord].dir);			
		}
		printf("\n");
	}
	return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10000 + 5;
int order[N]; // 记录初始态前序号为id的蚂蚁的排序位置order[id] ; 
char dirName[][10] = {"L","Turning","R"};
struct Ant{
	int id, p , d;
	bool operator < (const Ant &a) const {
		return p < a.p;
	}
}ant[N];

int main(int argc, char** argv) {
	int K;
	scanf("%d",&K);
	for(int kase = 1; kase <= K; kase++){
		int L , T, n;
		scanf("%d%d%d",&L,&T,&n);
		for(int i = 0; i < n; i++){
			char c;
			scanf("%d %c",&ant[i].p, &c);
			ant[i].id = i;
			ant[i].d = c == 'L' ? -1 : 1; //L为-1,R为1. 
		}
		sort(ant, ant+n);
		for(int i = 0; i < n; i++) //记录order 
			order[ant[i].id] = i;
		for(int i = 0; i < n; i++) //计算蚂蚁位值 
				ant[i].p += ant[i].d*T;
		sort(ant, ant+n);
		for(int i = 1; i < n; i++)
			if(ant[i].p == ant[i-1].p)//标记蚂蚁正在转向。 
				ant[i].d = ant[i-1].d = 0;
		printf("Case #%d:\n",kase);
		for(int i = 0; i < n; i++){
			int ord = order[i];
			if(ant[ord].p < 0 || ant[ord].p > L)
				printf("Fell off\n");
			else			
				printf("%d %s\n",ant[ord].p, dirName[ant[ord].d+1]);		
		}
		printf("\n");
	}
	return 0;
}

Input
The rst line of input gives the number of cases, N. N test cases follow. Each one starts with a linecontaining 3 integers: L , T and n (0 n 10000). The next n lines give the locations of the n ants(measured in 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 by n lines describing the locationsand directions of the n ants in the same format and order as in the input. If two or more ants are atthe same location, print `Turning' instead of `L' or `R' for their direction. If an ant falls off the polebefore T seconds, print `Fell off' for that ant. Print an empty line after each test case.
Sample Input
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Sample Output
Case #1:
2 Turning
6 R
2 Turning
Fell off
Case #2:
3 L
6 R
10 R

猜你喜欢

转载自blog.csdn.net/qq_42835910/article/details/89671128