题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078
给出一个n*n的矩阵,每次最多走m步,而且下一步的值必须大于当前值,求最大值
从(0,0)位置开始递归,求最大值,最后dp[0][0]就是最大值
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<string.h> #include<queue> using namespace std; const int maxn = 105; const int inf = 0x3f3f3f3f; typedef long long ll; int n, m; int mp[maxn][maxn]; int dp[maxn][maxn]; int d[4][2] = { 1,0,-1,0,0,1,0,-1 }; int dfs(int s, int t) { int max = 0; if (dp[s][t] > 0) { return dp[s][t]; } for (int i = 0; i < 4; i++) { for (int j = 1; j <= m; j++) { int x1 = d[i][0] * j + s; int y1 = d[i][1] * j + t; if (x1<0 || y1<0 || x1>=n || y1>=n) { continue; } if (mp[s][t] < mp[x1][y1]) { int tmp = dfs(x1, y1); if (tmp > max) { max = tmp; } } } } dp[s][t] = max + mp[s][t]; return dp[s][t]; } int main() { //freopen("C://input.txt", "r", stdin); while (scanf("%d%d", &n, &m) != EOF) { if (n == -1 && m == -1) { break; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &mp[i][j]); } } memset(dp, 0, sizeof(dp)); printf("%d\n", dfs(0, 0)); } return 0; }
hdu 1078 FatMouse and Cheese (DP)
猜你喜欢
转载自blog.csdn.net/Evildoer_llc/article/details/82928777
今日推荐
周排行