poj1351Number of Locks(記憶化搜尋)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   io   

題目連結:傳送門
思路:這道題是維基百科上面的記憶化搜尋的例題。。。
四維狀態dp[maxn][5][2][5]分別表示第幾根棒子,這根棒子的高度,是否達到題目的要求和使用不同棒子數,那麼接下來就是狀態轉移了。。。要用到位元運算判斷以前是否這種高度的棒子用到沒。。。那麼這個問題就解決了。。。題目:Number of Locks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1126   Accepted: 551

Description

In certain factory a kind of spring locks is manufactured. There are n slots (1 < n < 17, n is a natural number.) for each lock. The height of each slot may be any one of the 4 values in{1,2,3,4}( neglect unit ). Among the slots of a lock there are at least one pair of neighboring slots with their difference of height equal to 3 and also there are at least 3 different height values of the slots for a lock. If a batch of locks is manufactured by taking all over the 4 values for slot height and meet the two limitations above, find the number of the locks produced.

Input

There is one given data n (number of slots) on every line. At the end of all the input data is -1, which means the end of input.

Output

According to the input data, count the number of locks. Each output occupies one line. Its fore part is a repetition of the input data and then followed by a colon and a space. The last part of it is the number of the locks counted.

Sample Input

23-1

Sample Output

2: 03: 8

Source

Xi‘an 2002
代碼:
#include<cstdio>#include<cstring>#include<iostream>#define New (1<<(d-1))using namespace std;const int maxn=17+10;long long  dp[maxn][5][2][5];int n;long long dfs(int ith,int height,int k,int use,int s){    if(dp[ith][height][k][use]!=-1)          return dp[ith][height][k][use];    if(ith==n)    {        if(k&&use>=3)            return 1;        else            return 0;    }    long long ans=0;    int tmp;    for(int d=1;d<=4;d++)    {        if(!(s&New))           tmp=use+1;        else           tmp=use;       // tmp=min(use,3);        if(k||(d*height==4&&d!=2))            ans=ans+dfs(ith+1,d,1,tmp,s|New);        else            ans=ans+dfs(ith+1,d,0,tmp,s|New);    }    return dp[ith][height][k][use]=ans;}int main(){    while(~scanf("%d",&n))    {        if(n==-1)   return -1;        printf("%d: ",n);        memset(dp,-1,sizeof(dp));        if(n<3)            puts("0");        else            {                dfs(0,0,0,0,0);                printf("%lld\n",dp[0][0][0][0]);            }    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.