# bzoj1019 [SHOI2008]汉诺塔

## 1019: [SHOI2008]汉诺塔

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 1030  Solved: 638
[Submit][Status][Discuss]

## Sample Input

3
AB BC CA BA CB AC

7

## Source

Dp[i][x]表示前 i 个盘子一开始放在 第x柱上，用了多少步可以移动到另一柱上

P[i][x]表示前 i 个盘子一开始放在 第x柱上，按题目所给策略移动，用了Dp[i][x]步后移动到了 P[i][x] 根柱上

Dp[i+1] = a*Dp[i]+b

a，b根据策略的不同决定

。。。至于证明，不会。。。

 1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 #define For(i, s, t) for(int i = (s); i <= (t); i++)
16 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
17 #define Rep(i, t) for(int i = (0); i < (t); i++)
18 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
19 #define MIT (2147483647)
20 #define INF (1000000001)
21 #define MLL (1000000000000000001LL)
22 #define sz(x) ((bnt) (x).size())
23 #define clr(x, y) memset(x, y, sizeof(x))
24 #define puf push_front
25 #define pub push_back
26 #define pof pop_front
27 #define pob pop_back
28 #define ft first
29 #define sd second
30 #define mk make_pair
31 inline void SetIO(string Name) {
32     string Input = Name+".in",
33     Output = Name+".out";
34     freopen(Input.c_str(), "r", stdin),
35     freopen(Output.c_str(), "w", stdout);
36 }
37
38 const int N = 40;
39 LL Dp[N][4], P[N][4];
40 int n, Go[4];
41
42 inline void Input() {
43     scanf("%d", &n);
44     string S;
45     For(i, 1, 6) {
46         cin>>S;
47         int a = S[0]-'A'+1, b = S[1]-'A'+1;
48         if(!Go[a]) Go[a] = b;
49     }
50 }
51
52 inline void Solve() {
53     For(i, 1, 3) Dp[1][i] = 1, P[1][i] = Go[i];
54     For(i, 2, n)
55         For(x, 1, 3) {
56             int y = P[i-1][x];
57             int z = 1+2+3-x-y;
58             Dp[i][x] = Dp[i-1][x]+1+Dp[i-1][y];
59             if(P[i-1][y] == z) P[i][x] = z;
60             else Dp[i][x] += 1+Dp[i-1][x], P[i][x] = y;
61         }
62     cout<<Dp[n][1]<<endl;
63 }
64
65 int main() {
66     SetIO("1019");
67     Input();
68     Solve();
69     return 0;
70 }
View Code

posted @ 2015-07-03 17:01  yanzx6  阅读(122)  评论(0编辑  收藏