UvaLive 6662 The Last Ant 模拟
2014-07-19 14:44
323 查看
链接:http://vjudge.net/problem/viewProblem.action?id=49407
题意:有若干只蚂蚁,给出它们在管子内的坐标和它们的移动方向,如果两只蚂蚁在坐标为整数的位置相遇,那么它们分别调头,否则,两只蚂蚁穿过对方,继续前进。现在问什么时候蚂蚁能全部离开这个管子,并且求出最后一只离开管子的蚂蚁的编号。
是一道纯模拟题,以前觉得这种模拟题的代码量太大,不愿意做,这次A掉以后,感觉神清气爽。
思路:直接模拟即可。
代码:
题意:有若干只蚂蚁,给出它们在管子内的坐标和它们的移动方向,如果两只蚂蚁在坐标为整数的位置相遇,那么它们分别调头,否则,两只蚂蚁穿过对方,继续前进。现在问什么时候蚂蚁能全部离开这个管子,并且求出最后一只离开管子的蚂蚁的编号。
是一道纯模拟题,以前觉得这种模拟题的代码量太大,不愿意做,这次A掉以后,感觉神清气爽。
思路:直接模拟即可。
代码:
#include<iostream> #include<set> #include<map> #include<queue> #include<cstring> #include<string> #include<algorithm> #include<cstdio> using namespace std; struct aa { int num; int l; char dir[2]; } ant[25]; bool cmp(aa a,aa b) { return (a.l<b.l)||(a.l==b.l&&a.dir[0]>b.dir[0]); }; int main() { int tot,len; while(scanf("%d%d",&tot,&len)&&(tot!=0&&len!=0)) { memset(ant,0,sizeof(ant)); for(int i=0; i<tot; i++) { scanf("%s%d",ant[i].dir,&ant[i].l); ant[i].num=i; } int second=0,o=0; int a=0; while(1) { bool flag =0; o=0; for(int i=0; i<tot; i++) { if(ant[i].l==0||ant[i].l==len) o++; } if(o==tot-1) { for(int i=0; i<tot; i++) { if(ant[i].l>0&&ant[i].l<len) { a=ant[i].num+1; break; } } } else if(o==tot-2) { int a1=-1,a2=-1; int l1=-1,l2=-1; bool flag1=0; for(int i=0; i<tot; i++) { if(ant[i].l>0&&ant[i].l<len&&!flag1) { a1=ant[i].l; l1=ant[i].num; flag1=1; } else if(ant[i].l>0&&ant[i].l<len) { a2=ant[i].l; l2=ant[i].num; } } if(a1+a2==len) { if(a1<a2) a=l1+1; else a=l2+1; } } for(int i=0; i<tot; i++) { if(ant[i].l>0&&ant[i].l<len) { flag = 1; if(ant[i].dir[0]=='R') { if(ant[i+1].dir[0]=='L') { if(ant[i+1].l==ant[i].l+1) { ant[i].l++; ant[i+1].l--; i++; } else if(ant[i+1].l==ant[i].l+2) { ant[i].l++; ant[i+1].l--; i++; } else { ant[i].l++; } } else ant[i].l++; } else ant[i].l--; } } if(flag) second++; else break; sort(ant,ant+tot,cmp); for(int i=0; i<tot; i++) { if(ant[i].l==ant[i+1].l&&ant[i].dir[0]=='R'&&ant[i].l!=0&&ant[i].l!=len) { ant[i].dir[0]='L'; ant[i+1].dir[0]='R'; } } } printf("%d %d\n",second,a); } return 0; }
相关文章推荐
- UvaLive6662 The Last Ant 模拟
- UVALive6662 The Last Ant
- UVA 6662 The Last Ant(模拟退火)
- LA 6662 —— The Last Ant(模拟)
- UVALive 5695 -The Last Puzzle -区间dp
- UVALive 6581 What does the fox say? (模拟)
- The Same Game UVA 758/Uvalive 5423 大模拟 求最大联通块并清除
- POJ1218 HDU1337 ZOJ1350 UVALive2557 THE DRUNK JAILER【模拟】
- UVA694 UVALive5466 The Collatz Sequence【模拟】
- UVALive 6680 Join the Conversation (模拟)
- UVALive 7146 Defeat the Enemy(模拟)
- UVALive 6465 Islands in the Data Stream 暴力模拟
- UVa 573 The Snail (模拟)
- UVALive 3637 The Bookcase
- UVa 131 The Psychic Poker Player (枚举&模拟好题)
- UVALive 3942 Remember the Word(trie + dp)
- UVALive 3181 Fixing the Great Wall
- UVALive 2197 Paint the Roads(费用流)
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
- uva 573 The Snail(模拟)