Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1019
Hanoi game, but with a mobile priority, given a priority move target without violating the original rules. The number of steps required to complete the game.
Analysis
We use \ (f[j][i]\) to indicate the number of steps required to remove the topmost \ (i\) plate on the top \ (j\) column, and use \ (g[j][i]\) to indicate to which pillar it will be moved.
Then for \ (f[j][i]\), first to remove the former \ (i-1\), the number of steps to \ (f[j][i-1]\), to the column (y\), this time again to the first \ (i\) plate moved to the \ (3-j-y=z\) pillar up.
The next step is to move \ (i-1\) plates on \ (y\) up to \ (z\). In two cases:
1.\ (g[y][i-1]=z\), so you can move directly. $ $f [j][i]=f[j][i-1]+1+f[y][i-1]$$
2.\ (g[y][i-1]=j\), this situation must first move the \ (i-1\) plate to \ (j\) up, then the first \ (i\) moved to \ (y\) up, and then the \ (i-1\) moved to \ (y\) up.
$ $f [j][i]=f[j][i-1]+1+f[y][i-1]+1+f[j][i-1]$$
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong Longll;5 Const intmaxn= -+5;6 intn,r[5],g[3][MAXN];7ll f[3][MAXN];8 Chars[5];9 BOOLvis[3];TenInlinevoidsolve () { One for(intI=2; i<=n;i++) for(intj=0;j<3; j + +){ A inty=g[j][i-1],z=3-y-j; f[j][i]=f[j][i-1]+1; - if(z==g[y][i-1]) {f[j][i]+=f[y][i-1];g[j][i]=Z;} - Else{f[j][i]+=f[y][i-1]+1+f[j][i-1];g[j][i]=y;} the } -printf"%lld\n", f[0][n]); - } -Inlinevoidinit () { +scanf"%d",&n); - for(intI=0;i<6; i++){ +scanf"%s", s); A int from=s[0]-'A', to=s[1]-'A'; at if(vis[ from])Continue; -vis[ from]=true; g[ from][1]=to; f[ from][1]=1; - } - } - intMain () { - init (); in solve (); - return 0; to}
View Code
1019: [SHOI2008] Hanoi time limit:1 Sec Memory limit:162 MB
submit:1278 solved:789
[Submit] [Status] [Discuss] Description
Hanoi consists of three pillars (denoted by a B-C) and n-sized hollow plates of different sizes. At first n plates were stacked on pillar a,
Large in the lower, small on top, forming a tower-shaped cone body.
A lawful operation on the Hanoi means: take a plate from the top of a pillar to the top of the other pillar and ensure that it is moved
The plate must be placed on a larger plate than it (if you move to an empty pillar you do not need to meet this requirement). We can use a two-letter description.
One operation: The first letter represents the starting column and the second letter represents the target column. For example, AB is moving the top plate of pillar A to
Pillar B. The goal of Hanoi's game is to move all the plates from pillar A to column B or column C. There is a very simple and classic strategy that can help
Help us to finish the game. First, we have six operations in any order (AB, AC, BA, BC, CA, CB) before any action is performed
Assign different priorities, and then we always choose to move the plates with the following two conditions, until all the plates are moved from pillar A to
Another pillar: (1) This operation is the highest priority in all legal operations, and (2) the plate to be moved by this operation is not moved by the last operation
The dish that moved. Can prove that the above strategy will be able to complete the Hanoi game. Now your task is to assume that given the priority of each operation,
Calculates the number of steps required to move Hanoi in accordance with the above policy.
Input
The input has two lines. The first behavior is an integer n (1≤n≤30), which represents the number of plates. The second line is a string of uppercase ABC characters, representing six types of
Priority, the previous operation has a higher priority. Each operation is separated by a space.
Output
Just output one number, which indicates the number of moves. We guarantee that the answer will not exceed 10 18.
Sample Input3
AB BC CA BA CB ACSample Output7Hintsource
Bzoj_1019_[scoi2008]_ Hanoi _ (DP)