统计问题

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6506    Accepted Submission(s): 3836

Problem Description
在一无限大的二维平面中,我们做例如以下如果:

1、  每次仅仅能移动一格;

2、  不能向后走(如果你的目的地是“向上”,那么你能够向左走,能够向右走,也能够向上走。可是不能够向下走);

3、  走过的格子马上塌陷无法再走第二次;



求走n步不同的方案数(2种走法仅仅要有一步不一样,即被觉得是不同的方案)。

 
Input
首先给出一个正整数C,表示有C组測试数据

接下来的C行,每行包括一个整数n (n<=20),表示要走n步。
 
Output
请编程输出走n步的不同方案总数;

每组的输出占一行。
 
Sample Input
2
1
2
 
Sample Output
3
7
直接DFS + 打表过,比較水
HDU 2563 统计问题 (DFS + 打表)-LMLPHP
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 20 + 5;
LL M[MAXN << 2][MAXN << 2],dp[MAXN];
bool vis[MAXN << 2][MAXN << 2];
int C, n;
void dfs(int pos,int x,int y){
if(pos > 16) return;
if(vis[x][y]) return;
dp[pos] ++;
vis[x][y] = true;
dfs(pos + 1,x + 1, y);
dfs(pos + 1,x, y + 1);
dfs(pos + 1,x, y - 1);
vis[x][y] = false;
}
void init(){
memset(dp, 0, sizeof(dp));
memset(vis,false,sizeof(vis));
dp[20]=54608393;
dp[19]=22619537;
dp[18]=9369319;
dp[17]=3880899;
dfs(0,0,50);
}
int main(){
init();
scanf("%d", &C);
while(C --){
scanf("%d", &n);
printf("%I64d\n",dp[n]);
}
return 0;
}

 
05-11 22:10