hdu 2151 DP

很简单的DP,我用记忆化搜索打的~~

/*

 * hdu2151/win.cpp

 * Created on: 2012-11-2

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

const int MAXN = 105;

const int MAXM = 105;

int dp[MAXN][MAXM];

int N, P;

int dfs(int t, int m) {

    if(dp[t][m] >= 0) {

        return dp[t][m];

    }

    if(m == 0) {

        dp[t][m] = (t == P ? 1 : 0);

        return dp[t][m];

    }

    dp[t][m] = 0;

    if(t > 1) {

        dp[t][m] += dfs(t - 1, m - 1);

    }

    if(t < N) {

        dp[t][m] += dfs(t + 1, m - 1);

    }

    return dp[t][m];

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int m, t;

    while(scanf("%d%d%d%d", &N, &P, &m, &t) == 4) {

        fill_n(*dp, MAXN * MAXM, -1);

        printf("%d\n", dfs(t, m));

    }

    return 0;

}

你可能感兴趣的:(HDU)