联萌十一大决战之厉兵秣马 H. Hanoi Towers(汉诺塔)

[题目链接:] (http://www.bnuoj.com/v3/contest_show.php?cid=6868#problem/H)
题目描述:
H. Hanoi Towers
Time Limit: 5000msMemory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Main
Submit Status
The “Hanoi Towers” puzzle consists of three pegs (that we will name A, B, and C) with n disks of different diameters stacked onto the pegs. Initially all disks are stacked onto peg A with the smallest disk at the top and the largest one at the bottom, so that they form a conical shape on peg A.

A valid move in the puzzle is moving one disk from the top of one (source) peg to the top of the other (destination) peg, with a constraint that a disk can be placed only onto an empty destination peg or onto a disk of a larger diameter. We denote a move with two capital letters — the first letter denotes the source disk, and the second letter denotes the destination disk. For example, AB is a move from disk A to disk B.

The puzzle is considered solved when all the disks are stacked onto either peg B (with pegs A and C empty) or onto peg C (with pegs A and B empty). We will solve this puzzle with the following algorithm.

All six potential moves in the game (AB, AC, BA, BC, CA, and CB) are arranged into a list. The order of moves in this list defines our strategy. We always make the first valid move from this list with an additional constraint that we never move the same disk twice in a row.

It can be proven that this algorithm always solves the puzzle. Your problem is to find the number of moves it takes for this algorithm to solve the puzzle using a given strategy.

Input
The input file contains two lines. The first line consists of a single integer number n (1 ≤ n ≤ 30) — the number of disks in the puzzle. The second line contains descriptions of six moves separated by spaces — the strategy that is used to solve the puzzle.

Output
Write to the output file the number of moves it takes to solve the puzzle. This number will not exceed 1018.

Sample Input
#1 3
AB BC CA BA CB AC
#2 2
AB BA CA BC CB AC
Sample Output
#1 7
#2 5
言简意赅:
此题要根据所给出的策略进行移动盘子,且要满足的规则是:不能连续两次移动同一个盘子;且每一次都是要从策略的起始位置开始遍历,再决定所选策略;且要保证每次都是小盘子在上,大盘子在下。
由于数据的问题,此题要采用动态规划中的模拟,利用模拟求前三项,求出x和y,再利用递推公式求即可。

代码实现如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N=50;
long long x,y,dp[N];

int main()
{
    int n;
    char tip[7][3];
    int k[7];
    while(scanf("%d",&n)!=EOF)
    {
        memset(tip,0,sizeof(tip));
        memset(k,0,sizeof(k));
        for(int i=0; i<6; i++)
        {
            scanf("%s",tip[i]);
            int u= tip[i][0] - 'A' +1;
            int v= tip[i][1] - 'A' +1;
            if(k[u]) continue;
            k[u]=v;
        }
        if(k[2]!=1&&k[3]!=1)
        {
            x=3;
            y=0;
        }
        else if(k[k[1]]==1)
        {
            x=3;
            y=2;
        }
        else
        {
            x=2;
            y=1;
        }
        dp[1]=1;
        for(int i=2; i<=n; i++)
        {
            dp[i]=dp[i-1]*x+y;
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}

你可能感兴趣的:(模拟)