Ski
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 83489 |
|
Accepted: 31234 |
Description
It's not surprising that Michael likes to ski, because skiing is really exciting. But to get the speed, the slippery area must tilt downward, and when you slide to the bottom, you have to go up the slope again or wait for the lift to load you. Michael wants to know the longest bottom landslide in a region. The area is given by a two-dimensional array. Each digit of the array represents the height of the point. Here is an example
1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
One can slide from one point to the next four points adjacent to one another, when and only if the height decreases. In the example above, a sliding landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 longer. In fact, this is the longest one.
Input
The first line of input represents the number of rows in the range R and the number of columns C (1 <= r,c <= 100). The following are the r lines, each with a C integer representing a height of h,0<=h<=10000.
Output
The length of the longest region of the output.
Sample Input
5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Sample Output
25
100*100 Matrix, if the bare Dfs is likely to time out, so you can use the memory search method, Dp[i][j] represents the longest path currently reaching [I,j], at the same time as DFS, update dp[i][j]. The idea is simple in general.
#include <stack> #include <queue> #include <cmath> #include <cstdio> #include <cstring># include<iostream> #include <algorithm> #pragma commment (linker, "/stack:102400000 102400000") #define Mset0 (t) memset (t,0,sizeof (t)) #define Lson A,b,l,mid,cur<<1#define Rson a,b,mid+1,r,cur<<1|1using namespace Std;const double Eps=1e-6;const int maxn=110;int vis[maxn][maxn],dp[maxn][maxn],num[maxn][maxn],n,m,dir1[4 ]={0,0,-1,1},dir2[4]={-1,1,0,0};//dp[i][j] represents the longest path to [i,j] void dfs (int x,int y) {for (int i=0;i<4;i++) {int Xxx=x+dir1[i]; int yyy=y+dir2[i]; if (Xxx>=1&&xxx<=n&&yyy>=1&&yyy<=m&&num[xxx][yyy]>num[x][y]) { if (dp[x][y]+1>dp[xxx][yyy])//If go to [xxx,yyy] after reaching [xxx,yyy] the longest path will grow, walk up to make sense {DP[XXX][YYY]=DP [x] [Y]+1; DFS (XXX,YYY); }}}}int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//OnLine_judge while (scanf ("%d%d", &n,&m)!=eof) {for (Int. i=1;i<=n;i++) for (int j=1;j<=m; J + +) {scanf ("%d", &num[i][j]); Dp[i][j]=1; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) DFS (I,J); int ans=0; for (int. i=1;i<=n;i++) for (int j=1;j<=m;j++) Ans=max (Ans,dp[i][j]); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1088 Ski (memory search +dfs)