POJ 1088 滑雪 ( DFS+动态规划思想 )
2015-02-06 14:31
489 查看
滑雪
Description
Michael 喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 79519 | Accepted: 29581 |
Michael 喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25 代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <time.h> #include <math.h> #include <iostream> #include <string> #include <queue> #include <algorithm> #define INF 0x3f3f3f3f #define N 100+2 #define R_for(i, n) for(i=0; i<n; i++) #define r_for(i, n) for(i=1; i<=n; i++) #define D_for(i, n) for(i=n-1; i>=0; i--) #define d_for(i, n) for(i=n; i>=1; i--) using namespace std; int n, m; int map ; int dis ; int f[4][2]= {{0, -1}, {0, 1}, {-1, 0}, {1, 0} }; struct node { int x, y; } t; int ans; int DFS(int posi, int posj) { int i; if(dis[posi][posj]!=0) return dis[posi][posj]; for(i=0; i<4; i++) { int xx=posi+f[i][0]; int yy=posj+f[i][1]; if((xx>=0&&xx<n)&&(yy>=0&&yy<m) && map[xx][yy]<map[posi][posj]) { int dd=DFS(xx, yy); if(dis[posi][posj]<=dd) dis[posi][posj]=dd+1; } } return dis[posi][posj]; } int main() { int i, j; while(scanf("%d %d", &n, &m)!=EOF) { R_for(i, n) R_for(j, m) { scanf("%d", &map[i][j] ); } memset(dis, 0, sizeof(dis)); ans=-1; R_for(i, n) { R_for(j, m) { int dd=DFS(i ,j); if(dd>ans) { ans=dd; } } } printf("%d\n", ans+1); } return 0; }
相关文章推荐
- 滑雪 POJ - 1088 记忆化搜索=搜索的形式+动态规划的思想
- 动态规划 :POJ 1088 滑雪
- POJ-1088滑雪,典型的动态规划题,与NYOJ-10skiing一样,但NYOJ上时限是3s,用搜索可以过,但在POJ上就超时了~~
- POJ 1088 滑雪(动态规划经典)
- 动态规划 POJ 1088 滑雪
- POJ1088 滑雪 && NYOJ 10 skiing 经典的动态规划练习题
- poj 1088 滑雪(动态规划:记忆化搜索)
- POJ 1088--滑雪【DFS(记忆化)】
- [ACM] poj 1088 滑雪 (内存搜索DFS)
- 【DP动归】【DFS】POJ-1088 滑雪
- POJ 1088 滑雪(记忆化搜索+dfs)
- poj1088 滑雪(dfs、dp优化)
- POJ 1088 滑雪 DFS 记忆化搜索
- poj 1088 滑雪(简单dfs)
- POJ 1088 滑雪 DFS
- poj 1088 滑雪 dfs+动规
- 动态规划解决北大 ACM 1088 滑雪问题
- poj 1088滑雪------精简的dfs代码
- 动态规划入门(四)DP 基本思想 具体实现 经典题目 POJ1160 POJ1037
- poj1088 滑雪 dp记忆化搜索 dfs深度优先搜索