POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。 Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。 Output
输出最长区域的长度。 Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 Sample Output
25
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a));
const int max1=105;
long long int dp[max1][max1];
//int sum[max1];
long long int ob[max1][max1];
int dx[5]={0,-1,0,1,0},
dy[5]={0,0,-1,0,1};
//dx与dy一组,构成上下左右四个方向
long long int r,//长
c,//宽
t,//做中间变量,用来寻找最大的ans
ans;//预设答案变量
int search(int x,int y);//声明search函数
int main()
{
cin>>r>>c;//输入长宽
ans=0;//无合适条件下设ans为最小值0
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
cin>>ob[i][j];//输入矩阵
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){//遍历矩阵中每一个点
t=search(i,j);//调用searc函数
dp[i][j]=t;
if(t>ans) ans=t;
}
cout<<ans<<endl;
}
int search(int x,int y)
{
int w,tmp,nx,ny;
if(dp[x][y]>0){ //如果dp大于0代表dp被调用过可以直接使用
return(dp[x][y]);//返回dp[i][j]的值
}
w=1;
for(int i=1;i<=4;i++){//遍历四个方向
nx=x+dx[i];
ny=y+dy[i];
if((nx>=1)&&(nx<=r)&&(ny>=1)&&(ny<=c)&&(ob[x][y]<ob[nx][ny])){//边界
tmp=search(nx,ny)+1;//递归求当前点能够到达的最大值
if(tmp>w) w=tmp;
}
}
dp[x][y]=w;
return w;
}
/*
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
*/
FatMouse and Cheese
Problem Description
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he’s going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse – after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
There are several test cases. Each test case consists of
a line containing two integers between 1 and 100: n and k n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) … (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), … (1,n-1), and so on. The input ends with a pair of -1’s.
Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
Sample Output
37
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int ob[10050][105];
int dp[10050][105];
int dx[4]={0,1,0,-1},
dy[4]={1,0,-1,0};
int x,y,ans,temp,k;
int search(int q,int w);
int main()
{
while(cin>>x>>k)
{
memset(ob,0,sizeof(ob));
memset(dp,0,sizeof(dp));
if(x==-1||y==-1) break;
y=x;
ans=0;
for(int i=1;i<=y;i++)
for(int t=1;t<=x;t++) cin>>ob[i][t];
cout<<search(1,1)<<endl;
}
}
int search(int q,int w)
{
int nx,ny,t=0;
if(dp[q][w]>0) return dp[q][w];
for(int j=1;j<=k;j++)
for(int i=0;i<4;i++)
{
nx=q+dx[i]*j;
ny=w+dy[i]*j;
if((nx>=1)&&(nx<=x)&&(ny>=1)&&(ny<=y)&&(ob[q][w]<ob[nx][ny]))
t=max(t,search(nx,ny));
}
return dp[q][w]=t+ob[q][w];
}
- Web安全实战
- 【翻译】MongoDB指南/聚合——聚合管道
- PHP异步高并发扩展Swoole
- TensorFlow从0到1丨 第五篇:TensorFlow轻松搞定线性回归
- 【直播】我的基因组59:把我的数据伪装成23andme或wegene的芯片数据
- asp.net web api客户端调用
- 细说WebSocket - Node篇
- TensorFlow从0到1丨 第六篇:解锁梯度下降算法
- .Net多线程编程—误用点分析
- Web开发常见的几个漏洞解决方法
- .Net多线程编程—同步机制
- .Net多线程编程—Parallel LINQ、线程池
- 没有自己的服务器如何学习生物数据分析(下篇)
- .Net多线程编程—并发集合
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- PAT (Basic Level) Practice (中文)1041 考试座位号
- PAT (Basic Level) Practice (中文)1042 字符统计
- 搭建 Apache Jmeter 分布式压测与监控,真那么难搞定?|实战干货
- PAT (Basic Level) Practice (中文)1056 组合数的和
- PAT (Basic Level) Practice (中文)1057 数零壹
- PAT (Basic Level) Practice (中文)1063 计算谱半径
- PAT (Basic Level) Practice (中文)1064 朋友数
- PAT (Basic Level) Practice (中文)1076 Wifi密码
- PAT (Basic Level) Practice (中文)1077 互评成绩计算
- PAT (Basic Level) Practice (中文)1081 检查密码
- PAT (Basic Level) Practice (中文)1082 射击比赛
- PAT (Basic Level) Practice (中文)1083 是否存在相等的差
- 2017年天梯赛全国总决赛题集 L2-1 点赞狂魔
- 2017年天梯赛全国总决赛题集 L1-4 稳赢
- 2017年天梯赛全国总决赛题集 L1-8 矩阵A乘以B