本帖最后由 jhq999 于 2022-6-11 19:59 编辑 #include <bits/stdc++.h>
using namespace std;
int makeareas(int (*mp)[2],int n,int x,int y,int len,int *max)
{
int i,j;
if(mp[x*n+y][0]^mp[x*n+y+1][0]&&(0==mp[x*n+y+1][1])&&(y+1<n))
{
mp[x*n+y+1][1]=1;
makeareas(mp,n,x,y+1,len+1,max);
mp[x*n+y+1][1]=0;
}
if(mp[x*n+y][0]^mp[x*n+y-1][0]&&(0==mp[x*n+y-1][1])&&(y-1>=0))
{
mp[x*n+y-1][1]=1;
makeareas(mp,n,x,y-1,len+1,max);
mp[x*n+y-1][1]=0;
}
if(mp[x*n+y][0]^mp[(x+1)*n+y][0]&&(0==mp[(x+1)*n+y][1])&&(x+1<n))
{
mp[(x+1)*n+y][1]=1;
makeareas(mp,n,x+1,y,len+1,max);
mp[(x+1)*n+y][1]=0;
}
if(mp[x*n+y][0]^mp[(x-1)*n+y][0]&&(0==mp[(x-1)*n+y][1])&&(x-1>=0))
{
mp[(x-1)*n+y][1]=1;
makeareas(mp,n,x-1,y,len+1,max);
mp[(x-1)*n+y][1]=0;
}
if(*max<len)*max=len;
return 0;
}
int main(){
ios::sync_with_stdio(0);
int n,m,i,j;
cin >> n >> m;
int (*mp)[2]=(int(*)[2])malloc(sizeof(int)*n*n*2);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>mp[i*n+j][0];
mp[i*n+j][1]=0;
}
}
int max=0;
mp[0][1]=1;
makeareas(mp,n,0,0,1,&max);
// 输入 , 因为只有 0 和 1 , 所以省空间就用了 bool 存
//makeareas();
printf("%d",max);
free(mp);
return 0;
}
|