日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 1958 Strange Towers of Hanoi

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1958 Strange Towers of Hanoi 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Strange Towers of Hanoi
Time Limit:?1000MS Memory Limit:?30000K
Total Submissions:?2513 Accepted:?1667

Description

Background?
Charlie Darkbrown sits in another one of those boring Computer Science lessons: At the moment the teacher just explains the standard Tower of Hanoi problem, which bores Charlie to death!?

The teacher points to the blackboard (Fig. 4) and says: "So here is the problem:?
  • There are three towers: A, B and C.?
  • There are n disks. The number n is constant while working the puzzle.?
  • All disks are different in size.?
  • The disks are initially stacked on tower A increasing in size from the top to the bottom.?
  • The goal of the puzzle is to transfer all of the disks from tower A to tower C.?
  • One disk at a time can be moved from the top of a tower either to an empty tower or to a tower with a larger disk on the top.

So your task is to write a program that calculates the smallest number of disk moves necessary to move all the disks from tower A to C."?
Charlie: "This is incredibly boring—everybody knows that this can be solved using a simple recursion.I deny to code something as simple as this!"?
The teacher sighs: "Well, Charlie, let's think about something for you to do: For you there is a fourth tower D. Calculate the smallest number of disk moves to move all the disks from tower A to tower D using all four towers."?
Charlie looks irritated: "Urgh. . . Well, I don't know an optimal algorithm for four towers. . . "?
Problem?
So the real problem is that problem solving does not belong to the things Charlie is good at. Actually, the only thing Charlie is really good at is "sitting next to someone who can do the job". And now guess what — exactly! It is you who is sitting next to Charlie, and he is already glaring at you.?
Luckily, you know that the following algorithm works for n <= 12: At first k >= 1 disks on tower A are fixed and the remaining n-k disks are moved from tower A to tower B using the algorithm for four towers.Then the remaining k disks from tower A are moved to tower D using the algorithm for three towers. At last the n - k disks from tower B are moved to tower D again using the algorithm for four towers (and thereby not moving any of the k disks already on tower D). Do this for all k 2 ∈{1, .... , n} and find the k with the minimal number of moves.?
So for n = 3 and k = 2 you would first move 1 (3-2) disk from tower A to tower B using the algorithm for four towers (one move). Then you would move the remaining two disks from tower A to tower D using the algorithm for three towers (three moves). And the last step would be to move the disk from tower B to tower D using again the algorithm for four towers (another move). Thus the solution for n = 3 and k = 2 is 5 moves. To be sure that this really is the best solution for n = 3 you need to check the other possible values 1 and 3 for k. (But, by the way, 5 is optimal. . . )

Input

There is no input.

Output

For each n (1 <= n <= 12) print a single line containing the minimum number of moves to solve the problem for four towers and n disks.

Sample Input

No input.

Sample Output

REFER TO OUTPUT.

Source

TUD Programming Contest 2002, Darmstadt, Germany


漢諾塔的解法是一個遞歸,假設有ABC三個柱子,A柱上有n個盤子。將A上盤子移到C,相當于先把A上n-1個挪到B,再把A上剩余的一個挪到C,現在問題變為了將B上n-1個盤子挪到C。所以有遞推公式f(n)=2*f(n-1)+1。
題目中的是加了一根柱子,根據題目的提示,有遞推公式g(n)=min{2*g(n-k)+f(k)},即g(n)=min{2*g(n-k)+2^k-1}。可以用dp來做。

#include <cstdio> #include <cstdlib> #define MAXN 15 #define INF 100000000int main() {int dp[MAXN] = {0};dp[1] = 1;dp[0] = 0;for (int i = 2; i <= 12; ++i){int my_min = INF;for (int j = 1; j <= i; ++j){int tmp = (dp[i - j] << 1) + (1 << j) - 1;if (my_min > tmp){my_min = tmp;}}dp[i] = my_min;}for (int i = 1; i <= 12; ++i){printf("%d\n", dp[i]);}return 0; }



轉載于:https://my.oschina.net/locusxt/blog/350260

總結

以上是生活随笔為你收集整理的poj 1958 Strange Towers of Hanoi的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲无人区小视频 | 我色综合 | 天天谢天天干 | 色婷婷97 | 国产日批视频在线观看 | 丰满大爆乳波霸奶 | 少妇综合网 | 国产在线欧美在线 | 精品妇女一区二区三区 | www.麻豆av.com | 波多av在线 | 91区| a级一级黄色片 | 伊人网伊人影院 | 日韩一区二区三区免费在线观看 | 国产精品入口66mio | 一级片视频免费看 | 天天操网站 | 日韩成人午夜影院 | 大j8黑人w巨大888a片 | 99国产精品久久久久久久 | 麻豆免费电影 | 告诉我真相俄剧在线观看 | 成人性生交大全免 | 在线看福利影 | 黄色录像a | 一区二区三区三区在线 | 欧美一级片黄色 | 免费成人激情视频 | 男女无遮挡免费视频 | 国产黄色免费 | 国产一区二区在线观看视频 | 国产第一页第二页 | 亚洲乱码在线 | 污免费在线观看 | 国产精品久久久久久久免费 | 日韩美女一区二区三区 | 在线观看成年人网站 | 午夜网站免费 | 久久久婷婷 | 日韩毛片在线观看 | 精品中文字幕在线播放 | 艳妇臀荡乳欲伦交换gif | 恶虐女帝安卓汉化版最新版本 | 中国人妖和人妖做爰 | 丁香花完整视频在线观看 | 国产一二三区av | 国产99久久精品 | 精品日韩在线播放 | aa爱做片免费| 免费观看成人毛片 | 国产精品亚洲专区无码牛牛 | 久热国产视频 | 亚洲性视频| 亚洲熟女乱色一区二区三区久久久 | 欧美日韩网址 | 日批在线观看视频 | 亚洲欧美一区二区三区在线观看 | 91啦中文| 少妇高清精品毛片在线视频 | 日韩一级网站 | 在线观看免费成人 | 久草观看视频 | 国产精品久免费的黄网站 | 美国黄色a级片 | 91久久免费 | 美国少妇性做爰 | 精品人妻无码一区二区三区 | 激情综合视频 | 韩国av电影在线观看 | 免费的三级网站 | 性做爰视频免费播放大全 | 日本专区在线 | 一区二区在线免费观看视频 | 加勒比在线免费视频 | 久草国产精品视频 | 一本久草 | 欧美日韩大片在线观看 | 成人在线视频免费看 | 久久国产精品首页 | 69国产精品视频免费观看 | 成人免费视频国产在线观看 | 青青青青青青青青草 | 国产欧美一区二区在线观看 | 爱欲av | 澳门黄色网 | 日韩一区二区在线播放 | 少妇偷人精品无码人妻 | 午夜黄色影院 | 免费看裸体网站 | 欧美一级特黄aaaaaa大片在线观看 | 91精品免费| 国产一区二区黄色 | 亚洲一二三四在线 | 玉足调教丨vk24分钟 | 老熟妇高潮一区二区高清视频 | 中文字幕第315页 | 日本免费a视频 | 麻豆国产一区二区 |