Description
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 9Sample Output
25Source
SHTSC 2002[Submit] [Go Back] [Status] [Discuss]
//
// 5021415 11410 1088 Runtime Error C++ 963B 2009-04-21 18:25:30 // 5021906 11410 1088 Accepted 300K 0MS C++ 1156B 2009-04-21 20:01:06 #include < iostream > #define MAX 110 using namespace std; int dp[MAX][MAX],map[MAX][MAX]; int n,m,a[ 4 ][ 2 ] = {{ 1 , 0 },{ - 1 , 0 },{ 0 , 1 },{ 0 , - 1 }}; void Init() { int i,j; for (i = 0 ;i < m;i ++ ) for (j = 0 ;j < n;j ++ ) { scanf( " %d " , & map[i][j]); dp[i][j] =- 1 ; } } bool Bound( int x, int y) { if (x >= 0 && y >= 0 && x < m && y < n) return true ; else return false ; } int DFS( int x, int y) { int maxt,i,sx,sy,temp; if (dp[x][y] !=- 1 ) return dp[x][y]; else { maxt = 0 ; // 这样实际上也把底找到了,也就是这个数四周没有比它小的点 for (i = 0 ;i < 4 ;i ++ ) // 在四个方向中找一个最长的序列 { sx = x + a[i][ 0 ]; sy = y + a[i][ 1 ]; if (Bound(sx,sy) && map[sx][sy] < map[x][y]) { temp = DFS(sx,sy); if (temp > maxt) maxt = temp; } } dp[x][y] = maxt + 1 ; return dp[x][y]; } } int main() { int i,j,mm, tm; while (scanf( " %d%d " , & m, & n) != EOF) { Init(); mm =- 2 ; for (i = 0 ;i < m;i ++ ) for (j = 0 ;j < n;j ++ ) { DFS(i,j); if (dp[i][j] > mm) { mm = dp[i][j]; } } /* for(i=0;i<m;i++) for(j=0;j<n;j++) if(dp[i][j]>mm) mm=dp[i][j]; */ printf( " %d\n " ,mm); } return 0 ; }转载于:https://www.cnblogs.com/forever4444/archive/2009/05/13/1456257.html
相关资源:数据结构—成绩单生成器