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

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 ;
}
上一篇:[转载]使用QTP测试Windows对象


下一篇:【UVA 437】The Tower of Babylon(记忆化搜索写法)