POJ 1351 Number of Locks (memory-based search state compression)
Number of Locks
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:1161 |
|
Accepted:571 |
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, 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
Si'an 2002
Question link: http://poj.org/problem? Id = 1351
Calculate a sequence with a length of n using numbers 1, 2, 3, and 4. The sequence must contain at least three different numbers and have at least one set of adjacent values with a difference of 3, evaluate the number of sequences that meet the condition
Question Analysis: dp [num] [t] [OK] [last] indicates that the length of the current sequence is num, and t numbers are used. If there is an adjacent 1, 4, OK is 1, otherwise, the value is 0, and the last value indicates the last number of the current sequence. The memory-based search is used. In addition to the four values of dp, the DFS also has one parameter st, in binary notation, the current number is used. For example, st = 1111 indicates that all four numbers are used. Check whether the number of searches and the current status are increased each time.
#include
#include
#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)); }}