HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS)
题目大意
给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 (0, 0) 点开始, 下一步的值必须比现在的值大. 问所能得到的最大值.
解题思路
一般的题目只允许 向下 或者 向右 走, 而这个题允许走四个方向, 所以状态转移方程为 dp(x, y) = dp(nextX, nextY) + arr(x, y); dp 代表在 x, y 的最大值.
由于 下一个格子的值必须比现在的格子的大 . 因此, 不需要 vis 数组.
当无法继续遍历时候, 这个格子的 arr(x, y) 就是 dp(x, y), dp(x, y) 也是所有他能遍历得到的格子的最大的值 加上 它本身的值.
若 dp(x, y) 的值不为 0, 则 这个位置已经得到了最大的值 ( 能到 当前节点 的 节点 的 值 一定不当前节点大, 所以当前节点的值走向比它小的节点的情况), 可以直接返回 dp 数组的值.
代码
package 基础DP1;
import java.util.Scanner;
public class HDU1078 {
static int[][] arr = null;
static int[][] dp = null;
static int[][] next = new int[][]{{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
static int nRow = 0;
static int maxStep = 0;
public static int dfs(int x, int y) {
if(dp[x][y] != 0)
return dp[x][y];
int ans = 0;
for(int k = 1; k <= maxStep; k++) {
for(int i = 0; i < 4; i++) {
int nx = x + k * next[i][0];
int ny = y + k * next[i][1];
if(nx < nRow && ny < nRow && nx >= 0 && ny >= 0 && arr[nx][ny] > arr[x][y])
ans = Math.max(ans, dfs(nx, ny));
}
}
return dp[x][y] = ans + arr[x][y];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true) {
nRow = in.nextInt();
maxStep = in.nextInt();
if(nRow == -1)
break;
arr = new int[nRow][nRow];
dp = new int[nRow][nRow];
for(int i = 0; i < nRow; i++)
for(int j = 0; j < nRow; j++)
arr[i][j] = in.nextInt();
System.out.println(dfs(0, 0));
}
}
}
HDU 1078 FatMouse and Cheese ( DP, DFS)的更多相关文章
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- 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 ...
- HDU 1078 FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1078 FatMouse and Cheese【dp】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:每次仅仅能走 横着或竖着的 1~k 个格子.求最多能吃到的奶酪. 代码: #include ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- HDU 1078 FatMouse and Cheese (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- Java温故而知新(4)类String字符串
字符串是由字符组成,在Java中,字符串是对象,是描述字符的基本数据结构.String类可以用来保存一个字符串,本类是最终类,不允许继承: 1.String对象的创建 初始化 由于String对象特别 ...
- jQuery源码分析系列 : 整体架构
query这么多年了分析都写烂了,老早以前就拜读过, 不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍 我也不会照本宣科的翻译源码,结合自己的实际经验一起拜读吧! ...
- 修改phpmyadmin不能导入大文件的限制
情景:我需要导入一张1.03GB的数据表,但是phpmyadmin导入文件默认为不能超过2M.因此需要修改phpmyadmin导入文件的大小限制. 1.phpmyadmin的导入也就是php完成文件上 ...
- order by MetadataToken解决反射字段顺序问题
public class Person { public string 姓名{get;set;} } public class Profile:Person { public string 档案号{g ...
- 万网知您所需,“域”众不同--.link/.love/.help等一大波新顶级域来袭!
万网在新顶级域市场再次发力,一大波域名界的小鲜肉新鲜上线,价格优惠,限时低至9元起,更有丰富的可注册资源. 一下,即刻世界,用记录生活,用观看世界, 用和做最好的! 新上线的个性化新顶级域价格如下: ...
- VC6.0开发OCX按钮控件
原文:http://www.cnblogs.com/joinclear/archive/2013/05/21/3091934.html 0前言 1.OCX是典型的ActiveX控件,常见的OCX控件有 ...
- android query 模糊查询
package com.example.utils; import java.util.ArrayList; import android.content.ContentValues; import ...
- 【Supervised Learning】支持向量机SVM (to explain Support Vector Machines (SVM) like I am a 5 year old )
Support Vector Machines 引言 内核方法是模式分析中非常有用的算法,其中最著名的一个是支持向量机SVM 工程师在于合理使用你所拥有的toolkit 相关代码 sklearn-SV ...
- as3.2版本中中jar生成方法
lintOptions { abortOnError false } task makeJar(type: Copy) { //删除存在的 delete 'build/libs/myjar.jar' ...
- CSS media query应用中的层叠特性使用最佳实践
media query是css3规范中引入的,它提供了一种responsive design的基础机制:浏览器在不同size的设备中将以不同样式展现网页,这就给一个网页能够适应不同device一种可能 ...