记得之前做过一个类似的,这里的话蚂蚁碰见掉头和碰见穿过去起始本质上都是一样的,只不过相撞之后蚂蚁对应的ID换了。
有了思路就很好模拟了。
14058049 | 10881 | Piotr‘s Ants | Accepted | C++ | 0.052 | 2014-08-18 03:53:00 |
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<list> #include<cmath> #include<string> #include<sstream> #include<ctime> using namespace std; #define _PI acos(-1.0) #define esp 1e-9 typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> pill; /*=========================================== ===============KinderRiven=================== ===========================================*/ #define MAXD 10000 + 10 struct Ant{ int pos; /*位置*/ int id; /*编号*/ int dir; /*方向*/ friend bool operator < (Ant p, Ant q){ if(p.pos < q.pos) return true; else return false; } }ant[MAXD]; int main(){ int T; scanf("%d",&T); for(int Case = 1; Case <= T; Case ++){ int ord[MAXD]; int ans_pos[MAXD],ans_dir[MAXD]; map<int,int>vis; memset(ans_pos,-1,sizeof(ans_pos)); int L, t , n; scanf("%d%d%d",&L,&t,&n); for(int i = 0 ; i < n ; i++){ char dir[5]; scanf("%d%s",&ant[i].pos,dir); if(dir[0] == 'L') ant[i].dir = -1; else ant[i].dir = 1; ant[i].id = i; } sort(ant ,ant + n); for(int i = 0 ; i < n ; i++){ ant[i].pos = ant[i].pos + ant[i].dir * t; ord[i] = ant[i].id; } sort(ant ,ant + n); for(int i = 0 ; i < n ; i++) ant[i].id = ord[i]; for(int i = 0 ; i < n ; i++){ int id = ant[i].id; if(ant[i].pos >= 0 && ant[i].pos <= L){ vis[ant[i].pos] ++; /*记录改点蚂蚁数量*/ ans_pos[id] = ant[i].pos; ans_dir[id] = ant[i].dir; } } printf("Case #%d:\n",Case); for(int i = 0 ; i < n ; i++){ if(ans_pos[i] == -1) printf("Fell off\n"); else if(vis[ans_pos[i]] > 1) printf("%d Turning\n",ans_pos[i]); else{ printf("%d ",ans_pos[i]); if(ans_dir[i] == 1) printf("R\n"); else printf("L\n"); } } printf("\n"); } return 0; }
【UVA】10881-Piotr's Ants
时间: 2024-10-12 19:23:26