UVA - 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp---记忆化搜索)
题意:在一个R*C(R, C<=100)的整数矩阵上找一条高度严格递减的最长路。起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外。矩阵中的数均为0~100。
分析:dp[x][y]为从位置(x,y)出发的最长路。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXN][MAXN];
int pic[MAXN][MAXN];
int r, c;
bool judge(int x, int y){
return x >= 0 && x < r && y >= 0 && y < c;
}
int dfs(int x, int y){
if(dp[x][y] != -1) return dp[x][y];
int ans = 0;
for(int i = 0; i < 4; ++i){
int tmpx = x + dr[i];
int tmpy = y + dc[i];
if(judge(tmpx, tmpy) && pic[tmpx][tmpy] < pic[x][y]){
ans = Max(ans, dfs(tmpx, tmpy));
}
}
return dp[x][y] = ans + 1;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
string name;
cin >> name;
scanf("%d%d", &r, &c);
for(int i = 0; i < r; ++i){
for(int j = 0; j < c; ++j){
scanf("%d", &pic[i][j]);
}
}
memset(dp, -1, sizeof dp);
int ans = 0;
for(int i = 0; i < r; ++i){
for(int j = 0; j < c; ++j){
ans = Max(ans, dfs(i, j));
}
}
printf("%s: %d\n", name.c_str(), ans);
}
return 0;
}
UVA - 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp---记忆化搜索)的更多相关文章
- UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)
Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- UVa 10285 Longest Run on a Snowboard【记忆化搜索】
题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...
- UVa 10285 - Longest Run on a Snowboard
称号:给你一个二维矩阵,找到一个点.每一个可以移动到的位置相邻的上下,求最长单调路径. 分析:贪婪,dp.搜索. 这个问题是一个小样本,我们该怎么办. 这里使用贪心算法: 首先.将全部点依照权值排序( ...
- UVA - 10285 Longest Run on a Snowboard (线性DP)
思路:d[x][y]表示以(x, y)作为起点能得到的最长递减序列,转移方程d[x][y] = max(d[px][py] + 1),此处(px, py)是它的相邻位置并且该位置的值小于(x, y)处 ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVa 10599【lis dp,记忆化搜索】
UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...
随机推荐
- 洛谷P2296 寻找道路
\(\Large\textbf{Description:} \large {在有向图 G 中,每条边的长度均为 1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:}\) \ ...
- lucene实践 - 索引维护、多域查询、高亮显示
之前的博客搜索栏用的是 sql 模糊查询进行查找,最近学完lucene,要学以致用啊,就把sql搜索给替换下来吧 中间遇到一些问题,也是学过程中没有提到的,所以说,还是实践出真知啊. lucene分开 ...
- Day6 - C - Count HYSBZ - 1452 /1452: [JSOI2009]Count
Description 一个N*M的方格,初始时每个格子有一个整数权值,接下来每次有2个操作: 改变一个格子的权值 求一个子矩阵中某个特定权值出现的个数 Input 每一行有两个数字N,M 接下来 ...
- C语言循环
C 练习实例1 #include<stdio.h> int main() { int i,j,k; printf("\n"); //此处巧妙的利用循环次数和四个相等的关 ...
- PHP时间格式
date 用法: date(格式,[时间]); 如果没有时间参数,则使用当前时间.格式是一个字符串,其中以下字符有特殊意义: Y - 年,四位数字; 如: "1999" y - 年 ...
- 微信小程序提示:https://api.map.baidu.com 不在以下 request 合法域名列表中
如果你想利用百度地图API定位来获得当前位置,但却出现了如标题所示问题,那么请接着看: 1.首先我们需要在百度地图开放平台(https://lbs.baidu.com/apiconsole/key?a ...
- 循环指令 LOOP
循环程序: 如果需要重复执行若干次同样任务.用循环执行 循环指令: LOOP <跳转标号> 用累加器的低字做循环计数器 每次执行LOOP 指令的时候,累加器的低字减去1 若减去后 非零 , ...
- ArrayList与LindedList区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList ...
- maven集成SSM项目,jetty部署运行——搭建maven项目部署jetty试运行(一)
今天闲来没事采用maven集成一个SSM框架来复习复习,下面开始我的复习之旅,慢慢来,不着急,哈哈,不忙时候敲两下,整起来. 工具为Eclipse,首先需要建立一个maven工程,file右键new- ...
- nginx的日志切换
#touch /usr/local/nginx/sbin/cut_nginx_log.sh #chmod 755 /usr/local/nginx/sbin/cut_nginx_log.sh 下面是 ...