# HDU - 1176 免费馅饼 【DP】

http://acm.hdu.edu.cn/showproblem.php?pid=1176

dp[i][j] 表示 在时刻为t的时候 坐标为j 的时刻 可以获得的最多馅饼数

dp[i][j] += max(dp[i + 1][j - 1], dp[i + 1][j], dp[i + 1][j + 1]);

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

int dp[maxn][11];
int Move[3] = { -1, 0, 1 };

bool ok(int x)
{
if (x < 0 || x >= 11)
return false;
return true;
}

int main()
{
int n;
while (scanf("%d", &n) && n)
{
CLR(dp, 0);
int a, b;
int T = -INF;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &a, &b);
dp[b][a]++;
T = max(T, b);
}
for (int i = T - 1; i >= 1; i--)
{
for (int j = 0; j < 11; j++)
{
int tmp = 0;
for (int k = 0; k < 3; k++)
{
int x = j + Move[k];
if (ok(x))
{
tmp = max(tmp, dp[i + 1][x]);
}
}
dp[i][j] += tmp;
}
}
int ans = max(max(dp[1][4], dp[1][5]), dp[1][6]);
cout << ans << endl;
}
}