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 5
16 17 18) 19 6
15 24 25) 20 7
14 23 22) 21 8
13 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
Redo the problem that was done to n years ago, the original code is this
1#include <stdio.h>2#include <stdlib.h>3 #defineN 1114 #defineMAX (x, y) x>y?x:y5 inthuaxue[111][111]; 6 intlen[111][111];7 8 intGetmax (intIintJintRintCinthuaxue[][111],intlen[][111])9 {Ten inttemp=1; One A if(I-1>=0&&huaxue[i-1][j]<Huaxue[i][j]) -temp=1+ (len[i][j]==0? Getmax (I1, J,r,c,huaxue,len): len[i-1][j]); - if(i+1<=r-1&&huaxue[i+1][j]<Huaxue[i][j]) thetemp= (MAX (temp,1+ (len[i][j]==0? Getmax (i+1, J,r,c,huaxue,len): len[i+1][j] )); - if(J-1>=0&&huaxue[i][j-1]<Huaxue[i][j]) -temp= (MAX (temp,1+ (len[i][j]==0? Getmax (i,j-1, R,c,huaxue,len): len[i][j-1]) ) ); - if(j+1<=c-1&&huaxue[i][j+1]<Huaxue[i][j]) +temp= (MAX (temp,1+ (len[i][j]==0? Getmax (i,j+1, R,c,huaxue,len): len[i][j+1]) ) ) ; - +len[i][j]=temp; A at returntemp; - } - Main () - { - intR,c,i,j,result; - //freopen ("Input.txt", "R", stdin); inscanf"%d%d",&r,&C); - to for(i=0; i<r;i++) + { - for(j=0; j<c;j++) thescanf"%d",&huaxue[i][j]); *len[i][j]=0; $ }Panax Notoginsengresult=0; - the for(i=0; i<r;i++) + for(j=0; j<c;j++) A theresult=(MAX (Result,getmax (I,j,r,c,huaxue,len))); + - $printf"%d\n", result); $ return 0; -}
This is the code for this submission.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4 5 intheight[102][102];6 intcnt[102][102];7 intdir[][4] = {{0,1},{0,-1},{1,0},{-1,0}};8 intr,c;9 Ten intDfsintXinty) { One if(Cnt[x][y]! =0) { A returnCnt[x][y]; - } - intTmpmax =1; the for(inti =0; I <4; i++) { - intTMPX = x + dir[i][0]; - intTmpy = y + dir[i][1]; - intTMPC =0; + if(Tmpx >=0&& Tmpy >=0&& tmpx < R && Tmpy < C && Height[tmpx][tmpy] <Height[x][y]) { -TMPC =1+dfs (TMPX, tmpy); + } A if(Tmpmax <TMPC) { atTmpmax =TMPC; - } - } -Cnt[x][y] =Tmpmax; - returnTmpmax; - } in - intMainintargcChar Const*argv[]) to { +Freopen ("Input.txt","R", stdin); - while(SCANF ("%d%d", &r,&c)! =EOF) { thememset (CNT,0,sizeof(CNT)); * for(inti =0; I < R; i++) { $ for(intj =0; J < C; J + +) {Panax Notoginsengscanf"%d",&height[i][j]); - } the } + intMax =1; A for(inti =0; I < R; i++) { the for(intj =0; J < C; J + +) { + intTMP =DFS (I,J); - if(Max <tmp) { $Max =tmp; $ } - } - } theprintf"%d\n", max); - }Wuyi return 0; the}
The first commit did not pass because the 18th line TMPC did not initialize!
POJ 1088 Ski