zoukankan      html  css  js  c++  java
  • Piotr‘s Ants


    Problem Description

    "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 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 up T 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 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 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

    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


    Solution

    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    const int maxn=10000+5;
    string name[3]={"L","Turning","R"};
    //理解本题目的关键在于蚂蚁无论怎么运动,Ts后其相对位置不会改变 
    struct ant
    {
    	int id;//输入顺序
    	int p;//位置
    	int d;//朝向:-1:左  0:Turning 1:右
    	bool operator < (const ant& a) const
    	{
    		return p<a.p;
    	 } 
    };
    ant before[maxn],after[maxn];
    int order[maxn];
    int main()
    {
    	int T;
    	cin>>T;
    	for(int t=0;t<T;t++)
    	{
    		cout<<"Case #"<<t+1<<":"<<endl;
    		int L,T,n;
    		cin>>L>>T>>n;
    		for(int i=0;i<n;i++)
    		{
    			int p,d;
    			char c;
    			cin>>p>>c;
    			d=(c=='L'?-1:1);
    			before[i]=(ant){i,p,d};
    			after[i]=(ant){0,p+T*d,d};//after的输入id无关紧要 
    		}
    		sort(before,before+n);
    		for(int i=0;i<n;i++)
    		order[before[i].id]=i;//order数组建立实际输入顺序->在木棍上移动实际位置的映射
    		sort(after,after+n);
    		for(int i=0;i<n-1;i++)
    		{
    			if(after[i].p==after[i+1].p)
    			after[i].d=after[i+1].d=0;//修改正在碰撞蚂蚁的方向 
    	    } 
    	    for(int i=0;i<n;i++)
    	    {
    	    	int a=order[i];
    			//order数组第i个对应数值(i代表输入顺序) 
    			//其实就是该只 蚂蚁实际位置(体现在after数组的位置 
    			if(after[a].p<0||after[a].p>L)
    			cout<<"Fell off"<<endl;
    			else
    			cout<<after[a].p<<" "<<name[after[a].d+1]<<endl;
    		}
    		cout<<endl;
    	}
    	
    	return 0;
    }
    

    Python版本 

    def sort_dict(d):
        for j in range(len(d)):
            mindex=j;
            for i in range(j+1,len(d)):
                if(d[i]["pos"]<d[mindex]["pos"]):
                    mindex=i
            temp=d[mindex]
            d[mindex]=d[j]
            d[j]=temp               
        
    name=["L","Turning","R"]
    T=int(input());
    for i in range(T):
        print("Case #"+str(i+1)+":")
        s=input()
        w=s.split()
        length=int(w[0])
        time=int(w[1])
        num=int(w[2])
        before=[]
        after=[]
        for j in range(num):
            temp=dict()
            temp1=dict()
            temp1["index"]=temp["index"]=j
            s1=input()
            w1=s1.split()
            temp["pos"]=int(w1[0])
            d=w1[1]
            if(d=="L"):
                temp1["dire"]=temp["dire"]=-1
            else:
                temp1["dire"]=temp["dire"]=1
            before.append(temp)
            temp1["pos"]=temp["pos"]+time*temp["dire"]
            after.append(temp1)
        sort_dict(before)
        order=list(range(num))
        for i in range(num):
            order[before[i]["index"]]=i
        sort_dict(after)
        for i in range(num-1):
            if(after[i]["pos"]==after[i+1]["pos"]):
                after[i]["dire"]=after[i+1]["dire"]=0
        for i in range(num):
            start=order[i]
            if(after[start]["pos"]<0 or after[start]["pos"]>length):
                print("Fell off
    ")
            else:
                print(str(after[start]["pos"])+" "+str(name[after[start]["dire"]+1])+"
    ")
    

      

  • 相关阅读:
    Docker——搭建SFTP
    PicGo——利用PicGo和GitHub搭建免费图床提供给Typecho使用
    GitHub——如何生成Personal access tokens
    leetcode——两数相加【二】
    每天一道面试题——请实现add(1,2)(3)【二】
    leetcode——两数之和【一】
    每天一道面试题——JavaScript的this指向【一】
    PHP——安装ThinkPHP框架报错
    项目代码 if/else 过多,引起程序猿口吐莲花
    JDK9-JDK14 相关新特性说明及使用
  • 原文地址:https://www.cnblogs.com/xlqtlhx/p/7794072.html
Copyright © 2011-2022 走看看