点击打开链接
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
Input
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 9
Sample Output
25
题解:
1,
直接dfs过不去,你必须吧搜索过的点的最大下滑值保存下来,这样的话之后再走到该点即可直接返回,
代码如下:
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn=105;
int n,m;
int ma[maxn][maxn];
int dp[maxn][maxn];
int dfs(int x,int y,int h){
if(h<=ma[x][y]) return 0;
if(x<1||x>n||y<1||y>m) return 0;
if(dp[x][y]>=0) return dp[x][y];
dp[x][y]=max(max(dfs(x+1,y,ma[x][y]),dfs(x-1,y,ma[x][y])),max(dfs(x,y+1,ma[x][y]),dfs(x,y-1,ma[x][y])))+1;
return dp[x][y];
}
int main(){
memset(dp,-1,sizeof(dp));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
scanf("%d",&ma[i][j]);
int ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
int s=dfs(i,j,999999);
ans=max(ans,s);
}
}
printf("%d\n",ans);
return 0;
}
2. 直接dp,
把所有点记录他的位置,按照高度排序。
然后从低到高依次枚举,每次从后向前枚举当前可行的最大下滑高度。
610ms 很危险。
代码如下:
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn=105;
int dp[maxn*maxn];
int n,m;
struct node{
int x,y;
int h;
}p[maxn*maxn];
bool cmp(node a,node b){
return a.h=1;--j){
if(p[i].h>p[j].h){
if((abs(p[i].x-p[j].x)==1&&p[i].y==p[j].y)||(p[i].x==p[j].x&&abs(p[i].y-p[j].y)==1)){
dp[i]=max(dp[i],dp[j]+1);
}
}
}//printf("%d ",dp[i]);
ans=max(ans,dp[i]);
}
printf("%d\n",ans+1);
}
return 0;
}
3.
还是dp ,方法2 的一点优化,
其实枚举到该点之后,不用枚举所有比它低的能不能走,
直接枚举他能走的,能不能更新,这样和方法一复杂度一样。
15ms
代码如下:
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
const int maxn=105;
int dp[maxn][maxn];
int ma[maxn][maxn];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int n,m;
struct node{
int x,y;
int h;
}p[maxn*maxn];
bool cmp(node a,node b){
return a.hn||yi<1||yi>m) continue;
if(ma[tx][ty]>ma[xi][yi])
dp[tx][ty]=max(dp[tx][ty],dp[xi][yi]+1);
}
ans=max(ans,dp[tx][ty]);
}
printf("%d\n",ans+1);
}
return 0;
}