hdu1078
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,k,dp[105][105],a[105][105];
int to[4][2] = {1,0,-1,0,0,1,0,-1};
int check(int x,int y)
{
if(x<1 || y<1 || x>n || y>n)
return 1;
return 0;
}
int dfs(int x,int y)
{
int i,j,l,ans = 0;
if(!dp[x][y])
{
for(i = 1; i<=k; i++)
{
for(j = 0; j<4; j++)
{
int xx = x+to[j][0]*i;
int yy = y+to[j][1]*i;
if(check(xx,yy))
continue;
if(a[xx][yy]>a[x][y])
ans = max(ans,dfs(xx,yy));
}
}
dp[x][y] = ans+a[x][y];
}
return dp[x][y];
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&k),n>0&&k>0)
{
for(i = 1; i<=n; i++)
for(j = 1; j<=n; j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
printf("%d\n",dfs(1,1));
}
return 0;
}
hdu1078的更多相关文章
- dfs+dp思想的结合------hdu1078
首先是题目的意思: 从一个正方形的0,0点开始走,只能横着走,竖着走,最多走k步,下一个点的数一定要比当前这个点的值大,每走一步,就加上下一个点的数据,问数据最大能有多少. 首先遇到这种题目,走来走去 ...
- hdu1078 dp(递推)+搜索
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1078 题意:老鼠从(1.1)点出发,每次最多只能走K步,而且下一步走的位置的值必须必当前值 ...
- 记忆化搜索hdu1078 dfs
http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 #includ ...
- hdu1078 记忆化搜索
/* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...
- hdu1078 bfs
//Accepted 468 KB 812 ms //bfs+dp #include <cstdio> #include <cstring> #include <iost ...
- hdu1078 记忆化搜索(DP+DFS)
题意:一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子上的数必须比上一个走的格子的数大,问最大的路径和. 我一开始的思路是,或许是普通的最大路径和,只 ...
- HDU-1078
Problem Description FatMouse has stored some cheese in a city. The city can be considered as a squar ...
- HDU-1078.FatMouseandCheese(线性dp + dfs)
本题大意:在一个n * n的迷宫内进行移动,左上角为初始位置,每次可以走的步数不能超过m,并且每次走的方格上面的数字要大于前一次走的放个数字,不能走到格子外面,问如何能使得到的数字和最大. 本题思路: ...
- HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏
FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...
- hdu1078(记忆化搜索)
题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 这道题目,值得我记住它,re了n次,以前写搜索没有注意的一个小地方,导致re这么多次的 ac代码: #inc ...
随机推荐
- Linux电源管理(3)-Generic PM之reboot过程【转】
本文转载自:http://www.wowotech.net/pm_subsystem/reboot.html 1. 前言 在使用计算机的过程中,关机和重启是最先学会的两个操作.同样,这两个操作在Lin ...
- AdobeFlashPlayer.资料
1.chrome 设置 chrome-->设置-->高级-->内容设置-->Flash 2. 3. 4. 5.
- C 字节对齐.我的算法学习之路
C/C++基础笔试题1.0(字节对齐) http://blog.csdn.net/dengyaolongacmblog/article/details/37559687 我的算法学习之路 http:/ ...
- 如何理解Box-sizing模型?
CSS3 box-sizing 属性 http://www.w3school.com.cn/tiy/t.asp?f=css3_box-sizing <style> div.containe ...
- Oracle学习笔记_04_多表查询
一.概念: 1.多表连接有以下几种分法: (1)内连接 vs 外连接 (左.右.满) (2)等值连接 vs 不等值连接 (3)非自连 ...
- PHP如何得到数组最后元素的key
1.array_keys(end($arr)) $array = array( 'one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, ); $a ...
- L99
You're not obligated to win. You're obligated to keep trying.你不一定要获胜,但你必须不断尝试.He announced an expans ...
- 3.1 第一个场景 HelloWorldScene
HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...