Winner the cyclist (typ2)

Finish 2006-04-12 09:00:00 UTC

Survival of the Obfuscated !!

by David

Status: Passed
Results: 84348
CPU Time: 66.384
Score: 847.496
Submitted at: 2006-04-12 15:56:27 UTC
Scored at: 2006-04-12 18:08:33 UTC

Current Rank: 59th
Based on: Survival of the Fittest !!!! (diff)

Comments
Please login or create a profile.
Code
function mv=solver(B,nmv)
[R, C]=size(B);
mv=ones(nmv,3);
J=0;
r=rand(16,1);
if (R*C)>(10.5+(rand-0.5))*nmv
next_move_J=40;
chokeF=.847;
depthF=2.93;
rot         =1.566;
elseif (R*C)>(8.5+(rand-0.5))*nmv
next_move_J=29;
chokeF=.847;
depthF=2.93;
rot         =1.566;
elseif R*C >= 256+round((rand-0.5)*10)
next_move_J=26;
chokeF=.951;
depthF=2.8;
rot=1.4; 
else
next_move_J=26;
chokeF=0.945;
depthF=2.97;
rot         =1.46;
end
B=nthroot(B,rot);
depth=ceil(depthF*nmv/10);
flip=floor((0.66+(rand-0.5)*0.1)*nmv);
while 1
if J==flip, B=nthroot(B,2/(1+rot)); end
[P,Q,M]=Calculatemv(R,C,B);
if isempty(P),
[swapmv,B]=CheckForSwap(R,C,B);
if swapmv(1)
J=J+1;
mv(J,:)=swapmv;
if (J==nmv)
break;
end
continue
end
break
end
if J<nmv-1
[Q,pos]=sort(Q,2,'descend');
P=P(pos);
for k=1:min(numel(P),next_move_J)
new_B=ProcessMove(B,M,P(k),R);
[new_P,new_Q]=Calculatemv(R,C,new_B);
if (~isempty(new_P))
D=max(min(depth,nmv-J-3),1);
if numel(new_Q)<=D
Q(k)=Q(k)+sum(new_Q);
else
new_Q=sort(new_Q,2,'descend');
Q(k)=Q(k)+sum(new_Q(1:D));
end
end
if (Q(k) < chokeF * Q(1))
break;
end
end
end
[max_val,pos]=max(Q);
max_cell=P(pos);
J=J+1;
mv(J,:)=[mod(max_cell-1,R)+1,ceil(max_cell/R),0];
B=ProcessMove(B,M,max_cell,R);
if (J==nmv)
break;
end
end
mv=mv(1:J,:);
function [P,Q,M]=Calculatemv(R,C,B)
M=zeros(R,C);
first_row=find(any(B,2),1);
if (first_row == 1)
bk=B(1);
if bk
M(1)=1;
end
k=1;
for j=2:C
m=k;
k=k+R;
bm=bk;
bk=B(k);
if bk
M(k)=k;
if (bm == bk)
M(m)=k;
end
end
end
end
for i=max(2,first_row):R
k=i;
bk=B(k);
if (bk)
M(k)=k;
m=k-1;
if (B(m) == bk)
n=-1;
while (n ~= m)
n=m;
m=M(n);
M(n)=k;
end
end
end
for j=2:C
k=k+R;
bm=bk;
bk=B(k);
if (bk)
M(k)=k;
if (bm==bk)
M(k-R)=k;
end
if (B(k-1)==bk)
n=-1;
m=k - 1;
while (n ~= m)
n=m;
m=M(n);
M(n)=k;
end
end
end
end
end
P=zeros(1,256);
group_J=0;
group_J_matrix=zeros(R, C);
for i=R:-1:first_row
k=i+R*(C-1);
for j=1:C
if B(k)
m=M(k);
if (m == k)
group_J_matrix(k)=1;
else
while group_J_matrix(m) == 0
m=M(m);
end
M(k)=M(m);
group_block_J=group_J_matrix(m)+1;
group_J_matrix(m)=group_block_J;
if (group_block_J == 2)
group_J=group_J+1;
P(1,group_J)=m;
end
M(m)= k;
end
end
k=k - R;
end
end
P=P(1:group_J);
Q=zeros(1,group_J);
for k=1:group_J
m=P(k);
P(k)=M(m);
Q(k)=group_J_matrix(m) * B(m);
M(m)=m;
end
function B=ProcessMove(B,dj,cell,R)
y=cell;
B(y)=0;
col=ceil(y/R);
minCol=col;maxCol=col;
while dj(y)~=y
y=dj(y);
B(y)=0;
col=ceil(y/R);
minCol=min(minCol, col);
maxCol=max(maxCol, col);
end;
for i=minCol:maxCol,
k=B(B(:,i)>0,i);
B(:,i)=0;B(R-numel(k)+1:R,i)=k;
end
function [swapmv,outB]=CheckForSwap(R,C,B)
swapper=[-1 R 1 -R];
lentmp=R*C;
swapmv=zeros(1,3);
outB=B;
maxswap=0;
list=find(B~=0)';
nList=numel(list);
for k=1:nList
jj=list(k);
for swapdir=1:4
np=jj+swapper(swapdir);
if np>0 && np<=lentmp && B(np)>0 && B(np)~=B(jj)
tB=DoSwap(B,jj,np);
[s_ceil,s_value]=Calculatemv(R,C,tB);
if (~isempty(s_value))
if (max(s_value)> maxswap)
maxswap=max(s_value);
idxq=jj;idxr=swapdir;idxs=np;
end
else
for k2=max(1,k-10):min(nList,k+10)
jj2=list(k2);
for swapdir2=1:4
np2=jj2+swapper(swapdir2);
if np2>0 && np2<=lentmp && tB(np2)>0 && tB(np2)~=tB(jj2)
tB2=DoSwap(tB,jj2,np2);
[s_ceil2,s_value2,s_M2]=Calculatemv(R,C,tB2);
if (~isempty(s_value2) && max(s_value2)>maxswap)
[s_max,s_idx]=max(s_value2);
Y=s_ceil2(s_idx);
while (s_M2(Y)~=Y)
Y=s_M2(Y);
end
if (Y==np || Y==jj)
maxswap=max(s_value2);
idxq=jj;idxr=swapdir;idxs=np;
end
end
end
end
end
end
end
end
end
if (maxswap>0)
swapmv=[mod(idxq-1,R)+1,ceil(idxq/R),idxr];
outB=DoSwap(B,idxq,idxs);
end
function B=DoSwap(B,el1,el2)
B([el1,el2])=B([el2,el1]);