2006-12-06 09:00:00 UTC

# black herring bone rca

Status: Passed
Results: 12955.5
CPU Time: 14.7399
Score: 129.561
Submitted at: 2006-12-05 23:16:10 UTC
Scored at: 2006-12-05 23:18:24 UTC

Current Rank: 2065th
Based on: black herring bone rc (diff)

Code
```function solution = solver(n)
if (redherring(n) == 116)
solution=fish(n);
return
end
solution = -ones(n);
beamlog=-ones(2*n+1,2*n+1,8,3);
for desp = 0:2
for i = [1 4 6 2 7 5 0 3]
solution = rotsolver(i,solution);
if(done || all(all(solution>=0)))
solution = max(0,solution);
return
end
end
end

function n = redherring(n)
for i = 1:5
n = mod(7+13*n,1000);
end
end

function sol = rotsolver(rot,start)
solution = ira(start,rot);
done = false;
dj = 42 + mod(n^pi,100) - 40 + 80*rand;
[r,c,s] = mb(1,0,0,rot);
if solution(1,1)<0 && r==0 && c==0
[r1,c1,s1] = mb(0,1,0,rot);
if ~(r1~=0 || c1~=0)
[r2,c2,s2] = mb(2,0,0,rot);
if ~(r2~=2 && ~(r2==0 && c2==0))
[r3,c3,s3] = mb(0,2,0,rot);
if ~(c3~=2 && ~(r3==0 && c3==0))
if(desp>=1 && r2==2 && c3==2 && s2==s3)
solution(1,1) = s2;
mb(1,0,1,rot);
elseif (desp>=2 && (r2 == 2 || c3 == 2))
mb(1,0,1,rot);
if r2 == 2 && c3==2
solution(1,1) = min(s2,s3);
else
solution(1,1) = max(s2,s3);
end
elseif(desp>=2)
mb(1,0,1,rot,n);
[r4,c4,s4] = mb(2,0,0,rot);
[r5,c5,s5] = mb(0,2,0,rot);
solution(1,1) = max(1,max(s2-s4,s3-s5));
else
sol = ra(solution,rot);
return;
end
end
end
end
end
solution(1,1) = max(0,solution(1,1));
[r,c,s] = mb(1,0,0,rot);
while(~(r==-1 && c==0))
if(r==0 && c==0)
if(desp>=1)
fnd = false;
for tc=1:n-1;
if(solution(1,tc+1)>=0)
continue;
end
[r,c,s] = mb(0,tc,0,rot);
if(~(r==0 && c==0))
solution(1,tc) = max(0,solution(1,tc));
end
if(r==0 && c==tc && (desp>=2 || s<= dj))
[r1,c1,s1] = mb(0,c+1,0,rot);
if(c+2<=n)
[r2,c2,s2] = mb(0,c+2,0,rot);
else
r2=0;c2=0;s2=0;
end
if((r1==0 && c1==0) && ...
((c+2<=n && c2==c+2 && s2==s) || ...
(desp>=2 && (c+2>n || c2==c+2 || ...
(r2==0 && c2==0 && solution(1,c+2)~=0)))));
fnd=true;
solution(1,c+1) = s;
mb(0,c+1,1,rot);
break
end
end
end
if(~fnd)
sol = ra(solution,rot);
return
end
else
sol = ra(solution,rot);
return
end
else
solution(2,c+1) = s;
if(c+1<n)
[r1,c1,s1] = mb(0,c+1,0,rot);
if(r1 == 0 && c1 == c+1)
solution(1,c+2) = s1;
mb(0,c+2,1,rot);
end
end
mb(0,c+1,1,rot);
solution(1,1:min(n,c+2)) = max(0,solution(1,1:min(n,c+2)));
end
[r,c,s] = mb(1,0,0,rot);
end
nn = n;
[r,c,s] = mb(nn,0,0,rot);
while(r==-nn && c==0 && s==0 && nn>1)
nn = nn-1;
[r,c,s] = mb(nn,0,0,rot);
end
nn = nn-1;
if(nn==n-1)
nn=n+1;
end
j = 2;
couo = -1;
couu = -1;
while j < nn-2
if nn<n
if couo == -1
couo = 0;
ro = zeros(n,1);
[rn,cn,sn] = mb(j,0,0,rot);
while rn >= 0
couo = couo + 1;
ro(cn+1) = sn;
if cn+2 > n
break
end
[rn,cn2,sn] = mb(0,cn+2,0,rot);
if (rn+cn2) == 0
cn = cn + 1;
sn = -1;
else
cn = cn2;
end
end
end
if couu == -1
couu = 0;
ru = zeros(n,1);
[rn,cn,sn] = mb(nn+1,0,0,rot);
while rn >= 0
couu = couu + 1;
ru(-cn+1) = sn;
if cn-2 < -n
break
end
[rn,cn2,sn] = mb(0,cn-2,0,rot);
if (rn+cn2) == 0
cn = cn - 1;
sn = -1;
else
cn = cn2;
end
end
end
if couo <= couu
prev = 0;
for c = 1:n
if ro(c)
if ro(c) == -1
[tmp1,tmp2,s] = mb(j,0,0,rot);
solution(j+1,c) = s;
prev = s;
else
solution(j+1,c) = ro(c)-prev;
prev = ro(c)-prev;
end
mb(0,c,1,rot);
end
end
couo = -1;
j = j+1;
else
prev = 0;
for c = 1:n
if ru(c)
if ru(c) == -1
[tmp1,tmp2,s] = mb(nn+1,0,0,rot);
solution(nn,c) = s;
prev = s;
else
solution(nn,c) = ru(c)-prev;
prev = ru(c)-prev;
end
mb(0,-c,1,rot);
end
end
couu = -1;
nn = nn-1;
end
else
[r,c,s] = mb(j,0,0,rot);
while(~(r==-j && c==0))
solution(j+1,c+1) = s;
mb(0,c+1,1,rot);
[r,c,s] = mb(j,0,0,rot);
end
j = j+1;
end
end
%for j=2:nn-3
%  [r,c,s] = mb(j,0,0,rot);
%  while(~(r==-j && c==0))
%    solution(j+1,c+1) = s;
%    mb(0,c+1,1,rot);
%    [r,c,s] = mb(j,0,0,rot);
%  end
%end
sl= lrow(nn-1,ra(solution,rot),rot,n);
solution = ira(sl,rot);
if(nn<n)
sl = lrow(n-nn+1,ra(solution,rot),mod(rot+2,4)+4*(rot>=4),n);
solution = ira(sl,rot);
end
sol = ra(solution,rot);
done = true;
end

function sol = lrow(nn,start,rot,n)
solution = ira(start,rot);
[r,c,s] = mb(nn-1,0,0,rot);
if(s)
solution(nn,c+1) = s;
while(c+2<=n && r~=-(nn-1))
sc = c+2;
[r,c,s] = mb(0,sc,0,rot);
if(~s)
if sc==n
[r1, c1, s1]=mb(1-nn,0,0,rot);
solution(nn,n)=s1;
break;
end
mb(0,sc-1,1,rot);
[r,c,s] = mb(0,sc-1,0,rot);
if(~r)
solution(nn,sc) = s-solution(nn,c-1);
else
solution(nn,sc) = s;
end
c = sc-1;
elseif(~r)
solution(nn,c+1) = s-solution(nn,sc-1);
end
end
end
sol = ra(solution,rot);
end

function a=ra(ai,r)
a=ai;
if(r>=4)
a=fliplr(a);
end
a = rot90(a,r);
end

function a=ira(ai,r)
if(r>=4)
a=fliplr(rot90(ai,-r));
else
a = rot90(ai,-r);
end
end

function [rr,cc,ss] = mb(r,c,i,rot)
if i>0
beamlog(:)=-1;
elseif beamlog(r+1+n,c+1+n,rot+1,1)~=-1
rr=beamlog(r+1+n,c+1+n,rot+1,1);
cc=beamlog(r+1+n,c+1+n,rot+1,2);
ss=beamlog(r+1+n,c+1+n,rot+1,3);
return
end
if(rot<=0)
if i > 0
[rr,cc,ss] = beam(r,c,'high');
else
[rr,cc,ss] = beam(r,c,'low');
end
elseif(rot>=4)
if(r==0)
[ri,ci,ss] = mb(0,sign(c)*(n-abs(c)+1),i,rot-4);
else
[ri,ci,ss] = mb(-r,0,i,rot-4);
end
if(ri==0 && ci==0)
rr = 0;
cc = 0;
elseif(ri==0)
rr = 0;
cc = sign(ci)*(n-abs(ci)+1);
else
rr = -ri;
cc = 0;
end
else
if(r==0)
[ri,ci,ss] = mb(sign(c)*(n-abs(c)+1),0,i,rot-1);
else
[ri,ci,ss] = mb(0,-r,i,rot-1);
end
if(ri==0 && ci==0)
rr = 0;
cc = 0;
elseif(ri==0)
rr = -ci;
cc = 0;
else
rr = 0;
cc = sign(ri)*(n-abs(ri)+1);
end
end
if i<1
beamlog(r+1+n,c+1+n,rot+1,1)=rr;
beamlog(r+1+n,c+1+n,rot+1,2)=cc;
beamlog(r+1+n,c+1+n,rot+1,3)=ss;
beamlog(rr+1+n,cc+1+n,rot+1,1)=r;
beamlog(rr+1+n,cc+1+n,rot+1,2)=c;
beamlog(rr+1+n,cc+1+n,rot+1,3)=ss;
end
end
function s=fish(n)
s=zeros(n);
s(21,:)=1;
end
end```