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)的更多相关文章

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

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

  2. 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 ...

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

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

  4. hdu 1078 FatMouse and Cheese【dp】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:每次仅仅能走 横着或竖着的 1~k 个格子.求最多能吃到的奶酪. 代码: #include ...

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

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

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

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

  7. hdu 1078 FatMouse and Cheese 记忆化dp

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

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

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

随机推荐

  1. 【转】OkHttp使用进阶 译自OkHttp Github官方教程

    作者:GavinCT 出处:http://www.cnblogs.com/ct2011/ 英文版原版地址 Recipes · square/okhttp Wiki 同步get 下载一个文件,打印他的响 ...

  2. 基础架构之日志管理平台搭建及java&net使用

    在现代化的软件开发流程中,日志显得非常的重要,不可能再零散的游离在各个项目中,等查看日志的时候再登录服务器去到特定的目录去查看,这显然很繁琐且效率低下,所有整合一套日志管理平台,也显得非常重要,这篇文 ...

  3. [转载]hive中order by,sort by, distribute by, cluster by作用以及用法

    1. order by     Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...

  4. 抖音C#版,自己抓第三方抖音网站

    感谢http://dy.lujianqiang.com技术支持 文章更新:http://dy.lujianqiang.com这个服务器已经关了,现在没用了 版权归抖音公司所有,该博客只是为交流学习所使 ...

  5. PHP 多图片上传实例demo

    upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  6. SQL Server ->> SQL Server 2016新特性之 -- sp_set_session_context存储过程和SESSION_CONTEXT函数

    sp_set_session_context存储过程和SESSION_CONTEXT函数出现在了SQL Server 2016 CTP3.0上.它俩配合起来的作用是sp_set_session_con ...

  7. React - React Developer Tools开发者工具的安装与使用(Chrome调试插件)

    原文地址:http://www.cnplugins.com/zhuanti/how-to-use-react-tools.html 虽然我们曾经在React开发者工具的基础介绍里面有概括性的介绍过Re ...

  8. 【Leetcode】【Medium】Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. RuntimeBroker ClipboardBroker EoP

    datetime: 2017.04.28 漏洞简介 随着沙箱技术的普及,现在主流的操作系统及软件都开始支持沙箱,以此来缓解层出不穷的远程代码执行漏洞对系统造成的危害.AppContainer是自Win ...

  10. 判断ORACLE启动时使用spfile还是pfile

    自Oracle 9i以后启动的时候默认使用的初始化文件是spfile,我们可以通过如下三种方式来判断是SPFILE还是PFILE方式启动数据库.1.show parameter spfile2.sho ...