日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 三级精品在线 | 私库av在线 | 九九久久99 | 国产裸体舞一区二区三区 | 亚洲黄色大全 | 亚洲热在线视频 | 中文字幕欧美日韩 | 国产热| 超碰免费公开 | 欧美日本国产在线 | 9l视频自拍蝌蚪9l视频 | 激情视频一区二区 | 欧美顶级metart裸体全部自慰 | 久久99精品久久久久婷婷 | 国产成人无码久久久精品天美传媒 | 在线中文天堂 | 国产又粗又猛又爽又黄 | 91视频一区| 香蕉毛片 | 91沈先生在线 | 日韩一二三区在线观看 | 91精品国产福利在线观看 | 日韩中文字幕一区二区 | 中国一级大黄大黄大色毛片 | 国产一区二区免费电影 | 天天摸天天碰天天爽天天弄 | 精品丰满少妇一区二区三区 | 国产做爰高潮呻吟视频 | 欧美视频第二页 | 亚洲欧美自偷自拍 | 国产精品免费视频观看 | 国产精品亚洲综合 | 伊人网视频在线观看 | 国产一级做a爰片在线看免费 | 久久精品www | 亚洲高清视频在线播放 | 国产成人精品综合 | 玉势 (1v1 高h) | 久久久一二三四 | 日韩精品在线视频观看 | 超碰caopor| 亚洲精品字幕在线观看 | 久久国产柳州莫菁门 | 亚洲视频黄 | 日韩不卡在线播放 | 污网站在线看 | 亚洲免费视频一区 | 久久夜夜操妹子 | 在线观看国产黄色 | 欧美一级特黄aaaaaa | 一区二区日韩在线观看 | 九九av在线 | 亚洲天堂毛片 | 日本在线视频不卡 | 成年人免费在线观看视频网站 | 日本一区二区三区免费电影 | 网站在线看 | 欧美综合亚洲图片综合区 | 成年人的黄色片 | 伊人久久久久噜噜噜亚洲熟女综合 | 国产伦精品一区二区三区免.费 | 黄色国产在线 | 亚洲美女av在线 | 男女搞网站 | 亚洲精品在线免费观看视频 | 久久综合色网 | 黑人巨大国产9丨视频 | 国产又粗又大又爽视频 | 中国黄色网页 | 伊人222成人综合网 亚洲日本中文 | 黄色avv | 亚洲av无码国产精品色午夜 | 伊人日韩| 午夜寂寞影视 | 岛国av大片| 久久精品国产一区二区三区 | 性生交大片免费看女人按摩 | 亚洲国产精品狼友在线观看 | 日韩不卡一区二区三区 | 丰满白嫩尤物一区二区 | 在线免费看黄av | 色婷婷国产精品视频 | 91精品国产91久久久久 | 精品国产视频一区二区 | 欧美精品久久久久久久久 | 国产成人av无码精品 | 伊人久艹 | 99色99| 亚洲爱| 影音先锋波多野结衣 | 黄色国产一区 | 九九久久99 | 舒淇裸体午夜理伦 | 日韩日日日 | 一区二区三区日韩精品 | 卡一卡二视频 | 日本乱淫视频 | 婷婷亚洲综合五月天小说 | 亚洲性图一区二区 |