hdu1078 记忆化dfs

FatMouse and Cheese

Problem Description

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


题意:老鼠每次只能去LIS的hole,每次可以在col或者row方向走k步,求最大能吃到多大的cheese

思路:很简单的dp,深搜时注意记忆化dfs即可

理解错了,以为k步里面,每一小步都可以col或者row,其实每个k都确定了单一的方向了,所以tle了,但是要哭了,明明昨天做过类似的记忆化的,怎么会tle:cold_sweat:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 1e3+10;

using namespace std;
int a[MAXN][MAXN];
int dp[MAXN][MAXN];
/*int mov[4][2] = {{1,1},{1,-1},{-1,1},{-1,-1}};*/
int n,k;

void init(){
    memset(dp,-1,sizeof(dp));
}

int check(int x,int y){
    if(x<0||x>=n||y<0||y>=n)return 0;
    else return 1;
}

int dfs(int x,int y){
    if(dp[x][y]!=-1)return dp[x][y];
    dp[x][y] = a[x][y];
    int nx,ny;
    /*for(int i=k;i>=0;i--){
        for(int j=k-i;j>=0;j--){
            if(i+j==0)continue;
            for(int w=0;w<4;w++){
                nx = x+mov[w][0]*i;
                ny = y+mov[w][1]*j;
                if(check(nx,ny)&&a[x][y]<a[nx][ny]){
                    //cout<<"ok"<<endl;
                    if(dfs(nx,ny)+a[x][y]>dp[x][y]){
                        dp[x][y] = dp[nx][ny]+a[x][y];
                    }
                }
            }
        }
    }*/
    //每次都是单向的
    for(int i=0;i<4;i++){
       // nx = x+move[j][0];
       // ny = y+move[j][1];
       nx = x;
       ny = y;
       for(int j=1;j<=k;j++){
            if(i==0)nx = x+j;
            else if(i==1)nx = x-j;
            else if(i==2)ny = y+j;
            else ny = y-j;
            if(check(nx,ny)&&a[x][y]<a[nx][ny]){
                //dfs(nx,ny,t+1);
                if(dfs(nx,ny)+a[x][y]>dp[x][y]){
                    dp[x][y] = dp[nx][ny]+a[x][y];
                }
            }
       }
    }

    return dp[x][y];
}

int main()
{
    while(scanf("%d%d",&n,&k)!=EOF&&n!=-1){
        init();
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        dfs(0,0);
        /*for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<dp[i][j]<<" ";
            }
            cout<<endl;
        }*/
        cout<<dp[0][0]<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

时间: 2024-09-29 07:58:29

hdu1078 记忆化dfs的相关文章

HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报告才明白的Orz....英语渣~ 思路: 1.1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中. 2.然后从起点开始深搜每条路,看看满足题意的路径有多少条. 3.这样搜索之后,dp[1]就是从起

从DFS到记忆化DFS

从DFS到记忆化DFS到动态规划 什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求解的过程中通过子问题的解求出原问题的解. 动态规划的分类: 1.       线性规划:拦截导弹,合唱队形,挖地雷等. 2.       区域规划:石子合并,加分二叉树,统计单词个数等. 3.       树形动规:贪吃的九头龙,二分查找树,聚会的欢乐等. 4.  

[HDU 1078]FatMouse and Cheese(记忆化DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意:一个胖老鼠要在一个n*n大小的棋盘里吃奶酪,这个老鼠每一步最多能走k单位远,而且每走一步,必须走到比当前点奶酪数多的点那去.告诉你这个棋盘里每个点上的奶酪个数,求这个老鼠最多能吃多少奶酪. 思路:类似于棋盘DP的记忆化DFS,直接搜加记忆答案就可以了. #include <iostream> #include <stdio.h> #include <string.

hdu1078 记忆化搜索

/* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,l; int m[105][105]; int dp[105][105]; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1};

HDU1078记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9499    Accepted Submission(s): 4007 Problem Description FatMouse has stored some cheese in a city. The city can be considered

*HDU1142 最短路+记忆化dfs

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7995    Accepted Submission(s): 2943 Problem Description Jimmy experiences a lot of stress at work these days, especiall

HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

题意:FatMouse在一个N*N方格上找吃的,每个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的,每次最多走k步,他走过的位置可以吃掉吃的,保证吃的数量在0-100,规定他只能水平或者垂直走,每走一步,下一步吃的数量需要大于此刻所在位置,问FatMouse最多可以吃多少东西. 需要对步数进行扩展. #include<iostream> using namespace std; #define N 101 #define max(a,b) ((a)>(b)?(a):(

(记忆化DFS)Codeforces Round #413 D-Field expansion

In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n 

HDU - 1078 FatMouse and Cheese(记忆化+dfs)

FatMouse and Cheese 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 a