FatMouse and CheeseI - I
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.
InputThere 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.
OutputFor 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
1 #include<stdio.h>
2 #include<string.h>
3 #include<queue>
4 #include<set>
5 #include<iostream>
6 #include<map>
7 #include<stack>
8 #include<cmath>
9 #include<algorithm>
10 #define ll long long
11 using namespace std;
12 int n,k;
13 int f[4][2]={1,0,0,1,-1,0,0,-1};
14 int dp[1000][1000],e[1000][1000];
15 int dfs(int x,int y)
16 {
17 if(dp[x][y]) return dp[x][y];
18 for(int i=0;i<4;i++)
19 {
20 for(int j=1;j<=k;j++)
21 {
22 int tx=x+j*f[i][0];
23 int ty=y+j*f[i][1];
24 if(tx>=0&&ty>=0&&tx<n&&ty<n&&e[tx][ty]>e[x][y])
25 {
26 dp[x][y]=max(dfs(tx,ty)+e[tx][ty],dp[x][y]);
27 }
28 }
29 }
30 return dp[x][y];
31 }
32 int main()
33 {
34 while(~scanf("%d%d",&n,&k)&&(n+k)!=-2)
35 {
36 memset(dp,0,sizeof(dp));
37 for(int i=0;i<n;i++)
38 for(int j=0;j<n;j++)
39 scanf("%d",&e[i][j]);
40 printf("%d\n",dfs(0,0)+e[0][0]);
41 }
42 }
FatMouse and CheeseI - I的更多相关文章
- FatMouse's Speed——J
J. FatMouse's Speed FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- Hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1078 FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- FatMouse' Trade_贪心
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- [题解]hdu 1009 FatMouse' Trade(贪心基础题)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- 【HDU 1009】FatMouse' Trade
题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...
- FatMouse的交易问题
想按照某个值排序,用sort()函数,结果想了半天不知道用数组怎么解决,然后看了答案,才知道原来可以用struct,想想我真是笨死了.. 原题描述以及答案如下: Problem Description ...
- hdu 1009:FatMouse' Trade(贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- ssh问题之复盘
一.问题发生.排查以及解决 某天H博士在登录B服务器时发现一个严重的问题,问题是H博士在执行脚本出现一个异常,这个异常是过去我执行脚本只需输入一次密码,现在要输入五六次,只有输入五六次后才能正确执行完 ...
- Java 中泛型的实现原理
泛型是 Java 开发中常用的技术,了解泛型的几种形式和实现泛型的基本原理,有助于写出更优质的代码.本文总结了 Java 泛型的三种形式以及泛型实现原理. 泛型 泛型的本质是对类型进行参数化,在代码逻 ...
- 你都用过SpringCloud的哪些组件,它们的原理是什么?
前言 看到文章的题目了吗?就是这么抽象和笼统的一个问题,确实是我面试中真实被问到的,某共享货车平台的真实面试问题. SpringCloud确实是用过,但是那是三四年前了,那个时候SpringCloud ...
- Java 在pom.xml中配置build resources, 来防止我们资源导出失败问题(Maven项目)
在pom.xml中配置build, 来防止我们资源导出失败问题 <!--在build中配置resources, 来防止我们资源导出失败问题--> <build> <res ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- poj-DNA排序
描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...
- 一个div画同心圆
二话不说上代码 background-image:radial-gradient(7px,#00A4FF 50%,#fff 75%,#00A4FF 94%); 7px是圆的半径 效果:
- ReactRouter的实现
ReactRouter的实现 ReactRouter是React的核心组件,主要是作为React的路由管理器,保持UI与URL同步,其拥有简单的API与强大的功能例如代码缓冲加载.动态路由匹配.以及建 ...
- uni-app开发经验分享十一: uniapp iOS云打包修改权限提示语
打包提交appstore如果用到了如下权限需要修改提示语,详细描述使用这个权限的原因,如不修改提示语appstore审核可能会被拒绝.Apple的原则是,如果一个app想要申请用户同意某个隐私信息访问 ...
- Qt QMenuBar和QMenu以及QAction巧妙的使用方法
这里简单介绍QMenuBar和QMenu以及QAction是什么,其详细功能本文不做介绍,如果还不了解的朋友可以查阅Qt的帮助手册或浏览其它相关博客.如下图,软件中蓝色条框是QMenuBar用来承载Q ...