百度面试题——蚂蚁爬杆
2012-05-02 16:00
169 查看
有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
第一感觉这种和实际挂边的算法题都比较难,特别是它又被称做“灵魂算法”。(名字倒是挺玄乎的,呵呵)不过看了一遍题目后,感觉远没有想的那么难,(可能之前做”ACM”的题都留下阴影了,-_-)。好了言规正传,下面说一下我的解答思路:(欢迎交流哦P:)
1.最小时间
这道题的最小时间还是非常好定的,27cm的木杆,中线处为13.5cm,也就是说第3、7、11厘米处的蚂蚁朝着0点处走,第17、23厘米处的蚂蚁朝着终点(27厘米处)走,此时5只蚂蚁用的时间即为最小时间为11秒。
2.最长时间
最长时间确实是一个比较有趣的问题,在刚开始起笔写这篇文字的时候,我一直在琢磨一种说法“动量守恒”,不过刚才突然间似乎明白了这道题之所以被叫做“灵魂算法”的缘由了,这里我更希望说说用“灵魂算法”解决最大时间的思想,权当与大家讨论了,如有说得不对的地方,请您加入到讨论中来,我们一起交流。好,废话说了这么多,下面我说下我的理解。
“灵魂算法”即是指当两只蚂蚁碰面后,理论上它们应该立即掉头反向而行,但此时我们可以认为它们是可以穿过对方的“灵魂”,碰面之后仍会坚持原来的方向行走。(要知道,对我们来说题目中两只蚂蚁并没有什么不同之处,这是算法思想的关键,理解了这里我想接下来计算最大时间就不成问题了)。既然蚂蚁可以穿越对方而行走,那么用时最长的就是行走路线最长的那只蚂蚁喽,回头看看情景中给出条件,即可得出结果:
第一只:27-3=24/1=24(s)
第二只:27-7=20/1=20(s)
第三只:27-11=16/1=16(s)
第四只:17-0=10/1=17(s)
第五只:23-0=23/1=23(s)
最长时间为24s
程序如下
orient(0-left;1-right)
orient:00000 cost time:23s
orient:00001 cost time:17s
orient:00010 cost time:23s
orient:00011 cost time:11s
orient:00100 cost time:23s
orient:00101 cost time:17s
orient:00110 cost time:23s
orient:00111 cost time:16s
orient:01000 cost time:23s
orient:01001 cost time:20s
orient:01010 cost time:23s
orient:01011 cost time:20s
orient:01100 cost time:23s
orient:01101 cost time:20s
orient:01110 cost time:23s
orient:01111 cost time:20s
orient:10000 cost time:24s
orient:10001 cost time:24s
orient:10010 cost time:24s
orient:10011 cost time:24s
orient:10100 cost time:24s
orient:10101 cost time:24s
orient:10110 cost time:24s
orient:10111 cost time:24s
orient:11000 cost time:24s
orient:11001 cost time:24s
orient:11010 cost time:24s
orient:11011 cost time:24s
orient:11100 cost time:24s
orient:11101 cost time:24s
orient:11110 cost time:24s
orient:11111 cost time:24s
第一感觉这种和实际挂边的算法题都比较难,特别是它又被称做“灵魂算法”。(名字倒是挺玄乎的,呵呵)不过看了一遍题目后,感觉远没有想的那么难,(可能之前做”ACM”的题都留下阴影了,-_-)。好了言规正传,下面说一下我的解答思路:(欢迎交流哦P:)
1.最小时间
这道题的最小时间还是非常好定的,27cm的木杆,中线处为13.5cm,也就是说第3、7、11厘米处的蚂蚁朝着0点处走,第17、23厘米处的蚂蚁朝着终点(27厘米处)走,此时5只蚂蚁用的时间即为最小时间为11秒。
2.最长时间
最长时间确实是一个比较有趣的问题,在刚开始起笔写这篇文字的时候,我一直在琢磨一种说法“动量守恒”,不过刚才突然间似乎明白了这道题之所以被叫做“灵魂算法”的缘由了,这里我更希望说说用“灵魂算法”解决最大时间的思想,权当与大家讨论了,如有说得不对的地方,请您加入到讨论中来,我们一起交流。好,废话说了这么多,下面我说下我的理解。
“灵魂算法”即是指当两只蚂蚁碰面后,理论上它们应该立即掉头反向而行,但此时我们可以认为它们是可以穿过对方的“灵魂”,碰面之后仍会坚持原来的方向行走。(要知道,对我们来说题目中两只蚂蚁并没有什么不同之处,这是算法思想的关键,理解了这里我想接下来计算最大时间就不成问题了)。既然蚂蚁可以穿越对方而行走,那么用时最长的就是行走路线最长的那只蚂蚁喽,回头看看情景中给出条件,即可得出结果:
第一只:27-3=24/1=24(s)
第二只:27-7=20/1=20(s)
第三只:27-11=16/1=16(s)
第四只:17-0=10/1=17(s)
第五只:23-0=23/1=23(s)
最长时间为24s
程序如下
#include<iostream> using namespace std; class ant { public: bool orient; //0-left;1-right; short pos; //0-27; bool onbar; //0-off;1-on; void meet()//--------------------相遇反向 { orient=orient>0?0:1; } void move()//--------------------未到端点,按方向移动,到则标记 { if(orient==0) pos--; else pos++; if(pos==0||pos==27)onbar=0; } }; int main() { ant a1,a2,a3,a4,a5; cout<<"orient(0-left;1-right)"<<endl; for(int i1=0;i1<=1;i1++) for(int i2=0;i2<=1;i2++) for(int i3=0;i3<=1;i3++) for(int i4=0;i4<=1;i4++) for(int i5=0;i5<=1;i5++) {//--------------------初始化五只蚂蚁 a1.orient=i1;a2.orient=i2;a3.orient=i3;a4.orient=i4;a5.orient=i5; a1.pos=3;a2.pos=7;a3.pos=11;a4.pos=17;a5.pos=23; a1.onbar=1;a2.onbar=1;a3.onbar=1;a4.onbar=1;a5.onbar=1; cout<<"orient:"<<a1.orient<<a2.orient<<a3.orient<<a4.orient<<a5.orient<<" "; short time=0; while(a1.onbar||a2.onbar||a3.onbar||a4.onbar||a5.onbar) {//--------------------蚂蚁在杆上,爬一秒,判断相遇 if(a1.onbar) a1.move(); if(a2.onbar) a2.move(); if(a3.onbar) a3.move(); if(a4.onbar) a4.move(); if(a5.onbar) a5.move(); if(a1.pos+1==a2.pos&&a1.onbar&&a2.onbar){a1.meet();a2.meet();} if(a2.pos+1==a3.pos&&a2.onbar&&a3.onbar){a2.meet();a3.meet();} if(a3.pos+1==a4.pos&&a3.onbar&&a4.onbar){a3.meet();a4.meet();} if(a4.pos+1==a5.pos&&a4.onbar&&a5.onbar){a4.meet();a5.meet();} time++; } cout<<"cost time:"<<time<<"s"<<endl; } }结果:
orient(0-left;1-right)
orient:00000 cost time:23s
orient:00001 cost time:17s
orient:00010 cost time:23s
orient:00011 cost time:11s
orient:00100 cost time:23s
orient:00101 cost time:17s
orient:00110 cost time:23s
orient:00111 cost time:16s
orient:01000 cost time:23s
orient:01001 cost time:20s
orient:01010 cost time:23s
orient:01011 cost time:20s
orient:01100 cost time:23s
orient:01101 cost time:20s
orient:01110 cost time:23s
orient:01111 cost time:20s
orient:10000 cost time:24s
orient:10001 cost time:24s
orient:10010 cost time:24s
orient:10011 cost time:24s
orient:10100 cost time:24s
orient:10101 cost time:24s
orient:10110 cost time:24s
orient:10111 cost time:24s
orient:11000 cost time:24s
orient:11001 cost time:24s
orient:11010 cost time:24s
orient:11011 cost time:24s
orient:11100 cost time:24s
orient:11101 cost time:24s
orient:11110 cost time:24s
orient:11111 cost time:24s
相关文章推荐
- 百度面试题——蚂蚁爬杆答案
- 百度面试题一道2(著名的蚂蚁问题)
- 百度面试题蚂蚁爬竹竿
- 百度笔度题-----蚂蚁爬杆问题
- 百度面试题蚂蚁(C#)
- 百度面试题蚂蚁爬竹竿
- 百度面试题-蚂蚁过木板-完整解答
- 面试题:蚂蚁爬杆
- 百度面试题蚂蚁爬竹竿
- 百度面试题--5只蚂蚁走木棍问题的非递归解法(Java调试通过)
- [百度] 蚂蚁爬竿面试题-非递归解法
- 每天一道编程题------------百度蚂蚁爬杆问题
- 百度面试题(著名的蚂蚁问题)
- 各大互联网企业Java面试题汇总,看我如何成功拿到百度的offer
- 从一道百度面试题到分析输入url到页面返回的过程(或者查询返回过程)
- 百度面试题:有一个很大很大的输入流,大到没有存储器可以将其存储下来,而且只输入一次,如何从 这个输入 流中随机取得 m 个记录。
- 百度面试题
- 数组中只出现1次的两个数字(百度面试题) .
- 32. 百度面试题:用天平(只能比较,不能称重)从一堆小球中找出其中唯一一个较轻的,使用x次天平,最多可以从y个小球中找出较轻的那个,求y与x的关系式
- 编程之美4.7蚂蚁爬杆扩展问题附猎人抓狐狸(必胜策略)