• UVA 10881 Piotr's Ants


    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 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, NN 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 Tseconds, 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

    题目大意:

      多只蚂蚁,给定初始位置,初始方向(左或右),然后朝给定的方向走,超过给定的长度L时就会掉下木杆,当两只蚂蚁碰头时,瞬间掉头,让我们计算时间T秒后,各蚂蚁的位置以及方向;

    解题思路:

      由于蚂蚁碰头时,是瞬间掉头,于是我们从远处看就像对穿而过,我们就可以处理为对穿而过;

      另外蚂蚁的相对位置是不变的,所以我们可以存储蚂蚁们的相对位置,也就是说如果蚂蚁1在蚂蚁2之后,则不管出现什么情况蚂蚁1在一直会在蚂蚁2之后;

    解题代码:

    View Code
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 using namespace std;
     5 #define Max 10010
     6 
     7 const char dir_name[][10] = {"L", "Turning", "R"};//处理最后的状态 
     8 int order[Max];//记录蚂蚁的相对位置 
     9 struct Ant
    10 {
    11     int id;//蚂蚁的输入顺序 
    12     int xx;//位置 
    13     int chao;//朝向 
    14 }before[Max], after[Max];//before数组存储蚂蚁们的开始状态,after数组存储蚂蚁们的最后状态 
    15 
    16 bool cmp(const Ant &a, const Ant &b)
    17 {
    18     return a.xx < b.xx;
    19 }
    20 
    21 int main()
    22 {
    23     int total;
    24     scanf ("%d", &total);
    25     for (int i = 0; i < total; i ++)
    26     {
    27         int l, t, n;
    28         int x;
    29         char chf;
    30         scanf ("%d%d%d", &l, &t, &n); 
    31         for (int j = 0; j < n; j ++)
    32         {
    33             scanf ("%d %c", &x, &chf);
    34             int temp = (chf == 'R') ? 1 : -1;
    35             before[j] = (Ant){j, x, temp};
    36             after[j] = (Ant){0, x + temp * t, temp};//最后状态id未知,所以为0 
    37         }
    38         printf ("Case #%d:\n", i+1);
    39         sort(before, before + n, cmp);
    40         for (int j = 0; j < n; j ++)
    41         {
    42             order[before[j].id] = j;//存储蚂蚁们的相对位置 
    43         }
    44         sort(after, after + n, cmp);
    45         for (int j = 0; j < n-1; j ++)
    46         {
    47             if (after[j].xx == after[j+1].xx)
    48                 after[j].chao = after[j+1].chao = 0;
    49         }
    50         for (int j = 0; j < n; j ++) 
    51         {
    52             int k = order[j];//按相对位置顺序输出各蚂蚁的状态 
    53             if (after[k].xx > l || after[k].xx < 0)//蚂蚁掉下木杆的情况 
    54                 printf ("Fell off\n");
    55             else
    56                 printf ("%d %s\n", after[k].xx, dir_name[after[k].chao+1]);
    57         }
    58         printf ("\n");
    59     }
    60 }
  • 相关阅读:
    ryu 下发流表配置
    openstack kolla 部署---不同的节点采用不同的物理接口
    kolla 安装
    Ubuntu add-apt-repository: command not found
    大前端发展趋势
    这些优化技巧可以避免我们在 JS 中过多的使用 IF 语句
    asap异步执行实现原理
    为什么 JS 对象内部属性遍历的顺序乱了
    你可能忽略的 async/await 问题
    深入浅出JS原型链
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/2991850.html
Copyright © 2020-2023  润新知