hdu 1078 FatMouse and Cheese【dfs+dp】

来源:互联网 发布:ios 元数据丢失 编辑:程序博客网 时间:2024/05/21 11:28

FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he’s going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse – after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
There are several test cases. Each test case consists of

a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) … (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), … (1,n-1), and so on.
The input ends with a pair of -1’s.
Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1
1 2 5
10 11 6
12 12 7
-1 -1
Sample Output
37
题意:
给定一张N*N的图,每个格子上有一些奶酪,初始位置是(1,1),每次只能向一个方向走最多K步,然后停下吃掉这个格子上的奶酪,但有个限制是当前格子上的奶酪必须比之前呆的一个格子里的奶酪多,问最多能吃多少奶酪。
思路:
刚开始写成了n^2,果然TLE(这是暴力了所有点都可为出发点的情况)。这道题应该是dfs + dp,对于出发点仅进行一次深搜。可以理解为结果的形成是反向的,从终点向起点(1,1)慢慢形成,每次搜索一个点周围能够满足条件的点,每找到一个点就继续递归找这个点周围满足条件的点,最终找到的点将会是数值最大的,也就是终点,这时把矩阵dp中的数值记录起来当作所有用它当作终点的路径反向搜索的最大值,然后向上层返回这个值,而对于上一层的这个点来说,下面返回的就是下面那段路程的数值的最大值,于是将这个返回上来的值和这个点的数值加起来当作最大值。(参考大神博客)

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define max_n 110using namespace std;int dp[max_n][max_n], mapp[max_n][max_n];int mov[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int n, k;//dp[i][j]: 表示从终点到起点在mapp[i][j]这个位置的最大和; int dfs(int x, int y) {    if(!dp[x][y]) {        int maxn = 0, ans = 0;        for(int i = 1; i <= k; i++) {            for(int j = 0; j < 4; j++) {                int fx = x + mov[j][0] * i; //mov[j][0] * i 表示遍历了i步所有能到到达的地方                int fy = y + mov[j][1] * i;                if(fx < 1 || fx > n || fy < 1 || fy > n)                    continue;                if(mapp[fx][fy] > mapp[x][y]) {                    ans = dfs(fx, fy); //向上递归搜索出所有满足条件的洞                     maxn = max(maxn, ans);  //选出奶酪最多                 }            }        }        dp[x][y] = maxn + mapp[x][y];    }    return dp[x][y];}int main() {    while(scanf("%d %d", &n, &k) && (n + k != -2)) {        for(int i = 1; i <= n; i++) {            for(int j = 1; j <= n; j++) {                scanf("%d", &mapp[i][j]);            }        }        memset(dp, 0, sizeof(dp));        printf("%d\n", dfs(1, 1));    }    return 0;}
原创粉丝点击