1 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
25
#include<stdio.h> #include<string.h> int n,m,count; int visit[101][101],map[101][101]; int f[4][2]={-1,0,0,1,1,0,0,-1}; int dfs(int p,int q) { int i,s,k; if(visit[p][q]>1) return visit[p][q]; s=map[p][q]; for(i=0;i<4;++i) if(p+f[i][0]>=1&&p+f[i][0]<=n&&q+f[i][1]>=1&&q+f[i][1]<=m) { k=map[p+f[i][0]][q+f[i][1]]; if(k<s) { count=dfs(p+f[i][0],q+f[i][1]); if(visit[p][q]<count+1) visit[p][q]=count+1; } } return visit[p][q]; } int main() { int x,max,i,j; scanf("%d",&x); while(x--) { scanf("%d%d",&n,&m); max=-1; memset(map,0,sizeof(map)); for(i=1;i<=n;++i) for(j=1;j<=m;++j) { scanf("%d",&map[i][j]); visit[i][j]=1; } for(i=1;i<=n;++i) for(j=1;j<=m;++j) { dfs(i,j); if(visit[i][j]>max) max=visit[i][j]; } printf("%d\n",max); } return 0; }