hdu 1078 FatMouse and Cheese

来源:互联网 发布:淘宝网刷销量 编辑:程序博客网 时间:2024/06/05 05:07

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1078

题意: 给定一个 n*n 的 map, 每个点有其权值, 只能从权值小的点移动到权值大的点, 一次移动只能沿一个方向且距离小于 k.
问 能吃到点的权值和最大为多少?

很显然的记忆化搜索 开始 bfs 写崩了…..

#include <stdio.h>#include <iostream>#include <cstring>#define localusing namespace std;const int MAX = 111, dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};int mp[MAX][MAX], dp[MAX][MAX], n, k;int mmax(int a, int b) {return a > b ? a : b;}int dfs(int x, int y) {    if (dp[x][y] != -1) return dp[x][y];    int nx = 0, ny = 0, ret = mp[x][y];    for (int j = 0; j < 4; ++j) {        for (int i = 1; i <= k; ++i) {             nx = x + dir[j][0] * i;            ny = y + dir[j][1] * i;            if (nx >= n || ny >= n || nx < 0 || ny < 0)                 continue;            if (mp[nx][ny] > mp[x][y])                ret = mmax(ret, dfs(nx, ny) + mp[x][y]);         }    }    return dp[x][y] = ret;}int main() {#ifdef local    freopen("in.in", "r", stdin);    freopen("out.t", "w", stdout);#endif     while (cin >> n >> k && (n != -1 && k != -1)) {        for (int i = 0; i < n; ++i)             for (int j = 0; j < n; ++j)                cin >> mp[i][j];        memset (dp, -1, sizeof (dp));        cout << dfs(0, 0) << endl;    }    return 0;}

咕咕咕
2017-10-04

原创粉丝点击