uva 10081

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822

题意:在一根棍子上,有n个?,然后每个?都有一个初始的位置,以及头的朝向,然后每一秒移动一个单位的距离,两只蚂蚁互撞后互换反向,问t秒后,它们各自所在的位置。

思路:明确一个事情,就是说对于每一个?,它所在的位置的顺序是一定的,只不过后来的位置每个都改变了,因为是互换方向,而不是穿透而过。

所以我们需要是找出它的初始的位置顺序,然后每个记录一下,最后T秒后的位置,按这个排序,然后找到对应位置输出就可以了

注意0这个位置也还是在棍子上

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct Node{
 7     int x;
 8     char r;
 9     int loc;
10 }node[10005];
11 
12 bool cmp(const Node &a,const Node &b)
13 {
14     return a.x<b.x;
15 }
16 
17 
18 int main()
19 {
20    // freopen("ina.txt","w",stdout);
21    // freopen("in.txt","r",stdin);
22     int l,t,n,T;
23     int loc[10005];
24     scanf("%d",&T);
25     for(int cnt = 1;cnt<=T;cnt++)
26     {
27         scanf("%d%d%d",&l,&t,&n);
28        // printf("%d %d %d\n",l,t,n);
29      //   printf("password\n");
30         for(int i = 1;i<=n;i++)
31         {
32             scanf("%d %c",&node[i].x,&node[i].r);
33           //  printf("%d %c\n",node[i].x,node[i].r);
34             node[i].loc = i;
35         }
36         node[0].x = node[n+1].x = -5;
37         stable_sort(node+1,node+n+1,cmp);
38         for(int i = 1;i<=n;i++)
39         {
40             loc[node[i].loc] = i;
41             if(node[i].r=='R')
42                 node[i].x+=t;
43             else
44                 node[i].x-=t;
45         }
46         stable_sort(node+1,node+n+1,cmp);
47         printf("Case #%d:\n",cnt);
48         for(int i =1;i<=n;i++)
49         {
50             if(node[loc[i]].x<0||node[loc[i]].x>l)
51                 printf("Fell off\n");
52             else {
53                 printf("%d ",node[loc[i]].x);
54                 if(node[loc[i]-1].x==node[loc[i]].x||node[loc[i]+1].x==node[loc[i]].x)
55                     printf("Turning\n");
56                 else
57                     printf("%c\n",node[loc[i]].r);
58             }
59         }
60         printf("\n");
61     }
62     return 0;
63 }

 

转载于:https://www.cnblogs.com/Tree-dream/p/7349168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值