POJ 1088

记忆化搜索啊

或者直接对高度排序啊,这样就会先更新步数少的,后更新步数多的了,就可以直接利用先前已经得到的值了

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <vector>
 6 #define max(x, y) (x > y ? x : y)
 7 #define min(x, y) (x > y ? y : x)
 8 #define INF 0x3f3f3f3f
 9 #define MOD 1000000007
10 #define Yes printf("Yes\n")
11 #define No printf("No\n")
12 using namespace std;
13 typedef long long LL;
14 typedef pair<int, int> PII;
15
16 int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
17
18 int h, w;
19 const int maxn = 1e2 + 10;
20
21 int hei[maxn][maxn];
22 int ans[maxn][maxn];
23
24 struct node {
25     int x, y, h;
26 }map[maxn * maxn];
27
28
29 bool cmp(const node& A, const node& B) {
30     return A.h < B.h;
31 }
32
33 int main(int argc, const char * argv[]) {
34     int cnt = 0;
35     scanf("%d%d", &h, &w);
36     for (int i = 1; i <= h; i++) {
37         for (int j = 1; j <= w; j++) {
38             scanf("%d", &hei[i][j]);
39             map[cnt].x = i, map[cnt].y = j, map[cnt].h = hei[i][j];
40             cnt++;
41             ans[i][j] = 1;
42         }
43     }
44     sort(map, map  + cnt, cmp);
45     for (int i = 0; i < cnt; i++) {
46         int x = map[i].x, y = map[i].y;
47         for (int j = 0; j < 4; j++) {
48             int nx = x + dx[j], ny = y + dy[j];
49             if (hei[x][y] > hei[nx][ny]) {
50                 ans[x][y] = max(ans[x][y], ans[nx][ny] + 1);
51             }
52         }
53     }
54     int res = 0;
55     for (int i = 1; i <= h; i++) {
56         for (int j = 1; j <= w; j++) {
57             res = max(res, ans[i][j]);
58         }
59     }
60     printf("%d\n", res);
61     return 0;
62 }
时间: 2024-08-08 16:16:33

POJ 1088的相关文章

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

POJ 1088 滑雪 记忆化优化题解

本题有人写是DP,不过和DP还是有点差别的,应该主要是记忆化 Momoization 算法. 思路就是递归,然后在递归的过程把计算的结果记录起来,以便后面使用. 很经典的搜索题目,这种方法很多题目考到的. 关键还是如何把代码写清晰工整了,O(∩_∩)O~. #include <stdio.h> const int MAX_N = 101; int R, C; int arr[MAX_N][MAX_N]; int tbl[MAX_N][MAX_N]; inline int max(int a,

poj 1088 滑雪

http://poj.org/problem?id=1088 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int g[200][200]; 7 int dp[200][200]; 8 int r,c; 9 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 10 int max1=-1; 11 12

[ACM] poj 1088 滑雪 (记忆化搜索DFS)

求n*m网格内矩形的数目[ACM] poj 1088 滑雪 (记忆化搜索DFS),布布扣,bubuko.com

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

POJ 1088 滑雪 (动规)

滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 75664 Accepted: 28044 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 1

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

poj - 1088 - 滑雪(dp)

题意:一个R * C的矩阵(1 <= R,C <= 100),元素代表该点的高度h(0<=h<=10000),从任意点出发,每次只能走上.下.左.右,且将要到的高度要比原高度小,求最长路. 题目链接:http://poj.org/problem?id=1088 -->>设dp[i][j]表示从ij位置出发的最长路,则状态转移方程为: dp[x][y] = max(dp[x][y], Dp(nNewX, nNewY) + 1); 时间复杂度:O(R * C) #inclu

poj 1088 滑雪(区间dp+记忆化搜索)

题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程:由于由位置[i, j]只能向四个方向移动,所以子问题最多有四个:所以dp[i][j]为其邻域可以滑雪的最大区域长度加上从该位置滑到邻域的长度,即1: 代码如下: #include <cstdio> #include <iostream> #include <algorithm&

poj 1088 滑雪【记忆化搜索】

题目链接:http://poj.org/problem?id=1088 基础题,不讲了,饿了 吃早饭去... 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #include <map> #include <string>