题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

题意:每次仅仅能走 横着或竖着的 1~k 个格子。求最多能吃到的奶酪。

代码:



#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h> using namespace std; int n, k;
int a[110][110], dp[110][110];
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; bool is_ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= n) return false;
return true;
} int res(int x, int y)
{
int sum = 0,tmp = 0;
if (!dp[x][y])
{
for (int i = 1; i <= k; i++)
{
for (int j = 0; j < 4; j++)
{
int xx = x + dir[j][0] * i;
int yy = y + dir[j][1] * i;
if (is_ok(xx,yy) && a[xx][yy] > a[x][y])
{
sum = res(xx, yy);
tmp = max(tmp, sum);
}
}
}
dp[x][y] = tmp + a[x][y];
}
return dp[x][y];
} int main()
{
while (scanf("%d %d", &n,&k) != EOF)
{
if (n == -1 && k == -1) break;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d",&a[i][j]);
memset(dp, 0, sizeof(dp));
printf("%d\n", res(0,0));
}
return 0;
}

hdu 1078 FatMouse and Cheese【dp】的更多相关文章

  1. HDU 1078 FatMouse and Cheese【记忆化搜索】

    题意:给出n*n的二维矩阵,和k,老鼠每次最多走k步,问老鼠从起点(0,0)出发,能够得到的最大的数(即为将每走过一点的数都加起来的和最大)是多少 和上一题滑雪一样,搜索的方向再加一个循环 #incl ...

  2. HDU - 1160 FatMouse's Speed 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意 给出一系列的 wi si 要找出一个最长的子序列 满足 wi 是按照升序排列的 si 是按 ...

  3. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  4. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  5. HDU 1078 FatMouse and Cheese(记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

    FatMouse and Cheese FatMouse has stored some cheese in a city. The city can be considered as a squar ...

  7. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  8. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  9. hdu 1078 FatMouse and Cheese 记忆化dp

    只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...

随机推荐

  1. 【Sofa】Sofa比赛成绩记录

    最高得到过第4名,然后后面跌倒了第7名,现在追到了第6名.虽然名次还不是最高,但是很开心,今天能在一道一直困扰的题目上有突破,就是那个自行车预测的题目,开始过拟合了.后面进行了一些处理,效果很明显.继 ...

  2. Linux下PCI设备驱动程序开发 --- PCI驱动程序实现(三)

    三.PCI驱动程序实现 1. 关键数据结构 PCI设备上有三种地址空间:PCI的I/O空间.PCI的存储空间和PCI的配置空间.CPU可以访问PCI设备上的所有地址空间,其中I/O空间和存储空间提供给 ...

  3. javascript快速入门21--DOM总结

    跨浏览器开发 市场上的浏览器种类多的不计其数,它们的解释引擎各不相同,期待所有浏览器都一致的支持JavaScript,CSS,DOM,那要等到不知什么时候,然而开发者不能干等着那天.历史上已经有不少方 ...

  4. D3.js系列——布局:打包图和地图

    一.打包图 打包图( Pack ),用于表示包含与被包含的关系,也可表示各对象的权重,通常用一圆套一圆来表示前者,用圆的大小来表示后者. 1.布局(数据转换) var pack = d3.layout ...

  5. [Python爬虫] 之十七:Selenium +phantomjs 利用 pyquery抓取梅花网数据

    一.介绍 本例子用Selenium +phantomjs爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...

  6. python 列表合并

    列表合并主要有以下方法: 1.用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部 结果:[1, 2, 3, 4, 5, 1, 20, 30] 2.用切 ...

  7. MAC 使用技巧及常用软件备忘

    公司转向MAC快一年, 换了MAC PRO半年时间,MAC这东西除了颜值和性能,软件真是不如WINDOWS啊,不是没有,只是好多都收费! 先介绍几个跨平台的. WIN+MAC 通用: 浏览器: CHR ...

  8. 依据错误原理解决Hibernate执行出现No CurrentSessionContext configured!错误

    (1)异常信息例如以下: 严重: Servlet.service() for servlet action threw exception java.lang.RuntimeException: &l ...

  9. attempt to dereference a generic a pointer(转)

    在Linux下的GDB环境中,用p   命令查看一个void   *型的变量的时候,提示为:  "attempt   to   dereference   a   generic   a   ...

  10. UITabBarController超强拓展

    代码地址如下:http://www.demodashi.com/demo/11901.html 一.运行效果图 二.项目结构截图 三.主要API 我们主要关注的是TLTabBarController, ...