[HDOJ1078]FatMouse and Cheese(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078
题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能走直线,并且目标点的数值比当前点要大。每次最长可以走k步,问最长的一条链的数值和。
用一个二维数组dp(i,j)表示某一格出发的时候最长链的数值和,然后dfs。
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <climits>
- #include <complex>
- #include <fstream>
- #include <cassert>
- #include <cstdio>
- #include <bitset>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <ctime>
- #include <set>
- #include <map>
- #include <cmath>
- using namespace std;
- const int maxn = ;
- const int dx[] = {, , , -};
- const int dy[] = {, -, , };
- int n, k;
- int G[maxn][maxn];
- int dp[maxn][maxn];
- bool ok(int x, int y) {
- return x >= && y >= && x < n && y < n;
- }
- int dfs(int x, int y) {
- if(dp[x][y]) return dp[x][y];
- int cur = ;
- for(int i = ; i < ; i++) {
- for(int j = ; j <= k; j++) {
- int xx = x + dx[i] * j;
- int yy = y + dy[i] * j;
- if(ok(xx, yy) && G[xx][yy] > G[x][y]) {
- cur = max(cur, dfs(xx, yy));
- }
- }
- }
- return dp[x][y] = cur + G[x][y];
- }
- inline bool scan_d(int &num) {
- char in;bool IsN=false;
- in=getchar();
- if(in==EOF) return false;
- while(in!='-'&&(in<''||in>'')) in=getchar();
- if(in=='-'){ IsN=true;num=;}
- else num=in-'';
- while(in=getchar(),in>=''&&in<=''){
- num*=,num+=in-'';
- }
- if(IsN) num=-num;
- return true;
- }
- int main() {
- // freopen("in", "r", stdin);
- while(~scanf("%d%d", &n, &k) && n + k != -) {
- memset(dp, , sizeof(dp));
- for(int i = ; i < n; i++) {
- for(int j = ; j < n; j++) {
- scan_d(G[i][j]);
- }
- }
- printf("%d\n", dfs(, ));
- }
- return ;
- }
[HDOJ1078]FatMouse and Cheese(记忆化搜索)的更多相关文章
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- 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)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
随机推荐
- 笔直的水管 usaco 背包
背包dp入门,需要滚动数组: #include<iostream> #include<cstdio> #include<string> #include<cs ...
- html5 webApp常用Meta标签
Html5 webApp常用Meta标签 <meta charset="UTF-8"> <meta name="viewport" conte ...
- PowerDesigner(五)-概念数据模型(CDM生成LDM,PDM和OOM)(转)
概念数据模型 概念数据模型(Conceptual Data Model,CDM):表达的是数据整体逻辑结构,该结构独立于任何软件和数据存储结构,即它只是系统分析人员,应用程序设计人员,维护人员和用户之 ...
- IDA 在string窗口中显示中文字符串
打开ida61\cfg中的ida.cfg文件找到 // (cp866 version)AsciiStringChars = "\r\n\a\v\b\t\x1B" " !\ ...
- Unity上使用Linq To XML
using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; ...
- 容器字段FieldContainer
//Ext.form.FieldContainer扩展自Ext.container.Container.当需要把多个字段或组件作为一个表单项展示的时候就需要此组件 Ext.Quick ...
- iOS16进制设置颜色
UIColor+Hex.h // // UIColor+Hex.h // 16进制颜色类别 // // Created by apple on 15-4-3. // Copyright (c) 201 ...
- 李洪强漫谈iOS开发[C语言-004]-开发概述程序设计语言程序编译过程
汇编语言 指令用特定的名字来标记,这就是汇编语言 人比较容易看懂汇编语言 汇编直接和程序一一对应的 有汇编器把程序翻译成机器码 把高级语言编译成计算机识别的语言 程序编译过程 命令行 UNIX 系统中 ...
- Map中如何把没有定义操作符<的类作为key
Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...
- C#的控制台程序输出
1. int nChar; string mystring; Console.WriteLine("{0} {1}",nChar,mystring); 其中{0},{1}为占位符 ...