UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事。。。
Code
/**
* UVa
* Problem#10285
* Accepted
* Time:0ms
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<ctime>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline boolean readInteger(T& u) {
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-') {
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
u *= aFlag;
ungetc(x, stdin);
return true;
} template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int rows, int lines):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
#define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows) int T;
char name[];
int n, m;
Matrix<int> h; inline void init() {
scanf("%s", name);
readInteger(n);
readInteger(m);
h = Matrix<int>(n, m);
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
readInteger(h[i][j]);
}
}
} const int mov[][] = {{, , , -}, {, , -, }};
boolean isExceeded(int x, int y) {
if(x < || x >= n) return true;
if(y < || y >= m) return true;
return false;
} Matrix<int> f;
int dfs(int x, int y) {
if(f[x][y]) return f[x][y];
for(int i = , ret; i < ; i++) {
int x0 = x + mov[][i], y0 = y + mov[][i];
if(isExceeded(x0, y0)) continue;
if(h[x0][y0] >= h[x][y]) continue;
ret = dfs(x0, y0);
smax(f[x][y], ret + );
}
if(f[x][y] == ) f[x][y] = ;
return f[x][y];
} inline void solve() {
f = Matrix<int>(n, m);
matset(f, , sizeof(int));
int res = ;
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
if(f[i][j] == )
dfs(i, j);
smax(res, f[i][j]);
}
}
printf("%s: %d\n", name, res);
delete[] f.p;
delete[] h.p;
} int main() {
readInteger(T);
while(T--) {
init();
solve();
}
return ;
}
UVa 10285 Longest Run on a Snowboard - 记忆化搜索的更多相关文章
- 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 (记忆化搜索+dp)
Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...
- 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)处 ...
- UVA - 10285 Longest Run on a Snowboard(最长的滑雪路径)(dp---记忆化搜索)
题意:在一个R*C(R, C<=100)的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩阵外.矩阵中的数均为0~100. 分析:dp[x ...
- UVA 825 Walking on the Safe Side(记忆化搜索)
Walking on the Safe Side Square City is a very easy place for people to walk around. The two-way ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
随机推荐
- 解决UITableView分割线距左边有距离的办法
首先在viewDidLoad方法中加上如下代码: if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { ...
- window10 telnet的启用
(1) window+R打开运行窗口,输入control,如图: (2) 点击类别改成大图标: 如图所示: 然后点击程序和功能. (3) 然后依次点击:启用或者关闭windows功能->teln ...
- tcp3次握手,https加密,ca认证
参考: https://baijiahao.baidu.com/s?id=1570143475599137&wfr=spider&for=pc
- android 操作SD卡上的文件
(1)说明:操作SD卡上的文件须要增加下面权限 在SD卡上创建和删除文件权限 <uses-permission android:name="android.permission.M ...
- 后缀名htm与html的区别
前者是超文本标记(Hypertext Markup) 后者是超文本标记语言(Hypertext Markup Language) 可以说 htm = html 同时,这两种都是静态网页文件的扩展名,扩 ...
- Spark性能优化(二)
资源调优 调优概述 在开发完Spark作业之后,就该为作业配置合适的资源了.Spark的资源参数,基本都可以在spark-submit命令中作为参数设置.很多Spark初学者,通常不知道该设置哪些必要 ...
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- em和px比较
1em=16px. em具有继承性. 如果定义了 body{font-size=12px;} #title{font-siez=2.6em;} 而id=title恰好在body里面,那么,id=tit ...
- Hive错误:Unable to load native-hadoop library for your platform
WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin- ...
- CSS中 Zoom属性
CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...