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. 缓存框架EhCache的简单使用

    缓存框架EhCache的简单使用: 1.Spring和EhCache框架整合 1.1导入jar包 <dependencies> <dependency> <groupId ...

  2. svg基础知识体系建立

    一.简介:SVG 是使用 XML 来描述二维图形和绘图程序的语言. SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SVG 使 ...

  3. 高德地图 API JavaScript API

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx ...

  4. org.springframework.beans.factory.NoSuchBeanDefinitionException

    1. 问题描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxx ...

  5. 工作中常用的sql语句以及知识整理

    一.常用的sql语句 1.建表语句 create table tabname(colname1 type1 [not null][primary key], colname2 type2,...) 根 ...

  6. Repeat Number(数论)

    Repeat Number 题目描述: Definition: a+b = c, if all the digits of c are same ( c is more than ten), then ...

  7. 【Web crawler】爬虫之百度首页

    刚开始学习爬虫,照着教程手打了一遍,还是蛮有成就感的.使用版本:python2.7 *注意:python2的默认编码是ASCII编码而python3默认编码是utf-8 import urllib2 ...

  8. css3 background-sizing 属性,捎带 background-repeat 属性

    background-sizing: contain: 在指定大小的容器内把图像按照图像本身长宽比扩展到最大尺寸,有可能有留白 cover: 在指定大小的容器内,把图像按照图像本身的长宽比扩展到足够大 ...

  9. Python pymysql模块学习心得

    PyMySQL包含了一个纯Python的MySQL客户端的库,它的目的是用来替换MySQLdb,并且工作在CPython,PyPy和IronPython. PyMySQL官方地址:https://py ...

  10. QT样式

    最近在写QT的UI 分享一个助手网页 http://doc.qt.io/qt-4.8/stylesheet-examples.html