uva10285 - Longest Run on a Snowboard(记忆化搜索)

题目:uva10285 - Longest Run on a Snowboard(记忆化搜索)

题目大意:给出N * N的矩阵,要求找到一条路径,路径上的值是递减的,求这样的路径的最长长度。

解题思路:记忆话搜索。因为要求最长的路径那么就需要将所有的这样的路径找出,但是直接dfs会超时的。对于同一个位置,从这个点出发的最长路径长度是固定的。所以在找的时候就要将这个位置的最长路径算出来并且保存下来。一开始还担心会走回头路就是一直兜圈子。但是后面发现能往右走就不可能在从右边的那个位置走回去,因为路径上的值是要求递减的。

dp【x】【y】代表从x,y这个位置开始能够走的最长的路径长度。

代码:

#include <cstdio>
#include <cstring>

const int N = 105;
const int M = 4;
const int dir[M][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

int G[N][N];
int dp[N][N];
char name[N * 10];
int r, c;

int Max (const int a, const int b) { return a > b? a: b; }

int DP (int x, int y) {

	int& ans = dp[x][y];
	if (ans != -1)
		return ans;
	int newx, newy;
	for (int i = 0;  i < M; i++) {
		newx = x + dir[i][0];
		newy = y + dir[i][1];
		if (newx < 0 || newx >= r || newy < 0 || newy >= c)
			continue;
		if (G[x][y] > G[newx][newy])
			ans = Max (ans, DP (newx, newy) + 1);  //从这个位置开始四个方向的最长长度取最大值
	}
	if (ans == -1)      //这个点走不出去
		ans = 1;
	return ans;
}

int main () {

	int t;
	scanf ("%d", &t);
	while (t--) {

		scanf ("%s%d%d", name, &r, &c);
		for (int i = 0; i < r; i++)
			for (int j = 0; j < c; j++)
				scanf ("%d", &G[i][j]);

		memset (dp, -1, sizeof (dp));
		int ans = -1;
		for (int i = 0; i < r; i++) 从每个位置开始走都是有可能的取得最长路径
			for (int j = 0; j < c; j++)
				ans = Max (ans, DP(i, j));
		printf ("%s: %d\n", name, ans);
	}
	return 0;
}

uva10285 - Longest Run on a Snowboard(记忆化搜索),布布扣,bubuko.com

时间: 2024-10-05 11:34:57

uva10285 - Longest Run on a Snowboard(记忆化搜索)的相关文章

uva10285 Longest Run on a Snowboard(dp之记忆化搜索 )

10285 Longest Run on a Snowboard Michael likes snowboarding. That's not very surprising, since snowboarding is really great. The bad thing is that in order to gain speed, the area must slide downwards. Another disadvantage is that when you've reached

UVA10285 Longest Run on a Snowboard

题解: 记忆化搜索入门题 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,y,sizeof(x)) #define SZ(x) (

UVA-10285 Longest Run on a Snowboard (递推)

题目大意:滑雪.给一个二维数组,找出最长的连续下降序列的长度. 题目分析:定义dp(i,j)表示以a[i][j]结尾的最长连续下降序列的长度,则dp(i,j)=max(dp(i-1,j),dp(i+1,j),dp(i,j-1),dp(i,j+1)),重复这个DP过程滚动更新dp数组100次即可(我专门做了一下测试,更新次数在42次及以上时都能AC). 代码如下: # include<iostream> # include<cstdio> # include<string>

uva 10285 Longest Run on a Snowboard (记忆化搜索)

uva 10285 Longest Run on a Snowboard 题目大意:给出一张n*m的雪地地图,每格标注的是该点的高度.从地势高的地方可以滑到地势低的地方(只能上下左右滑),问最长的滑雪距离. 解题思路:逐一访问各点,若该点没有被访问过,则进行DFS找出该点为滑雪起始点的最长滑雪距离,用dp数组记录,若该点已被访问过,则返回其对应的dp数组记录的值. #include <cstdio> #include <cstring> #include <algorithm

UVa 10285 Longest Run on a Snowboard(DP 二维最长递减子序列)

题意  输入一个城市的滑雪地图  你可以从高的地方滑到伤下左右低的地方  求这个城市的最长滑雪线路长度   即在一个矩阵中找出最长递减连续序列 令d[i][j]为以格子map(i,j)为起点的最长序列   则有状态转移方程d[i][j]=max{d[a][b]}+1  a,b为与i,j相邻且值比i,j小的所有点 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #def

HDU 1331 Function Run Fun(记忆化搜索)

Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:w(20, 20, 20)

HDU 1331 Function Run Fun (基础记忆化搜索)

Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2602    Accepted Submission(s): 1263 Problem Description We all love recursion! Don't we? Consider a three-parameter recursive f

POJ 1579 Function Run Fun 记忆化搜索

Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns: 1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns: w(20, 20, 20) if a &

POJ 1579 Function Run Fun 【记忆化搜索入门】

题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20560   Accepted: 10325 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <=