2216: Ski
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 8s | 8192K | 853 | 216 | Special Test |
It is well-known that in the game of ski sportsmen slide from a higher position to a lower one, and they always desist when all the abutting (up, down, left, and right) positions are higher. In this problem, you are given a clearing to determine the longest course that can be used for skiing.
Input
Each input file contains only ONE test case, but there might be many input files. Each test case begins with two integers R (<=100) and C (<=100), representing the number of rows and columns of the clearing, which is always a rectangle indicated by a two-dimensional array here. And from the second line there follows R lines, each consisting of C integers, each of which represents the altitude of a position of the clearing, and it is not hard to infer that the larger the integer is the higher the position the integer represents is.
Output
For each test case, print the length of the longest course, the number of transitions, in a single line.
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
Notice: The beginning position of the course is haphazard.
This problem is used for contest: 31 147 148
#include<iostream>
#include<algorithm>
using namespace std;
int a[105][105],val[105][105];
int r,c;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int dfs(int x,int y)
{
if(val[x][y]!=-1) return val[x][y];
int i;
int m=0;
for(i=0;i<4;i++)
{
if(x+xx[i]<=0||x+xx[i]>r||y+yy[i]<=0||y+yy[i]>c) continue;
if(a[x][y]>a[x+xx[i]][y+yy[i]])
{
int temp=dfs(x+xx[i],y+yy[i]);
if(temp>m) m=temp;
}
}
val[x][y]=m+1;
return val[x][y];
}
int main()
{
int i,j;
while(scanf("%d%d",&r,&c)==2)
{
for(i=1;i<=r;i++) for(j=1;j<=c;j++) {scanf("%d",&a[i][j]);val[i][j]=-1;}
int maxn=1;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
int t=dfs(i,j);
if(maxn<t) maxn=t;
}
}
printf("%d/n",maxn);
}
return 0;
}