poj 1351 记忆化搜索

//好久没水题了,水一发记忆化好了= ̄ω ̄=

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "cmath"
 6 using namespace std;
 7 __int64 dp[20][5][2][2];
 8 bool vis[20][5][2][2];
 9 int n;
10
11 __int64 dfs(int rem, int num, int _14, int _23)
12 {
13     if(rem == 0) {
14         if(_14 && _23)
15             return 1;
16         return 0;
17     }
18     if(vis[rem][num][_14][_23])
19         return dp[rem][num][_14][_23];
20     vis[rem][num][_14][_23] = 1;
21     int i, n_14, n_23;
22     for(i = 1; i <= 4; ++i) {
23         if(_14 == 1 || (fabs(int(i - num))==3 && num != 0))
24             n_14 = 1;
25         else
26             n_14 = 0;
27         if(_23 == 1 || i == 2 || i == 3)
28             n_23 = 1;
29         else
30             n_23 = 0;
31         dp[rem][num][_14][_23] += dfs(rem - 1, i, n_14, n_23);
32     }
33     return dp[rem][num][_14][_23];
34 }
35
36 int main()
37 {
38     while(scanf("%d", &n) && (n != -1)) {
39         printf("%d: %I64d\n", n, dfs(n, 0, 0, 0));
40     }
41 }
时间: 2024-11-03 02:15:31

poj 1351 记忆化搜索的相关文章

poj 1088 记忆化搜索

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 90368   Accepted: 34028 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

Test for Job (poj 3249 记忆化搜索)

Language: Default Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9733   Accepted: 2245 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's

POJ 1143 记忆化搜索+博弈论

Number Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3181   Accepted: 1280 Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take tu

POJ 1179 记忆化搜索

Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5024   Accepted: 2108 Description Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an int

POJ 1579 Function Run Fun 【记忆化搜索入门】

题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20560   Accepted: 10325 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <=

poj1191 分治思想,记忆化搜索

http://poj.org/problem?id=1191 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和.现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小. 均方差,其中平均值,xi为第i块矩形棋盘的总分. 请编程对给

POJ 1351 Number of Locks (记忆化搜索 状态压缩)

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 are n slots (1 < n < 17, n is a natural number.) for each lock. The height

POJ 2704 Pascal&#39;s Travels (基础记忆化搜索)

Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5328   Accepted: 2396 Description An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from t

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17