UVALive6662 The Last Ant
2014-07-19 16:11
375 查看
题目链接
https://icpcarchive.ecs.baylor.edu/external/66/6662.pdf
题目意思是给你两个数n、l,n是蚂蚁的个数,l是木棍的长度
接下来n行第一个字母表示蚂蚁初始的方向第二个数字表示蚂蚁的初始位置,若是蚂蚁在相邻的两格相向而行,则通过,若是蚂蚁在相邻的三个相向而行,则碰头转向。
![](http://img.blog.csdn.net/20140719160942187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDQ2ODU1Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
题目意思最开始没有理解清楚,模拟算法写。
https://icpcarchive.ecs.baylor.edu/external/66/6662.pdf
题目意思是给你两个数n、l,n是蚂蚁的个数,l是木棍的长度
接下来n行第一个字母表示蚂蚁初始的方向第二个数字表示蚂蚁的初始位置,若是蚂蚁在相邻的两格相向而行,则通过,若是蚂蚁在相邻的三个相向而行,则碰头转向。
题目意思最开始没有理解清楚,模拟算法写。
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> #define PI acos(-1.0) #define MAXN 100005 #define eps 1e-7 #define INF 0x7FFFFFFF #define ff sqrt(5.0) typedef long long ll; using namespace std; struct node { int num,pos; char dir; } a[25]; bool cmp(node x,node y) { return x.pos<y.pos; } int main() { int l,n,i; int cnt,ans; while(scanf("%d%d",&n,&l)!=EOF) { if(n==0&&l==0) break; cnt = ans = 0; for(i=0; i<n; i++) { getchar(); cin>>a[i].dir>>a[i].pos; a[i].num = i+1; } sort(a,a+n,cmp); node ant[2]; int flag; while(1) { cnt++; //时间 flag = 0; for(i=0; i<n; i++) { if(a[i].pos<=0||a[i].pos>=l) continue; if(a[i].dir=='R') a[i].pos++; else a[i].pos--; if(a[i].pos==0||a[i].pos==l) //0向左出去,1向右出去。 { ant[flag] = a[i]; flag++; } } sort(a,a+n,cmp); for(i=0; i<n-1; i++) { if(a[i].pos==a[i+1].pos) { if(a[i].dir=='L') a[i].dir = 'R'; else a[i].dir = 'L'; if(a[i+1].dir=='L') a[i+1].dir = 'R'; else a[i+1].dir = 'L'; } } int ccc = 0; for(i=0; i<n; i++) { if(a[i].pos<=0||a[i].pos>=l) ccc++; } if(ccc==n) break; } if(flag==1) ans = ant[0].num; else { if(ant[0].dir=='L') ans = ant[0].num; else ans = ant[1].num; } cout<<cnt<<" "<<ans<<endl; } return 0; }
相关文章推荐
- UvaLive 6662 The Last Ant 模拟
- UvaLive6662 The Last Ant 模拟
- UVA 6662 The Last Ant(模拟退火)
- LA 6662 —— The Last Ant(模拟)
- UVALive 5695 -The Last Puzzle -区间dp
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
- UVALive - 3942 Remember the Word
- UVALive2701 UVA1189 POJ1426 ZOJ1530 Find The Multiple【BFS】
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive 7146-贪心&数据结构stl-Defeat the Enemy
- 【Uvalive3942】Remember the Word——字典树+dp
- UVALive 7146 Defeat the Enemy(模拟)
- LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
- UVALive 5033 I'm Telling the Truth 二分图匹配
- UVAlive11324 The Largest Clique(scc+dp)
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- UVALive - 3523 Knights of the Round Table(无向图的双连通分量)
- 【 UVALive - 2197】Paint the Roads(上下界费用流)
- UVALive 5984 Save the Students!(几何)
- UVALive 6177|HDU 4489|The King's Ups and Downs|动态规划