O(n^4)+强力剪枝优化无压力,不用二分图匹配的麻烦构图。
搜索策略:
枚举对角线——因为只有枚举对角线才能保证正方形唯一。
剪枝:
1、对于每个是'J'的点,枚举出它非严格下方的所有'J'点,不用全部枚举N^4次
2、对于每条对角线,计算出其中点tx、ty坐标与两个端点x、y坐标的差值x1,y1,x2,y2,如果tx+y1或ty+x2不是整数就判断下一个。
判断条件:
对于每条合法对角线,满足((map[tx+y1,ty+x2]='J')and(map[tx+y2,ty+x1]<>'B'))or((map[tx+y2,ty+x1]='J')and(map[tx+y1,ty+x2]<>'B'))
CODE
Program Bigsq;//By_Thispoet Const maxn=100; Var i,j,k,m,n,p,q,a,b,c,d,ans :Longint; map :Array[1..maxn,1..maxn]of Char; x1,x2,y1,y2,tx,ty :Extended; BEGIN readln(n); for i:=1 to n do begin for j:=1 to n do read(map[i,j]); readln; end; ans:=0; for i:=1 to n do for j:=1 to n do if map[i,j]<>'J' then continue else for p:=n downto i do for q:=n downto 1 do if ((sqr(p-i)+sqr(q-j))<=(ans <<1))or((p=i)and(q=j))or(map[p,q]<>'J') then continue else begin tx:=(i+p)/2;ty:=(j+q)/2; x1:=i-tx;x2:=p-tx;y1:=j-ty;y2:=q-ty; if (abs(tx+y1-round(tx+y1))<10e-6)and(abs(ty+x2-round(ty+x2))<10e-6) then begin a:=round(tx+y1);b:=round(ty+x2); c:=round(tx+y2);d:=round(ty+x1); if (a<=0)or(b<=0)or(c<=0)or(d<=0)or(a>n)or(b>n)or(c>n)or(d>n) then continue; if (map[a,b]='J')and(map[c,d]<>'B')then ans:=(sqr(p-i)+sqr(q-j))>>1 else if (map[a,b]<>'B')and(map[c,d]='J')then ans:=(sqr(p-i)+sqr(q-j))>>1; end; end; writeln(ans); END.