HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...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的更多相关文章
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- 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-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- git/ TortoiseGit 在bitbucket.org 使用证书登陆
背景:使用https协议在bitbucket中进行pull,push 时每次都要输入密码,比较麻烦还耽误时间,在网上找了下保存密码的方式 使用在用户环境变量中配置_netrc 文件的方式(http:/ ...
- 扎克伯格谈Facebook创业过程
第一课:Facebook的产品研发 (1)不仅注重用户体验,更关注程序本身对社会和产品的是否有益,进而对产品做出调整 (2)以学校为标准作为群组来划分,就是对产品进行了思考后决定的,不管是直觉决定还是 ...
- 小小地预览HTML5
程序示例 <!doctype html> <html> <head> <title>First </title> <meta char ...
- COUNT(*)与COUNT(列名)的区别(转)
COUNT(*)与COUNT(列名)的区别 以前一直没有留意到COUNT(*)与COUNT(列名)的区别,昨天晚上无意中看到数据库系统工程师教程里面的一句话."如果null参与聚 ...
- arguments.callee查询调用b函数的是哪个函数
// function functionname(){ // function b(){ // console.log(arguments.callee.caller.name); // } // b ...
- 设置Cookie
var Site=new Object(); Site.Cookie={ _expires:24*3600*1000, _domain:'.gdxxb.com', set:function(name, ...
- Akka Stream文档翻译:Quick Start Guide: Reactive Tweets
Quick Start Guide: Reactive Tweets 快速入门指南: Reactive Tweets (reactive tweets 大概可以理解为“响应式推文”,在此可以测试下GF ...
- sql批量删除wordpress所有日志修订revision
wordpress日志修订是所有速度慢的罪恶之源,每次在后台发布或修改文章的时候,数据库都会产生一个revision版本的记录,几百篇日志会有几千条日志修订的记录,如果更多文章的话,那一个网页打开可能 ...
- solr教程,值得刚接触搜索开发人员一看
http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍sol ...
- 几款开源的图形界面库(GUI Libraries)
SmartWin++ 遵循BSD许可协议的C++ GUI库,建立在Windows API之上,但仍可以通过使用WineLib在Linux/xNix上使用.也支持Pocket PC和基于Windows ...