当前位置: 代码迷 >> 综合 >> UVA 10881 - Piotr‘s Ants
  详细解决方案

UVA 10881 - Piotr‘s Ants

热度:17   发布时间:2023-12-24 11:23:52.0

题目大意:一根L长的木板上有n只蚂蚁,每只蚂蚁初始有一个朝向,当两只蚂蚁相遇,会掉头走。求走了T步以后所有蚂蚁的状况。

解题思路:每只蚂蚁相遇后,相当于对穿过,只是对穿过的蚂蚁不是原来那只。但整个图所有蚂蚁的位置就是看作对穿过求得的。只是最初蚂蚁的相对位置与最后蚂蚁的相对位置的一样的,因为相遇就掉头,所以不会改变蚂蚁的相对位置。就是要求初始相对位置。。。

ac代码:

#include <iostream>
#include <algorithm>
using namespace std;
int n, L, T, num, order[10005], temp, cnt=1;
char c;
struct node
{int pos;int d;int init;
};
bool compare(node a, node b)
{
return a.pos < b.pos;
}
int main()
{node before[10005], after[10005];scanf("%d", &n);while (n--){scanf("%d%d%d", &L, &T, &num);for (int i=0; i<num; i++){scanf("%d %c", &before[i].pos, &c);before[i].d = (c == 'L'?-1:1);	before[i].init = i;after[i].d = before[i].d;after[i].pos = before[i].pos + before[i].d * T;}sort(before, before+num, compare);for (int i=0; i<num; i++)order[ before[i].init ] = i;sort(after, after+num, compare);for (int i=0; i<num-1; i++)if (after[i].pos == after[i+1].pos)after[i].d = after[i+1].d = 0;printf("Case #%d:\n", cnt++);for (int i=0; i<num; i++){temp = order[i];if (after[temp].pos > L || after[temp].pos < 0)printf("Fell off\n");else if (!after[temp].d)printf("%d Turning\n", after[temp].pos);else if (after[temp].d == 1)printf("%d R\n", after[temp].pos);else printf("%d L\n", after[temp].pos);}printf("\n");}
return 0;
}