Dream_Chaser_ZYF @ 2023-02-16 21:40:59
#include <bits/stdc++.h>
using namespace std;
struct node{
int high;
int x;
int y;
};
bool cmp(node a,node b){
return a.high>b.high;
}
int maxn,r,c,a[110][110],dp[110][110],use1[4]={0,0,-1,1},use2[4]={1,-1,0,0};
node q[110*110];
int main(){
scanf("%d %d",&r,&c);
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
dp[i][j]=1;
scanf("%d",&a[i][j]);
q[j+(i-1)*r].high=a[i][j];
q[j+(i-1)*r].x=i;
q[j+(i-1)*r].y=j;
}
}
sort(q+1,q+1+(r*c),cmp);
for(int i=1;i<=r*c;i++){
int zx=q[i].x,zy=q[i].y,h=q[i].high;
for(int j=0;j<4;j++){
int hx=zx+use1[j],hy=zy+use2[j];
if(hx>=1 && hx<=r && hy>=1 && hy<=c && a[hx][hy]>h){
dp[zx][zy]=max(dp[zx][zy],dp[hx][hy]+1);
}
}
maxn=max(maxn,dp[zx][zy]);
}
cout << maxn;
return 0;
}
by Blue_Flower @ 2023-02-16 22:50:27
我表示虽然我AC了这题,但我看不懂你的思路(枚举吗)还是得记忆化搜索吧
by Blue_Flower @ 2023-02-16 22:53:27
不然AC代码给你自个琢磨琢磨(虽然也有题解)
#include<bits/stdc++.h>
using namespace std;
int n,m,ans=0;
int a[1000][1000],f[1000][1000],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
int dfs(int x,int y)
{
if(f[x][y]) return f[x][y];
int step=0;
for(int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(xx>0&&yy>0&&a[xx][yy]!=-1&&a[xx][yy]<a[x][y])
step=max(dfs(xx,yy)+1,step);
}
return f[x][y]=max(step,1);
}
int main()
{
memset(f,0,sizeof(f));
memset(a,-1,sizeof(a));
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
ans=max(dfs(i,j),ans);
cout<<ans;
}
by Dream_Chaser_ZYF @ 2023-02-25 21:18:12
已支付一个关注
by Dream_Chaser_ZYF @ 2023-07-04 17:32:13
额