POJ 1351 number of Locks (Memory Search state compression)

Source: Internet
Author: User


Number of Locks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1161 Accepted: 571

Description

In certain factory a kind of spring locks is manufactured. There is n slots (1 < n < +, n is a natural number.) for each lock. The height of each slot is any one of the 4 values in{1,2,3,4} (neglect unit). Among the slots of a lock there is at least one pair of neighboring slots with their difference of height equal to 3 and Also there is 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 both limitations above, fi nd 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 of the output occupies one line. Its fore part was 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


Title Link: http://poj.org/problem?id=1351

The main idea: to use a digital 1,2,3,4 to find a sequence of length n, which requires at least three different numbers in the sequence and at least one set of adjacent values of 3, to find the number of sequences that satisfy the condition

Title Analysis: Dp[num][t][ok][last] indicates that the current sequence length is num, with the number of T, if there is an adjacent 1,4 OK is 1, otherwise 0,last represents the last number of the current sequence, using the memory of the search, DFS in addition to the DP of the 4 value of one more parameter St, with 2 binary to indicate the current number of the use of the state, for example, st=1111 means that four numbers are used, each time the number of searches and the current state and a bit to determine whether the number of use has increased.

#include <cstdio> #include <cstring> #define LL long longll dp[20][5][2][5];int n;ll DFS (int num, int t, int OK, int last, int st) {    if (num = = N)    {        if (T >= 3 && OK)            return 1;        else            return 0;    }    if (dp[num][t][ok][last]! =-1)        return dp[num][t][ok][last];    ll tmp = 0;    for (int i = 1; I <= 4; i++)    {        int tt, OK2 = 0, Curst = 1 << (i-1);        if ((i = = 1 && last = = 4) | | (i = = 4 && last = = 1))            Ok2 = 1;        if (St & curst)            tt = t;        else            TT = t + 1;        if (TT > 3)            tt = 3;        TMP + = DFS (num + 1, tt, OK | | ok2, I, ST | curst);    }    return dp[num][t][ok][last] = tmp;} int main () {while    (scanf ("%d", &n)! = EOF && N! =-1)    {        memset (DP,-1, sizeof (DP));        printf ("%d:%lld\n", N, DFS (0, 0, 0, 0, 0));}    }


POJ 1351 number of Locks (Memory Search state compression)

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.