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 ...
随机推荐
- Oracle 数据库字典 sys.col$ 表中关于type#的解释
sys.col$ 表是oracle基础数据字典表中的列表,表中描述了数据库中各列信息,其中type#是列的数据类型.以下表格说明了各个数值的含义,以供参考. 值 说明 1 如果列 charsetfor ...
- Java温故而知新(2)多线程详解
多线程指的是在单个程序中可以同时运行多个同的线程执行不同的任务.线程是程序内的顺序控制流,只能使用分配给序的资源和环境. 一.线程与进程的区别 多个进程的内部数据和状态都是完全独立的,而多线程 ...
- SZU2
CF:Problem 425A 区间暴力,枚举区间.交换选定区间最小值和剩余区间最大值k次. 其实等同于将剩余区间最大k个加到选定区间里,然后排序 #include <iostream> ...
- js为什么放到head中有时候失效
1.今天写js碰到一个奇怪的问题,写好的js放到body里面执行,但是放到head中没有任何效果,为什么导致这种原因呢? 看失效代码: <!DOCTYPE html PUBLIC "- ...
- java 阿里云接口实现发送短信验证码
此刻自己做的小项目中,需要用到手机发送短信验证码实现注册功能,于是就去阿里云注册了账号,并实现随机发送验证码的功能 第一步:在阿里云官网登录注册 已有支付宝或淘宝的账号可以直接登录,最后需要实名认 ...
- async await基本使用
//——<ES6经典入门到进阶>牧码人-Strive 学习笔记//express示例 const fs = require('fs'); //简单封装 fs封装成一个promise con ...
- ioc autofac简单示例
1.winform用法: nuget安装autofac public interface ILog { bool Log(string msg); } public class TXTLogger : ...
- typeof的探讨
console.log(typeof 'abc') // "string" console.log(typeof true )// "boolean" cons ...
- Linux基础之命令练习Day7-nginx,nfs
一. Nginx Nginx("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗 ...
- Linux基础之命令练习Day4-fdisk,mkfs,mlabel,mount,umount,mkswap,swapon,dd,top,free,ps,kill,rpm,yum,make
一. 硬盘分区.格式化及文件系统的管理 1. 在Linux系统中,一切皆文件.每个设备都被当作一个文件来对待. 常见的存储设备在Linux系统中的文件名如下表所示: 2. 对硬盘进行分区有以下优点: ...