Sha 10795-A Different Task (recursion + state transfer)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10795-A Different Task


Thought Source: Click to open the link

Question:

There are n plates on the three plates of the sinhanruata question. I will give you an initial state and an ending state and ask you how to reach the minimum number of steps.


Ideas:

Recursion + state transfer, directly from the first state to the last state does not seem so easy to do, for the largest n, first of all, we must put him in the last state position, assume that the instance is in the starting position 1 and must be placed in the position 3. The instance must first arrive at the position s: 1 ~ N-1 must be placed on 2 from the largest to the smallest, then n, and then 1 ~ N-1 is transferred to the final state, from the symmetry, that is, the final state can be changed to the State s, so the processing can be unified.

How to transfer a status to s (1 ~ K is put on a plate c). To put k, you must first have a similar status s0,: 1 ~ Put the K-1 on a plate, move k, and then 1 ~ K-1 and put it on k (the original hanruita problem, the number of steps is 2 ^ (1 <(k-1), we can see that s0 and s is a problem, this gives the state transition equation, which can be recursive.

The f (P, I, c) function indicates that the initial cylindrical number array of a given plate is P, and the steps from 1 to I are moved to the c of the plate,

The answer is f (st, K-1, 6-st [k]-ed [k]) + f (ed, K-1, 6-st [k]-ed [k]) + 1;

Then there is State Transfer: Calculate f (P, I, c), if p [I] = c, then f (P, I, c) = f (P, I-1, c); otherwise you need to move the previous I-1 plate to the central turntable, move the plate I to column c, after doing the first I-1 plate from the turntable to column c.


Code:

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     #include 
     
      #include 
      #include 
       
        #include 
        
         #include 
         
          #include 
          
           #pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 50005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;ll n,m,ans,cnt,tot,flag;ll st[65],ed[65];ll f(ll p[],ll k,ll to){ if(k==0) return 0; if(p[k]==to) return f(p,k-1,to); else return f(p,k-1,6-to-p[k])+(1LL<<(k-1));}int main(){ ll i,j,t,test=0; while(scanf("%lld",&n),n) { for(i=1;i<=n;i++) { scanf("%lld",&st[i]); } for(i=1;i<=n;i++) { scanf("%lld",&ed[i]); } t=n; while(t>0&&st[t]==ed[t]) t--; if(t) ans=f(st,t-1,6-st[t]-ed[t])+f(ed,t-1,6-st[t]-ed[t])+1; else ans=0; printf("Case %lld: %lld\n",++test,ans); } return 0;}
          
         
        
       
     
    
   
  
 



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.