#include<iostream>
#include<stdio.h>
using namespace std;
#define MAX 1010
//#define MAX 10
int map[MAX][MAX];int n,m;
int cl[MAX][MAX];//向左走能走的距离//-1表示没有访问,-2表示无法到达。
int cr[MAX][MAX];//向右走能走的距离
int result[MAX][MAX];
class Point
{
public:
int x;
int y;
};
void init()
{
int i,j;
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
{
map[i][j]=0;
cl[i][j]=-1;
cr[i][j]=-1;
result[i][j]=0;
}
}
int over(Point p)
{
if(p.x<0||p.y<0)
return 1;
if(p.x>=m||p.y>=n)
return 1;
return 0;
}
int dpl(Point p)
{
if(over(p))
{
return 0;
}
if(map[p.x][p.y]==0)//如果这个点不合条件
{
cl[p.x][p.y]=0;
return cl[p.x][p.y];
}
if(cl[p.x][p.y]>=0)//如果访问过
{
return cl[p.x][p.y];
}
Point cs;cs.x=p.x;cs.y=p.y;
//向左走
cs.x--;cs.y--;
int yiqian=dpl(cs);//把以前的值放到这个地方
//如果能到达
cl[p.x][p.y]=yiqian+1;
return cl[p.x][p.y];
}
int dpr(Point p)
{
if(over(p))
{
return 0;
}
if(map[p.x][p.y]==0)//如果这个点不合条件
{
cr[p.x][p.y]=0;
return cr[p.x][p.y];
}
if(cr[p.x][p.y]>=0)//如果访问过
{
return cr[p.x][p.y];
}
Point cs;cs.x=p.x;cs.y=p.y;
//向左走
cs.x++;cs.y--;
int yiqian=dpr(cs);//把以前的值放到这个地方
//如果能到达
cr[p.x][p.y]=yiqian+1;
return cr[p.x][p.y];
}
int main()
{
//freopen("in.txt","r",stdin);
int T;scanf("%d",&T);
while(T--)
{
//初始化部分
init();
//输入部分
scanf("%d %d",&n,&m);
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&map[j][i]);
}
}
//开始处理
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
Point start;start.x=j;start.y=i;
dpl(start);
dpr(start);
}
}
//输出部分
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(cr[j][i]==1||cl[j][i]==1||cr[j][i]==0)//单边或者为0
{
result[j][i]=0;continue;
}
result[j][i]=cr[j][i]+cl[j][i]-1;
}
}
int tmax=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
int fmax=result[j][i];
if(fmax>tmax)
{
tmax=fmax;
}
}
}
if(tmax==0)
cout<<"-1"<<endl;
else
{
cout<<tmax<<endl;
}
}
return 0;
}
触宝cootek杯 中科大校赛 E (ac)(原来是dpr写成啦dpl)(n^2级别的直接dp)
最新推荐文章于 2023-02-25 19:43:32 发布