UvaLive 6662 The Last Ant 模拟

链接:http://vjudge.net/problem/viewProblem.action?id=49407

题意:有若干只蚂蚁,给出它们在管子内的坐标和它们的移动方向,如果两只蚂蚁在坐标为整数的位置相遇,那么它们分别调头,否则,两只蚂蚁穿过对方,继续前进。现在问什么时候蚂蚁能全部离开这个管子,并且求出最后一只离开管子的蚂蚁的编号。

是一道纯模拟题,以前觉得这种模拟题的代码量太大,不愿意做,这次A掉以后,感觉神清气爽。

思路:直接模拟即可。

代码:

#include<iostream>
#include<set>
#include<map>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
struct aa
{
    int num;
    int l;
    char dir[2];
} ant[25];
bool cmp(aa a,aa b)
{
    return (a.l<b.l)||(a.l==b.l&&a.dir[0]>b.dir[0]);
};
int main()
{
    int tot,len;
    while(scanf("%d%d",&tot,&len)&&(tot!=0&&len!=0))
    {
        memset(ant,0,sizeof(ant));
        for(int i=0; i<tot; i++)
        {
            scanf("%s%d",ant[i].dir,&ant[i].l);
            ant[i].num=i;
        }
        int second=0,o=0;
        int a=0;
        while(1)
        {
            bool flag =0;
            o=0;
            for(int i=0; i<tot; i++)
            {
                if(ant[i].l==0||ant[i].l==len)
                    o++;
            }
            if(o==tot-1)
            {
                for(int i=0; i<tot; i++)
                {
                    if(ant[i].l>0&&ant[i].l<len)
                    {
                        a=ant[i].num+1;
                        break;
                    }
                }
            }
            else if(o==tot-2)
            {
                int a1=-1,a2=-1;
                int l1=-1,l2=-1;
                bool flag1=0;
                for(int i=0; i<tot; i++)
                {
                    if(ant[i].l>0&&ant[i].l<len&&!flag1)
                    {
                        a1=ant[i].l;
                        l1=ant[i].num;
                        flag1=1;
                    }
                    else if(ant[i].l>0&&ant[i].l<len)
                    {
                        a2=ant[i].l;
                        l2=ant[i].num;
                    }
                }
                if(a1+a2==len)
                {
                    if(a1<a2)
                        a=l1+1;
                    else a=l2+1;
                }
            }
            for(int i=0; i<tot; i++)
            {
                if(ant[i].l>0&&ant[i].l<len)
                {
                    flag = 1;
                    if(ant[i].dir[0]=='R')
                    {
                        if(ant[i+1].dir[0]=='L')
                        {
                            if(ant[i+1].l==ant[i].l+1)
                            {
                                ant[i].l++;
                                ant[i+1].l--;
                                i++;
                            }
                            else if(ant[i+1].l==ant[i].l+2)
                            {
                                ant[i].l++;
                                ant[i+1].l--;
                                i++;
                            }
                            else
                            {
                                ant[i].l++;
                            }
                        }
                        else ant[i].l++;
                    }
                    else ant[i].l--;
                }
            }
            if(flag)
                second++;
            else break;
            sort(ant,ant+tot,cmp);
            for(int i=0; i<tot; i++)
            {
                if(ant[i].l==ant[i+1].l&&ant[i].dir[0]=='R'&&ant[i].l!=0&&ant[i].l!=len)
                {
                    ant[i].dir[0]='L';
                    ant[i+1].dir[0]='R';
                }
            }
        }
        printf("%d %d\n",second,a);
    }
    return 0;
}
时间: 2024-08-27 02:14:23

UvaLive 6662 The Last Ant 模拟的相关文章

UVA 6662 The Last Ant(模拟退火)

A straight tunnel without branches is crowded with busy ants coming and going. Some ants walk left to right and others right to left. All ants walk at a constant speed of 1 cm/s. When two ants meet, they try to pass each other. However, some sections

UvaLive6662 The Last Ant 模拟

UvaLive6662 PDF题目 题意:给出隧道长度L,蚂蚁数量N,各蚂蚁位置Pi.前进方向Di,都为整数(前进方向为L或R),蚂蚁速度为1cm每秒,两蚂蚁若在整数点相遇则都反向,若不在整数点相遇则继续向前.求最后一个走出隧道的蚂蚁的编号.蚂蚁按编号1~n给出,隧道头尾位置为0和L. 题解: 模拟. 当然我们不用模拟蚂蚁实时行走,我们只用算一算蚂蚁什么时候会撞到. 给蚂蚁建个结构体,包括编号.位置.方向,方便以后操作.因为蚂蚁起始位置为整数,也总会在整数点相遇,所以所有数据都可以整数存储. 先

UVALive 6662 TheLastAnt

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 struct xxx 7 { 8 int p,d; 9 }a[22]; 10 int n,l; 11 bool f[22]; 12 int main() 13 { 14 while (~scanf("%d%d",&

uvalive 4254 Processor处理器 (二分模拟+贪心)

 有n个任务,每个任务有ri,di,wi;代表任务的[ri,di]代表可以做这个任务的时间区间,而wi代表这个任务的工作量;现在有有个处理器,如果它的执行速度是s,则完成第i个任务所需时间wi/s;要求算出处理器执行过程中最大速度的最小值 思路很简单二分,但如何模拟是难点,可以模拟处理器每一秒的工作,对于每一秒来说,用优先队列储存当前时间下可以处理的任务,优先处理d小的,如果处理完了,那么处理下一个任务,如果没处理完,时间加一. #include<cstdio> #include<c

UvaLive 4917 Abstract Extract (模拟)

题意: 给定一篇文章, 文章中有段落, 段落中有句子. 句子只会以'!' , '.' , '?' 结尾, 求出每段中含有与他下面同样是该段落中相同单词数最多的句子, 注意, 单词忽略大小写, 重复的单词只算一个. 题目中关键段: A topic sentence for a paragraph is the single sentence in the paragraph that best describes the paragraph's content. For our purposes,

UVAlive 6693 Flow Game(模拟)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4705 思路:若有解,两点连线最小距离=曼哈顿距离+1,则ans=abs(x1-x2)+abs(y1-y2)+abs(x3-x4)+abs(y3-y4)+2.若无解,则两线相交:对于边界,逆时针遍历,若1212或2121则此时无解,其他情况有解.注意所有点在同一行或同

UVALive 4887 Soccer 状压+模拟

题目链接:点击打开链接 题意:n个球队,m场比赛 下面n行表示n个球队的名字. 下面m场比赛表示该场比赛的2个队得分. -1表示我们可以任意填. 这种任意填的比赛场数不超过12场. 求: 胜一场球队得2分,平得1分,败得0分. 求每个球队最好名次与最差名字. 每场只有3个状态,最多只有12场,所以状压一下,3^12个状态. #include <cstdio> #include <algorithm> #include <cstring> #include <str

UESTC 2014 Summer Training #18 Div.2

A.UVALive 6661 题意从1~N中选k个数,和为s的方案数 第一眼搜索,估计错状态量,又去yydp...浪费大量时间 数据很小的,状态数都不会超过2^N...直接dfs就过了 //state二进制表示选取的数 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 200; in

UESTC 2014 Summer Training #19

A.UVALive 6161 去迟了,队友已经开始写了,应该是个水题,贴个队友代码 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #inclu