题意:在一个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---记忆化搜索)的更多相关文章

  1. UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)

    Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...

  2. UVA 10285 Longest Run on a Snowboard(记忆化搜索)

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  3. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  4. UVa 10285 Longest Run on a Snowboard【记忆化搜索】

    题意:和最长滑雪路径一样, #include<iostream> #include<cstdio> #include<cstring> #include <c ...

  5. UVa 10285 - Longest Run on a Snowboard

    称号:给你一个二维矩阵,找到一个点.每一个可以移动到的位置相邻的上下,求最长单调路径. 分析:贪婪,dp.搜索. 这个问题是一个小样本,我们该怎么办. 这里使用贪心算法: 首先.将全部点依照权值排序( ...

  6. 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)处 ...

  7. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

  8. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  9. UVa 10599【lis dp,记忆化搜索】

    UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...

随机推荐

  1. PE文件结构体-IMAGE_OPTIONAL_HEADER

    typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // WORD Magic; // 标志字, ROM 映像(0107h), ...

  2. Product of Polynomials

    题意:多项式相乘,合并同类项后输出每一项的系数. 题目链接:https://www.patest.cn/contests/pat-a-practise/1009 分析:注意合并后系数为0,这一项就不存 ...

  3. simplecheck

    环境:win10 工具:jadx .夜神模拟器.pycharm 下载好了之后加载到模拟器 输入正确的flag验证 加载到jadx.查看MainAtivity if语句进行验证是否正确,如果正确就输出y ...

  4. Java中遍历 Session 和 Request

    转: session的遍历: java.util.Enumeration e = request.getSession().getAttributeNames(); while( e.hasMoreE ...

  5. 嵊州普及Day2T2

    题意:对于n个数的数列,进行排列,求第m个大于此数列的数列. 思路:查找后2个是否逆序,若是,将后3个递归.如此运算,找后面大于此数中最小数交换,然后将后面数列顺序排列. 相对简单. 见代码: #in ...

  6. 安卓平分位置layout_weight学习记录

    weight (权重)  使用把layout_width写成0   weight 为平分剩余父容器位置 两个标签平分 <?xml version="1.0" encoding ...

  7. 小程序 scroll-view 中文字不换行问题

    问题描述:在scroll-view 中scroll-x="true"时控制文字超出显示省略号,要求如图: 但实际中会出现如文字不换行或样式错乱的问题. 横向滚动的实现如下: 超过两 ...

  8. 转载-- SQL连接查询2 外连接(左右联接查询)

    http://www.cnblogs.com/zhangqs008/archive/2010/07/02/2341196.html 外连接主要包括左连接.右连接和完整外部连接. 1)左连接:Left ...

  9. mysql IF-IFNULL和IF-ISNULL同样逻辑的运行差别

    首先,目标记录是存在的 SELECT * FROM d_device_user_bind dub WHERE dub.`uid`='222222222221' 其次, SELECT dub.uid,d ...

  10. netty权威指南学习笔记一——NIO入门(1)BIO

    公司的一些项目采用了netty框架,为了加速适应公司开发,本博主认真学习netty框架,前一段时间主要看了看书,发现编程这东西,不上手还是觉得差点什么,于是为了加深理解,深入学习,本博主还是决定多动手 ...