dp(动态规划之最佳路径+dfs)
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
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.
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.
1 2 5
10 11 6
12 12 7
-1 -1
//#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)的更多相关文章
- Day 5 笔记 dp动态规划
Day 5 笔记 dp动态规划 一.动态规划的基本思路 就是用一些子状态来算出全局状态. 特点: 无后效性--狗熊掰棒子,所以滚动什么的最好了 可以分解性--每个大的状态可以分解成较小的步骤完成 dp ...
- (转)dp动态规划分类详解
dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...
- 树形DP——动态规划与数据结构的结合,在树上做DP
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构的第15篇,也是动态规划系列的第4篇. 之前的几篇文章当中一直在聊背包问题,不知道大家有没有觉得有些腻味了.虽然经典的文 ...
- 【模板整合计划】DP动态规划
[模板整合计划]DP动态规划 一:[背包] 1.[01背包] 采药 \([P1048]\) #include<algorithm> #include<cstdio> int T ...
- 【leetcode-62,63,64 动态规划】 不同路径,最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- DP动态规划学习笔记——高级篇上
说了要肝的怎么能咕咕咕呢? 不了解DP或者想从基础开始学习DP的请移步上一篇博客:DP动态规划学习笔记 这一篇博客我们将分为上中下三篇(这样就不用咕咕咕了...),上篇是较难一些树形DP,中篇则是数位 ...
- 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 ...
- ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径
上篇已经创建好了线数据(shp文件格式)链接:点我 这篇将基于此shp线数据创建网络数据集. 在此说明:shp数据的网络数据集仅支持单一线数据,也就是说基于shp文件的网络数据集,只能有一个shp线文 ...
- 使用 EOLINKER 进行接口测试的最佳路径 (下)
本文为 <使用 EOLINKER 进行接口测试的最佳路径> 下半部分文章,主要介绍测试脚本如何执行和报告生成,以及测试项目人员如何协作.还没看过上篇文章请戳 使用 EOLINKER 进行接 ...
随机推荐
- 小程序里的自定义组件:组件的外部样式externalClasses的使用
启用外部样式: 自定义组件: v-tag 在html 引入 在组件写外部样式的css : .ex-tag { background-color: #fffbdd ; } 在组件使用该外部的样式 这个时 ...
- bzoj3091 城市旅行 LCT + 区间合并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3091 题解 调了整个晚自习才调出来的问题. 乍一看是个 LCT 板子题. 再看一眼还是个 LC ...
- kettle imestamp : Unable to get timestamp from resultset at index 22
在做ETL的时候,连接MySQL读取含有timestamp类型的表,出现如下错误: 经Google,据说是MySQL自身的问题.解决方法也很简单,在Spoon的数据库连接中,打开选项,加入一行命令参数 ...
- ESP8266-12F 中断
外部中断: 基于ESP8266的NodeMcu的数字IO的中断功能是通过attachInterrupt,detachInterrupt函数所支持的.除了D0/GPIO16,中断可以绑定到任意GPIO的 ...
- day2 for,not,while,range
>>> def str_len(s): ... l = len(s) ... if l > 3: ... print("3") ... elif l < ...
- 【leetcode】1160. Find Words That Can Be Formed by Characters
题目如下: You are given an array of strings words and a string chars. A string is good if it can be form ...
- 对Nuxt的研究
Nuxt就是基于Vue的一个应用框架,采用服务端渲染,让你的SPA应用(Vue)也可以拥有SEO Nuxt的生命周期有些在服务端(Node),客户端,甚至两边都在: 1.其他之前都不存在Window对 ...
- va_list原理及用法
最后更新:2017-02-22 这是一篇很早很早的博客文章,虽然很基础,但是毕竟曾经历程,因此也保存下来 1. 概念 va_list 是在C语言中定义的宏,指在解决 变参问题是指参数的个数不定,可以是 ...
- P1538迎春舞会之数字舞蹈
传送 输入输出样例:(洛咕的太丑了就不放了) (1前面有三个空格) 这真是一群闲(qian)圈(zou)的人.大号+小号提交了不下10遍终于a了 好了我们来研究一下这些数字"美观" ...
- redispy
w wuser@ubuntu:~/redispy$ redis-cli > keys * ) "w" ) "wpy" > set w1 w1valu ...