SGU 202 The Towers of Hanoi Revisited (dfs+ pretreatment)

Source: Internet
Author: User

Title Description:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2338


The Towers of Hanoi revisited Time limit: 5 Seconds Memory Limit: 32768 KB Special Judge

All must know the puzzle named?? The Towers of Hanoi??. The puzzle have three pegs and N discsof different radii, initially all disks is located on the first peg, ordered by Thei R radii-the largest atthe bottom, the smallest at the top. In a turn your may take the topmost disc from any peg and move itto another peg, the only rule says so you could not place The disc atop any smaller disk. The Problemis to move all disks to the last peg making the smallest possible number of moves.

There is the legend so somewhere in Tibet there are a monastery where monks tirelessly move disksfrom peg to peg solving The puzzle for discs. The legend says that's when they finish, the end of the TheWorld would come. Since It's well known so to solve the puzzle your need to make 2n-1 moves, Asmall calculation shows that the world see Ms to is a quite safe place for a while.

However, recent archeologists discoveries has shown that the things can be a bit worse. Themanuscript found in Tibet Mountains says that the puzzle the monks is solving have not 3 but mpegs. The problem, because when increasing the number of pegs, the number of moves needed tomove all discs from the Firs T peg to the last one following the rules described, decreases dramatically. Calculate How many moves one needs to move N discs from the first peg to the last one when Thepuzzle have M pegs and provid E The scenario for moving the discs.


This problem contains multiple test cases!

The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

Input file contains N and M (1 <= n <=, 4 <= M <= 65).


Output

On the first line output l-the number of moves needed to solve the puzzle. Next L Lines Mustcontain the moves themselves. For each move print the line of the form

Move <disc-radius> from <source-peg> to <target-peg>

If the disc is moved to the empty peg or

Move <disc-radius> from <source-peg> to <target-peg> atop <target-top-disc-radius>

If the disc is moved atop some and other disc.

Disc radii is integer numbers from 1 to N, pegs is numbered from 1 to M.


Sample Input

1

5 4


Sample Output

13
Move 1 from 1 to 3
Move 2 from 1 to 2
Move 1 from 3 to 2 atop 2
Move 3 from 1 to 4
Move 4 from 1 to 3
Move 3 from 4 to 3 atop 4
Move 5 from 1 to 4
Move 3 from 3 to 1
Move 4 from 3 to 4 atop 5
Move 3 from 1 to 4 atop 4
Move 1 from 2 to 1
Move 2 from 2 to 4 atop 3
Move 1 from 1 to 4 atop 2


Main topic:

Given n (1<= n <=64) plates and M (4<= m <= 65) Pillars, ask for the minimum number of steps required to move n plates from pillar 1th to column M, and output the move process.


Problem Solving Ideas:

The process for the entire move can be summarized as follows:

先把k个盘子,通过j跟柱子移动到中间的某根柱子上,步数:f[k][j];把剩下的i-k个盘子通过剩下的j-1跟柱子移到第j跟柱上,步数:f[i-k][j-1];最后再把中间的k个盘子移到J柱上,步数:f[k][j]。  为防止重复计算,且该题状态有限(64*65),因此作为预处理将所有的状态所需最小步数求出。再通过DFS按照上述思路打印出移动步骤即可。


Analysis of Complexity:

Time complexity O (n*n)
Space complexity O (n*n)


#include <cstdio> #include <stack> #include <cstring> #include <algorithm> #include <  iostream>using namespace Std;const int maxn = 70;const unsigned long long inf=0xffffffffffffffff;///definition infinitely large unsigned long Long F[MAXN][MAXN]; F[I][J] Represents the I disk, through the J pillars, the total movement of the minimum number of times required. int PRE[MAXN][MAXN]; PRE[I][J] The best solution for F[I][J] is to move the smallest path[i][j] disk to a pillar in the middle of an int n,m;///n plate m pillar Stack<int>stk[maxn];bool used[   Maxn];void init () {memset (f,inf,sizeof (f));       for (int i=3;i<=65;i++) {f[0][i]=0;   F[1][i]=1;       } for (int i=1;i<=64;i++) {f[i][3]=2*f[i-1][3]+1;   Pre[i][3]=i-1; } for (int i=2;i<=64;i++) for (int j=4;j<=65;j++) for (int k=1;k<i;k++) {if (F[i][j]>2*f[k][j]           +f[i-k][j-1]) {f[i][j]=2*f[k][j]+f[i-k][j-1];       Pre[i][j]=k; }}}void dfs (int nt,int mt,int Scr,int des) {if (nt==1) {if (Stk[des].size ()) printf ("Move%d from%d To%d atop%d\n ", Stk[scr].top (), Scr,des,stk[deS].top ());       else printf ("Move%d from%d to%d\n", Stk[scr].top (), scr,des);       Stk[des].push (Stk[scr].top ());       Stk[scr].pop ();   Return   } int peg=0;           for (int i=1;i<=m;i++) {if (scr!=i && des!=i &&!used[i]) {peg=i;       Break   }} dfs (PRE[NT][MT],MT,SCR,PEG);   Used[peg]=true;   DFS (Nt-pre[nt][mt],mt-1,scr,des);   Used[peg]=false; DFS (pre[nt][mt],mt,peg,des);}   int main () {init ();   int N;   cin>>n;       while (n--) {cin>>n>>m;       for (int i=1;i<=m;i++) while (!stk[i].empty ()) Stk[i].pop ();       for (int i=n;i>=1;i--) Stk[1].push (i);       memset (used,false,sizeof (used));       printf ("%llu\n", F[n][m]);   DFS (N,M,1,M); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SGU 202 The Towers of Hanoi Revisited (dfs+ pretreatment)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.