直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555

因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解

但是复杂的拓补关系无法简单的用循环实现,所以直接采取记忆化搜索的方式进行DP,成功避免重叠子问题,避免超时

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
int dp[][];
int a[][];
int n,k;
int dx[]= {,,,-};
int dy[]= {-,,,};
int dfs(int x,int y)
{
int maxc=;
if(!dp[x][y])
{
for(int i=; i<; ++i)
{
for(int j=; j<=k; j++)
{
int p=x+dx[i]*j;
int q=y+dy[i]*j;
if(p<||p>=n||q<||q>=n)break;
if(a[p][q]<=a[x][y])continue;
maxc=max(maxc,dfs(p,q));
}
}
dp[x][y]=a[x][y]+maxc;
}
return dp[x][y];
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==k&&n==-)
break;
memset(dp,,sizeof(dp));
for(int i=; i<n; i++)
for(int j=; j<n; j++)
scanf("%d",&a[i][j]);
printf("%d\n",dfs(,));
}
return ;
}

HDU 1078 FatMouse and Cheese 记忆化搜索DP的更多相关文章

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

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

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

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

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

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

  4. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

  5. hdu 1078 FatMouse and Cheese 记忆化dp

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

  6. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  7. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  8. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  9. P - FatMouse and Cheese 记忆化搜索

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

随机推荐

  1. 常见架构TLB miss处理方法(转)

    转自网站:http://blog.sina.com.cn/s/blog_633f462901018reb.html 0.       综述 总的来说TLB miss处理分为硬件处理和软件处理两种,硬件 ...

  2. JPA2 关于 PagingAndSortingRepository

    And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd): Or --- 等价于 SQL 中的 ...

  3. webApp添加到iOS桌面

    iOS中的safri浏览器可以将一个网页添加到桌面,当做一个独立的应用运行. 当然,这里我们不讨论怎么去做一个webApp,这需要html5的相关知识和开发经验.这里我们只讲webApp添加桌面后到启 ...

  4. Hibernate关系级别注解

    最近在学习Hibernate的相关知识,这一站学习的是Hibernate的注解相关的操作和知识.在这里标注以下为以后查阅和需要帮助的朋友提供便利. 一. 开发环境的搭建: 1. 需要的jar包配置: ...

  5. Deep Learning and the Triumph of Empiricism

    Deep Learning and the Triumph of Empiricism By Zachary Chase Lipton, July 2015 Deep learning is now ...

  6. setTimeout浅析

    刚学习javascript的时候,感觉setTimeout很好理解,不就是过n(传入的毫秒数)毫秒,执行以下传入的函数吗?这个理解伴随了我挺长的一段时间,才对setTimeout有了新的认识,请先看下 ...

  7. 网站404,500错误页面的处理,及500异常写入errorLog日志

    1.web.xml 配置 <error-page> <error-code>404</error-code> <location>/404.jsp< ...

  8. docker 通过commit方法创建镜像(Tomcat+Java+Scala)

    前一阵试了试写Dockerfile创建docker image,但有时全靠Dockerfile写实在有些难度,退而求其次试一试使用commit来创建镜像: 想了想干脆创建一个Java+Scala+To ...

  9. Chp2: Linked List

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. Just using " ...

  10. POJ3690+位运算

    题意:给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. /* 64位的位运算!!! 题意: 给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. (64位记录状 ...