Skiing
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:78040 |
|
Accepted:29009 |
Description
It's not surprising that Michael loves skiing because skiing is really exciting. But in order to get the speed, the slide area must be tilted down, and when you slide to the bottom, you have to go uphill again or wait for the elevator to carry you. Michael wants to know the longest landslide in a region. A region is given by a two-dimensional array. Each number in the array represents the vertex height. The following is an example.
1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
A person can slide from a certain point to one of the four adjacent points up and down, when and only when the height is reduced. In the preceding example, a slide is 24-17-16-1. Of course, 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.
Input
The first line indicates the number of rows in the region R and the number of columns C (1 <= r, C <= 100 ). Below is the R row, each row has a C integer, representing the height H, 0 <= H <= 10000.
Output
The length of the maximum output area.
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
The question is easy to tell ..
DP [I] [J] indicates the optimal solution of a path starting with an element at the position (I, j). You can enumerate each element DFS. I think it's okay to use arrays without marking .. After all, a valid path is in descending order, which means that the two points are irreversible ..
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll long longusing namespace std;const int INF = 0x3f3f3f3f;int ma[102][102],dp[102][102];int m,n,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int dfs(int x,int y){if(dp[x][y]!=-1)return dp[x][y];dp[x][y]=1;int tem=0;for(int i=0;i<4;i++){int tx=dir[i][0]+x;int ty=dir[i][1]+y;if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&ma[x][y]<ma[tx][ty])tem=max(tem,dfs(tx,ty));}dp[x][y]+=tem;return dp[x][y];}int main(){while(~scanf("%d%d",&n,&m)){for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&ma[i][j]);memset(dp,-1,sizeof(dp));int ans=-INF;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) ans=max(ans,dfs(i,j));printf("%d\n",ans);}return 0;}
Poj 1088-skiing (memory-based search)