注意此题起点不一定是最高点:
如图:
暴搜(核心代码):
int ans=0;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,-1,1};
viod dfs(int x,int y,int step){
for(int i=0;i<4;i++){
int tx=x+dx[i];
int ty=y+dy[i];
if(tx >= 0 and tx < r and ty >= 0 and ty < c and a[x][y] > a[tx][ty]){
dfs(tx,ty.step+1);
}
}
}
int main(){
int m,n;
cin>>m>>n;
for(int i =0;i<n;i++){
for(int j =0;j<m;j++){
dfs(i,j,1)
}
}
return 0;
}
你们肯定复制了这段代码,但是会TLE,所以,
可以把这道题拆成子问题
如:
但是要记录子问题那就要用到我的大(小)作:记忆化搜索
记忆化核心代码:
int f[105][105];
int ans=0;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,-1,1};
int dfs(int x,int y){ //传递信息
int res=1;
if(f[x][y]>0)
return f[x][y];
for(int i=0;i<4;i++){
int tx=x+dx[i];
int ty=y+dy[i];
if(tx >= 0 and tx < r and ty >= 0 and ty < c and a[x][y] > a[tx][ty]){
res=max(dfs(tx,ty)+1,res);
}
}
f[x][y]=res;
return res;
}
int main(){
int m,n;
cin>>m>>n;
for(int i =0;i<n;i++){
for(int j =0;j<m;j++){
ans=max(ans,dfs(i,j))
}
}
return 0;
}