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

FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17910    Accepted Submission(s): 7619

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
 
Sample Output
37
 题意:从(0,0)出发每次上下左右走一步,每次可以走1-k次停下吃奶酪,求最多可以吃多少奶酪。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 998244353
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[][] ;
int dp[][];
int dir[][] = {{ , } , {- , } , { , } , { , -}};
int n , k ; int dfs(int x , int y)
{
if(dp[x][y])//避免重复赋值,减少时间
return dp[x][y];
dp[x][y] = a[x][y] ;
for(int i = ; i < ; i++)
{
for(int j = ; j <= k ; j++)
{
int nx = x + dir[i][]*j ;
int ny = y + dir[i][]*j ;
if(nx >= && nx < n && ny >= && ny < n)
{
if(a[nx][ny] > a[x][y])
{
dp[x][y] = max(dp[x][y] , dfs(nx , ny) + a[x][y]);
}
}
} }
return dp[x][y] ;
} int main()
{
while(~scanf("%d%d" , &n , &k) && (n != - || k != -))
{
memset(dp , , sizeof(dp));
for(int i = ; i < n ; i++)
{
for(int j = ; j < n ; j++)
{
scanf("%d" , &a[i][j]);
}
}
cout << dfs( , ) << endl ;
}
return ;
}

dp(动态规划之最佳路径+dfs)的更多相关文章

  1. Day 5 笔记 dp动态规划

    Day 5 笔记 dp动态规划 一.动态规划的基本思路 就是用一些子状态来算出全局状态. 特点: 无后效性--狗熊掰棒子,所以滚动什么的最好了 可以分解性--每个大的状态可以分解成较小的步骤完成 dp ...

  2. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  3. 树形DP——动态规划与数据结构的结合,在树上做DP

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构的第15篇,也是动态规划系列的第4篇. 之前的几篇文章当中一直在聊背包问题,不知道大家有没有觉得有些腻味了.虽然经典的文 ...

  4. 【模板整合计划】DP动态规划

    [模板整合计划]DP动态规划 一:[背包] 1.[01背包] 采药 \([P1048]\) #include<algorithm> #include<cstdio> int T ...

  5. 【leetcode-62,63,64 动态规划】 不同路径,最小路径和

    给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [   [1,3,1], [1,5,1] ...

  6. DP动态规划学习笔记——高级篇上

    说了要肝的怎么能咕咕咕呢? 不了解DP或者想从基础开始学习DP的请移步上一篇博客:DP动态规划学习笔记 这一篇博客我们将分为上中下三篇(这样就不用咕咕咕了...),上篇是较难一些树形DP,中篇则是数位 ...

  7. leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]

    题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...

  8. ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径

    上篇已经创建好了线数据(shp文件格式)链接:点我 这篇将基于此shp线数据创建网络数据集. 在此说明:shp数据的网络数据集仅支持单一线数据,也就是说基于shp文件的网络数据集,只能有一个shp线文 ...

  9. 使用 EOLINKER 进行接口测试的最佳路径 (下)

    本文为 <使用 EOLINKER 进行接口测试的最佳路径> 下半部分文章,主要介绍测试脚本如何执行和报告生成,以及测试项目人员如何协作.还没看过上篇文章请戳 使用 EOLINKER 进行接 ...

随机推荐

  1. oozie与hue整合 执行WC案例报错: 连接10020端口被拒绝

    Call From hdp-05/192.168.230.15 to hdp-01:10020 failed on connection exception: java.net.ConnectExce ...

  2. 面试官:SpringBoot jar 可执行原理,知道吗?

    文章篇幅较长,但是包含了SpringBoot 可执行jar包从头到尾的原理,请读者耐心观看.同时文章是基于 SpringBoot-2.1.3进行分析.涉及的知识点主要包括Maven的生命周期以及自定义 ...

  3. bzoj4025 二分图 LCT + 最小生成树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4025 题解 貌似这道题有一个非常简单的做法是线段树分治+并查集. 可是我是为了练 LCT 来做 ...

  4. A1009

    两个多项式的乘积 两个数组,一个放多项式1,一个放结果 注意:arr2[j+exp]+=arr1[j]*coe; 因为有指数相加相同的情况下需要合并系数 #include<cstdio> ...

  5. select下拉框数据回显

    前台页面 <select class="select" name="operatorId" id="operatorId" style ...

  6. mysql SELECT INTO语句 语法

    mysql SELECT INTO语句 语法 作用:用于创建表的备份复件. 语法:SELECT * INTO new_table_name [IN externaldatabase]  FROM ol ...

  7. Windows上安装Apache

    1.下载 (1)进入Apache官网http://httpd.apache.org— (2)点击Download (3)点击Files for Microsoft Windows (4)点击Apach ...

  8. 洛谷 P4571 BZOJ 2257 [JSOI2009]瓶子和燃料

    bzoj题目链接 上面hint那里是选择第2个瓶子和第3个瓶子 Time limit 10000 ms Memory limit 131072 kB OS Linux Source Jsoi2009 ...

  9. Redis分布式锁服务

    阅读目录: 概述 分布式锁 多实例分布式锁 总结 概述 在多线程环境下,通常会使用锁来保证有且只有一个线程来操作共享资源.比如: object obj = new object(); lock (ob ...

  10. POJ 1434 Fill the Cisterns! (模拟 or 二分)

    Fill the Cisterns! 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/F Description During t ...