zoukankan      html  css  js  c++  java
  • 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 horizontal poleL cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end upT seconds from now.

    Input
    The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing 3 integers:L , T and n (0 <= n <= 10000) . The next n lines give the locations of then 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 byn lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their 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 Input Sample 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


    题意:

    一根木棍上有若干只蚂蚁,他们的爬行速度都为1m/s,0时刻的初始位置(距离木棍左端的距离)和爬行方向已知,当两只蚂蚁相遇时,会立刻掉头朝反方向爬去。问经过t秒之后,每只蚂蚁的位置和朝向。

    思路1:

    两只蚂蚁相遇后,因为是立刻掉头爬行,所以,两只蚂蚁原先会到达的地方仍然会有蚂蚁,就好像蚂蚁穿过而行一样,只不过是两只蚂蚁交换了位置和爬行方向。所以只需弄清楚蚂蚁之间是如何交换的即可。

    代码1:

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    #define MAXN 10000
    
    struct Ant
    {
    	int id;		//输入次序编号
    	int origin; //起点位置
    	int dir;	//朝向,-1代表向左,0代表转向中,1代表向右
    	int dest;	//终点位置
    	bool operator < (const Ant &a) const
    	{
    		return origin < a.origin;
    	}
    }ants[MAXN+5];
    
    char dirName[][10] = {"L", "Turning", "R"};
    
    int order[MAXN+5];
    
    void Swap(int i, int j) //除了id号,交换蚂蚁的所有信息
    {
    	Ant tmp = ants[i];
    	ants[i] = ants[j];
    	ants[i].id = tmp.id;
    	tmp.id = ants[j].id;
    	ants[j] = tmp;
    }
    
    int main(void)
    {
    	int ncases;
    	cin >> ncases;
    
    	int caseID = 1;
    	while (ncases-- != 0)
    	{
    		int l, t, n;
    		cin >> l >> t >> n;
    
    		int i, j;
    		for (i = 0; i < n; i++)
    		{
    			char dir;
    			cin >> ants[i].origin >> dir;
    
    			ants[i].id = i;
    			ants[i].dir = (dir == 'L' ? -1 : 1);
    			ants[i].dest = ants[i].origin + ants[i].dir*t;
    		}
    
    		sort(ants, ants+n); //按照起点位置从小到大排序
    
    		bool finish = false;
    		while (!finish)
    		{
    			finish = true;
    			for (i = 0, j = 1; j < n; i++, j++)
    			{
    				if (ants[i].dir == 1 && ants[j].dir == -1 //只有相向而行的蚂蚁才可能交换信息
    					&& ants[i].origin < ants[j].origin && ants[i].dest > ants[j].dest) //交换位置和方向的条件
    				{
    					Swap(i, j);
    					finish = false;
    				}
    			}
    		}
    		//更改正在相遇中的蚂蚁朝向
    		for (i = 0, j = 1; j < n; i++, j++)
    		{
    			if (ants[i].dest == ants[j].dest)
    			{
    				ants[i].dir = ants[j].dir = 0;
    			}
    		}
    		//记录输出次序
    		for (i = 0; i < n; i++)
    		{
    			order[ants[i].id] = i;
    		}
    		
    		cout << "Case #" << caseID++ << ":" << endl;
    		for (i = 0; i < n; i++)
    		{
    			int j = order[i];
    			if (ants[j].dest < 0 || ants[j].dest > l)
    			{
    				cout << "Fell off" << endl;
    			}
    			else
    			{
    				cout << ants[j].dest << ' ' << dirName[ants[j].dir+1] << endl;
    			}
    		}
    		cout << endl;
    	}
    	return 0;
    }


    思路2:

    因为蚂蚁相遇后立刻掉头,所以,经过 t 时间后,蚂蚁的位置次序保持不变。蚂蚁的输入次序与位置次序不相同,所以需要先建立起输入次序与起始位置次序的对应关系,又因为 t 时间后的位置次序与起始位置次序相同,也就得到了输入次序与结束位置次序的对应关系。

    代码2:

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    #define MAXN 10000
    
    struct Ant
    {
    	int id;		//输入次序编号
    	int pos;	//位置
    	int dir;	//朝向,-1代表向左,0代表转向中,1代表向右
    	bool operator < (const Ant &a) const
    	{
    		return pos < a.pos;
    	}
    }before[MAXN+5], after[MAXN+5];
    
    char dirName[][10] = {"L", "Turning", "R"};
    
    int main(void)
    {
    	int ncases;
    	cin >> ncases;
    
    	int caseID = 1;
    	while (ncases-- != 0)
    	{
    		int l, t, n;
    		cin >> l >> t >> n;
    
    		int i;
    		for (i = 0; i < n; i++)
    		{
    			int p;
    			char dir;
    			cin >> p >> dir;
    			int d = (dir == 'L' ? -1 : 1);
    
    			before[i].id = i;
    			before[i].pos = p;
    			before[i].dir = d;
    			after[i].pos = p + t*d;
    			after[i].dir = d;
    		}
    
    		sort(before, before+n);
    		int order[MAXN+5];
    		for (i = 0; i < n; i++)
    		{
    			order[before[i].id] = i;
    		}
    
    		sort(after, after+n);
    		//调整正在相遇的蚂蚁的朝向
    		for (i = 0; i < n-1; i++)
    		{
    			if (after[i].pos == after[i+1].pos)
    			{
    				after[i].dir = after[i+1].dir = 0;
    			}
    		}
    
    		cout << "Case #" << caseID++ << ":" << endl;
    		for (i = 0; i < n; i++)
    		{
    			int j = order[i];
    			if (after[j].pos < 0 || after[j].pos > l)
    			{
    				cout << "Fell off" << endl;
    			}
    			else
    			{
    				cout << after[j].pos << ' ' << dirName[after[j].dir+1] << endl;
    			}
    		}
    		cout << endl;
    	}
    	return 0;
    }

    测试数据:

    10 2 4
    1 R
    3 R
    5 L
    7 L


    10 4 4
    1 R
    4 L
    5 L
    7 L


  • 相关阅读:
    linux常用命令
    linux简介
    vmware+CentOs 6.9的安装步骤
    Android layer type与WebView白屏-第一次加载webview很慢
    Android View框架的measure机制
    maven的facet错误解决方法
    oracle jdbc驱动发布nexus
    dubbox2.8.4编译发布到私服(nexus)
    一致性哈希 与 普通哈希对比
    1对多业务,数据库水平切分架构一次搞定 | 架构师之路
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3402439.html
Copyright © 2011-2022 走看看