dp。。。开始我想的不是看成有向图用递归做,而是按高度排序,然后从小到大遍历,感觉应该能实现最优子结构的,但交上去wa。。顿时茫然了,以为自己的思路错了。。再去检查是不是最优子结构。。折腾了一会,才发现一个细节没考虑清楚:若相邻的点高度想同是不能连在一起的,改了下于是ac了。。。也许你想到的方法理论上是对的,只是实现方式不同,但如果你从来没这样用过就很有可能在细节上出问题。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 110
using namespace std;
struct node
{
int x,y,h;
bool operator <(node other)const
{
return h<other.h;
}
}s[MAX*MAX];
int dp[MAX][MAX],high[MAX][MAX],vector[4][2]={{0,-1},{0,1},{-1,0},{1,0},};
int main()
{
int t,i,j,r,c,xx,yy,maxx;
char name[100];
scanf("%d",&t);
while(t--)
{
scanf("%s",name);
scanf("%d %d",&r,&c);
memset(dp,0,MAX*MAX*4);
memset(high,-1,MAX*MAX*4);
maxx=0;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
scanf("%d",&s[(i-1)*c+j-1].h);
s[(i-1)*c+j-1].x=i,s[(i-1)*c+j-1].y=j;
}
}
//for(i=0;i<r*c;i++)
// printf("%d\n",s[i].h);
sort(s,s+r*c);
for(i=0;i<r*c;i++)
{
xx=s[i].x,yy=s[i].y,high[xx][yy]=s[i].h;
for(j=0;j<4;j++)
{
if(s[i].h>high[xx+vector[j][0]][yy+vector[j][1]])
dp[xx][yy]=max(dp[xx][yy],dp[xx+vector[j][0]][yy+vector[j][1]]+1);
}
}
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
maxx=max(maxx,dp[i][j]);
printf("%s: %d\n",name,maxx);
}
return 0;
}