HDU 2563 统计问题 (DFS + 打表)

来源:互联网 发布:网络电影播放器 编辑:程序博客网 时间:2024/06/08 16:38

统计问题

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
212
 

Sample Output
37
直接DFS + 打表过,比较水
#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;}


 
1 0