連萌十一大決戦の激戦は馬を飛ぶH.Hanoi Towers(ハノータ)

5410 ワード

[件名リンク:](http://www.bnuoj.com/v3/contest_show.php?cid=6868#problem/Hテーマ記述:H.Hanoi Towers Time Limit:5000 ms Memory Limit:65536 KB 64-bit integer IO format:%lld Java class name:Main Submit Stuts The“Hanoi Towers”puzle consists of three pegs(thatwer pegs)、and_B,andwith n disk s of different diameters stacked onto the pegs.Initially all disk s are stacked onto peg A with the smat disk the top and the larget one the bottom、so that the y form conical shop.a pe A.pe.
A valid move in the puzzle is moving disk fm the top of one peg to the top of the the othe peg、with a constststaintthaaadisk can be plced only onto to eemipty dededededeinination pegtototodidiaaaaadidididiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadidididiststststststststststststststaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaはい、and the second letter denotes the destination disk.For example、AB is a move from disk A to disk B.
The puzle is consided soloved when all the disk stacked one ther eigh B(with pegs A and C empty)or onto peg C(with pegs A and B empty).We will solove this puzle with the folled with the with the gorim.
All six potenttial moves in the game(AB,AC,BA,BC,CA,and CB)arranged into a list.The order of moves in in list defines our stratgy.We always make the first valid move from is the distration
It can be proven that is algorithm always solives the puzle.Your proble m is to find the number of moves it tars for is algorithm to solive the puzle using a given stregy.
Input The input file contains two lines.The first line consists of a single integer number n(1≦n≦30)-the number of disky the putzle.The second line contains descriptions of six moves parated strated
Output Write to the output file the number of moves it tars to solive the puzle.This number will not exceed 1018.
Sample Input舸1 AB BC_CA BA CB AC荐2 AB BACA 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
"
,dp[n]); } return 0; }