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 ...
随机推荐
- HDU - 5969 最大的位或 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...
- 判断tableViewCell是否在可视区
1.- (NSArray *)visibleCells; UITableview 的方法,这个最直接,返回一个UITableviewcell的数组. 对于自定义的cell,之后的处理可能会稍微复杂. ...
- an upstream response is buffered to a temporary file
an upstream response is buffered to a temporary file
- SCRAM
RFC 5802 - Salted Challenge Response Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms ht ...
- is_link
'Symbolic Link' to File1 content containing path to File1'Hard Link' to File1 content containing Fi ...
- Python 字典 setdefault() 方法
描述 Python 字典 setdefault() 方法和 get() 方法类似,返回指定键的值,如果键不在字典中,将会添加键并将值设置为一个指定值,默认为None. get() 和 setdefau ...
- elasticsearch数据转移,elasticdump的安装使用
模拟: 将本地的my_index的products的一条document转移到http://192.168.111.130的一个es服务器上. (一)安装elasticdump 先安装node.js, ...
- vue中 用媒体查询 空置根节点字体大小
在index.html中 <style> @media all and (min-width: 0px) { html{font-size: 20px;}/* 12*4.6 */ } @m ...
- JS模块化编程(四)--require应用
获取&使用require.js 下载最新版的Require.JS.下载之后,把它放在项目的脚本文件夹下,比如 js 文件夹下,项目结构看上去应该是: 要充分使用Require.JS,将html ...
- 冒泡排序快速版(C)
冒泡排序C语言版:在每轮排序中检查时候有元素位置交换,如果无交换,说明数组元素已经有序,无需继续排序 #include <stdio.h> #include <stdlib.h> ...