uva 10881

分类:排序与坐标映射

题意:给定木棒上蚂蚁的位置,木棒的长度,运动方向和速度,以及时间,确定之后蚂蚁的状态。蚂蚁碰撞同时改变方向,速度不变

输入:组数, 木棒长度, 时间T, 蚂蚁个数n, 以下n行是蚂蚁的位置(距离左端),以及运动方向

输出:按照输入顺序,每只蚂蚁的状态,掉下则为“Fell off”,正在碰撞为“Turning”, 其余为位置和方向

 

解法:

        核心:关注最终结果,“对穿”,见《训练指南》

        关键:如何“按照输入顺序”输出“最终结果”

                对每只蚂蚁记录其inputOrder为输入顺序,再按照初始位置排序,注意这个排序的下标即为最终终态的蚂蚁下标,记录为finalPos

                模拟运动过程,构造终态蚂蚁数组,以inputOrder为顺序,输出终态数组(不是按照数组下标,只是遍历)

        结构设计的一般般。。导致记录方向和位置的时候比较乱,出错了几次

 

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定义
const int  INF = 990000000;
const int MAXN = 10010;
const int maxn = MAXN;
///全局变量 和 函数
int cases;
int Cases;
int L, T, n;
struct ant
{
    int pos, inputOrder, finalOrder;
    char dir;
    bool operator < (const ant& T) const
    {
        return pos < T.pos;
    }
};

ant ants[maxn];
ant ants_[maxn];
bool cmp(const ant& a1, const ant& a2)
{
    return a1.inputOrder < a2.inputOrder;
}
int main()
{
    ///变量定义
    scanf("%d", &cases);
    Cases = 1;
    while (cases--)
    {
        scanf("%d %d %d", &L, &T, &n);
        for (int i = 0; i < n; i++)
        {
            ant curAnt;
            scanf("%d %c", &curAnt.pos, &curAnt.dir);
            curAnt.inputOrder = i;
            ants[i] = curAnt;
            ants_[i] = curAnt;
        }
        sort(ants, ants + n);
        sort(ants_, ants_ + n);
        //确定每只蚂蚁的最终位置
        for (int i = 0; i < n; i++)
        {
            ants[i].finalOrder = i;
            ants_[i].finalOrder = i;
        }
        //进行对穿
        for (int i = 0; i < n; i++)
        {
            if (ants[i].dir == 'L')
                ants[i].pos -= T;
            else
                ants[i].pos += T;        
        }
        //排序确定最终的蚂蚁位置,下标依据上面的终态
        sort(ants, ants + n);
        int i = 0;
        while (i < n) //有没有掉下去!
        {
            if (ants[i].pos < 0 || ants[i].pos > L)
                ants[i].dir = 'F';
            i++;
        }
        for (int i = 0; i < n - 1; i++)
        {
            if ((ants[i].pos == ants[i + 1].pos) && (ants[i].pos >= 0) && (ants[i].pos <= L)) //如果没有掉下去且碰撞了!
            {
                ants[i].dir = 'T';
                ants[i + 1].dir = 'T';
            }
        }
        //排序建立输入顺序与输出终态下标的映射
        sort(ants_, ants_ + n, cmp);
        printf("Case #%d:\n", Cases++);
        for (int i = 0; i < n; i++)
        {
            int tempPos = ants_[i].finalOrder;
            if (ants[tempPos].dir == 'F')
                printf("Fell off\n");
            else if (ants[tempPos].dir == 'T')
            {
                printf("%d ", ants[tempPos].pos);
                printf("Turning\n");
            }
            else
                printf("%d %c\n", ants[tempPos].pos, ants[tempPos].dir);
        }
        printf("\n");
    }

    ///结束
    return 0;
}

 

  

posted on 2013-09-17 16:39  小书包_Ray  阅读(174)  评论(0编辑  收藏  举报

导航