2011年8月25日

一道简单的但是经典的动态规划题目

摘要: 描述 打败万恶的ghost以后,将军准备回寝室告诉大伙儿这个消息,没想到杯具又发生了… 你知道有种鬼叫路鬼吗?让人莫名其妙的迷路,将军就被这种鬼上身了。将军本来要上楼梯的,但是因为鬼上身,他要不就上一层楼,要不就下一层楼,这个是随机的,他不能控制自己啦! 假设将军住在第M楼,因为体力原因,将军只能上或者下N次楼,刚开始将军在K楼,假设东6宿舍共有100层。现在问当体力消耗完的时候,将军刚好回到寝室那一层有多少种可能。 例如:将军住在5楼,将军的能上/下5次楼,现在在1楼, 那么将军将回不到寝室啦,为什么?我也不知道。 输入有多组测试数据,每组测试数据共一行,为M,N,K(0 < N &l 阅读全文

posted @ 2011-08-25 13:04 飞-虎 阅读(566) 评论(0) 推荐(0) 编辑

2011年8月23日

深入学习动规划

摘要: 1.引言:如下图所示,我们现在要求从A到E的最短路径,该如何做呢? 我们可以用深度优先搜索法来解决此问题,但是这个程序的效率如何呢?我们可以看到,每次除了已经访问过的城市外,其他城市都要访问,所以时间复杂度为O(n!),这是一个“指数级”的算法,那么,还有没有更好的算法呢?首先,我们来观察一下这个算法。在求从B1到E的最短距离的时候,先求出从C2到E的最短距离;而在求从B2到E的最短距离的时候,又求了一遍从C2到E的最短距离。也就是说,从C2到E的最短距离我们求了两遍。同样可以发现,在求从C1、C2到E的最短距离的过程中,从D1到E的最短距离也被求了两遍。而在整个程序中,从D1到E的最短距离. 阅读全文

posted @ 2011-08-23 19:44 飞-虎 阅读(301) 评论(0) 推荐(0) 编辑

2011年8月22日

动态规划解决POJ 3624

摘要: DescriptionBessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from theN(1 ≤N≤ 3,402) available charms. Each charmiin the supplied list has a weightWi(1 ≤Wi≤ 400), a 'desirability' factorDi(1 ≤Di≤ 100) 阅读全文

posted @ 2011-08-22 21:42 飞-虎 阅读(728) 评论(2) 推荐(1) 编辑

BFS解决POJ 2251

摘要: DescriptionYou are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid 阅读全文

posted @ 2011-08-22 09:27 飞-虎 阅读(608) 评论(3) 推荐(1) 编辑

2011年8月19日

DFS解决POJ 2386

摘要: DescriptionDue to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure 阅读全文

posted @ 2011-08-19 13:57 飞-虎 阅读(1051) 评论(0) 推荐(0) 编辑

BFS解决POJ 2386

摘要: DescriptionDue to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure 阅读全文

posted @ 2011-08-19 13:27 飞-虎 阅读(1736) 评论(0) 推荐(0) 编辑

2011年8月18日

字符串指针和整形指针的区别

摘要: 其实呀,对于 char *p="afjfkjkladfsjije"; 这句话中的指针着实很奇怪。 当你 cout<<*p<<endl; 的时候,它输出的是那个字符串的首个字符——'a'。 当你 cout<<p<<endl; 的时候,它输出的是整个字符串的内容——""afjfkjkladfsjije""。感觉到奇怪了吧,为什么这句话是输出整个字符串的内容,而不是地址呢。那么,我告诉你:没有为什么!然而,经过高人指点了一下,发现是有原因的,什么原因呢?我也不太清楚,等明白了再 阅读全文

posted @ 2011-08-18 12:15 飞-虎 阅读(2454) 评论(6) 推荐(1) 编辑

STL之map——解决POJ 2503

摘要: DescriptionYou have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.InputInput consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message 阅读全文

posted @ 2011-08-18 09:17 飞-虎 阅读(221) 评论(0) 推荐(0) 编辑

2011年8月17日

DFS解决POJ 1979

摘要: DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.Write a program to count the num 阅读全文

posted @ 2011-08-17 22:43 飞-虎 阅读(239) 评论(0) 推荐(0) 编辑

BFS解决POJ 2243

摘要: DescriptionA friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the 阅读全文

posted @ 2011-08-17 22:41 飞-虎 阅读(648) 评论(0) 推荐(0) 编辑

导航