本文主要是介绍uva10881 ants,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_N 10010
struct ant{int id;//记录输入顺序int p; //每只蚂蚁位置int d; //每只蚂蚁方向,-1左,0正在碰撞,1右;bool operator < (const ant& a) const{ //运算符重载,按p从小到大排序return p < a.p;}}before[MAX_N], after[MAX_N]; //之前与T秒之后
int order[MAX_N]; //最后按输入顺序输出蚂蚁
const char dir[][10] = {"L", "Turing", "R"};
int main(){freopen("in", "r", stdin);int t;
int cnt = 1;
scanf("%d", &t);while(t--){int L, T, N;
printf("Case #%d:\n",cnt++);
scanf("%d%d%d", &L, &T, &N);for(int i = 0;i < N; i++){char c;int p;scanf("%d %c", &p, &c);int d = (c == 'L' ? -1:1);before[i] = (ant){i, p, d}; //蚂蚁初始位置after[i] = (ant){0, p + d * T, d};//T秒之后}sort(before, before + N);//对输入位置排序for(int i = 0;i < N; i++){order[before[i].id] = i;}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++){if(after[order[i]].p < 0 || after[order[i]].p > L) printf("Fell off\n");else printf("%d %s\n", after[order[i]].p, dir[after[order[i]].d+1]);}printf("\n");}return 0;
}
这篇关于uva10881 ants的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!