Background: Did not consider the elevation can also be 0 and negative, so has been wrong for a long time.
Idea: dfs+ Memory search + recursion.
Learning: Memory Search.
#include <iostream> #include <cstring>using namespace Std;int area[52][52],memory[52][52];int m,n,max_ Time,k;int judge (int i1,int j1,int I,int j) {if (area[i1][j1]<area[i][j]&&i1>0&&j1>0& &I1<=N&&J1<=M) return 1; else return 0;} void Dfs (int i,int j,int time) {bool Ok=false; if (Memory[i][j]) {time+=memory[i][j]; K= ((k<time) time:k); Max_time= ((max_time<time) time:max_time); Return } if (judge (i+1,j,i,j)) {DFS (i+1,j,time+1); ok=true;} if (judge (i,j+1,i,j)) {DFS (i,j+1,time+1); ok=true;} if (judge (i-1,j,i,j)) {DFS (i-1,j,time+1); ok=true;} if (judge (i,j-1,i,j)) {DFS (i,j-1,time+1); ok=true;} if (!ok) k= ((k<time)? time:k);} int main (void) {while (cin>>n>>m&&n&&m) {memset (area,0,sizeof (area)); memset (memory,0,sizeof (memory)); max_time=0; int i,j; for (i=1;i<=n;i++) for (j=1;j<=m;j++) cin>>area[i][j]; for (i=1;i<=n;i++) for (j=1;j<=m;j++) {k=0; DFS (i,j,0); Memory[i][j]=k; } cout<<max_time<<endl; } return 0;}
soj1084 Ski Race