Winner the cyclist (typ2)

2006-04-12 09:00:00 UTC

# I said to myself not to do this kind of things.....

Status: Passed
Results: 89206
CPU Time: 61.1338
Score: 894.143
Submitted at: 2006-04-10 15:30:52 UTC
Scored at: 2006-04-10 15:36:47 UTC

Current Rank: 1505th
Based on: I said to myself not to do this kind of things.... (diff)
Basis for: SHsr03 (diff)

Code
```function move=solver(B,N)
if numel(B) < 520, B = B.^0.62461;else B=B.^0.63287;end
a80 = size(B,1);move = ones(N,3);count=1;
while count<=N
a28=B;a36=B;[a24,a22,a75] = findclusters(B);
djones=max(27,32.0000001-floor(0.99*(N-(1:N))));
while ~isempty(a22)
[a22,pos]=sort(a22,2,'descend');a24=a24(pos);a45=a22(1);pos=1;a23=true;if count<N
if numel(a24)==1
a28 = a21(B,a75,a24);else
a23=false;lim=0.62202 * a22(1);for k=1:min(numel(a24),djones(count))
a27 = a21(B,a75,a24(k));[a14,a13,a34] = findclusters(a27);if ~isempty(a14)
a33 = max(a13);a22(k) = a22(k) + a33;if a22(k)>a45
b45=a45;a45=a22(k);pos=k;a28=a27;lim=0.62202 * a22(1);a17=a14;a15=a13;a36=a34;if a45>2.6*b45
break;end
end
elseif k==1
a17=a14;a15=a13;a36=a34;a28=a27;end
if a22(k) < lim
break;end
end % for
end	% not e1.73ty
end	% not last
a31 = a24(pos);move(count,:) = [mod(a31-1,a80)+1,ceil(a31/a80),0];if count==N
count = count+1;break;end;count = count+1;
B=a28;if a23
[a24,a22,a75] = findclusters(B);else
a24=a17;a22=a15;a75=a36;end
end
if count<=N
[blk,num] = mode(B(find(B~=0)));if (blk && (num>1))
a65 = false;while count<=N
if (a65)
break;else
[a65,a35,B] = a25(B,blk,num);move(count,:) = a35;end;count = count+1;
end
end
end
end
function [a65,a35,B] = a25(B,a30,a18)
a65 = false;a35 =ones(1,3);[a78,a67] = size(B);[a80,a70] = find(B == a30);[a71,a68] = mode(a70);if (a68 > 1)
for (k=a78:-1:1)
if (B(k,a71) == a30)
a35 = [k a71 1];B(k,a71) = B(k-1,a71);B(k-1,a71) = a30;if (B(k-2,a71) == a30)
a65 = true;end
break;end
end
else
[a81,a79] = mode(a80);if (a79 > 1)
a69 = a70(find(a80(:)==a81));a29 = 1;a44 = size(a69);k = a69(a29);while (k<a69(a44))
k = k + 1;if (B(a81*k) == 0)
a29 = a29 + 1;k = a69(a29);if (a29 == a44)
break;end
else
if (k == a69(a29+1))
a35 = [a81 a69(a29) 2];B(a81,a69(a29)) = B(a81,a69(a29)+1)
B(a81,a69(a29)+1) = a30;if (B(a81,a69(a29)+2) == a30)
a65 = true;end
break;end
end
end
end
end
function [a24,a22,a75] = findclusters(B)
[a80,a70] = size(B);a75 = ones(a80,a70);a26 = find(any(B,2),1);if a26 == 1
k=1;bk=B(1);for j=2:a70
m = k;k = k + a80;bm=bk;bk=B(k);if bk
a75(k) = k;if bm == bk
a75(m) = k;end
end
end
end
for i = max(2,a26):a80
k=i;bk=B(k);if bk
a75(k) = k;m = k-1;if B(m) == bk
n = -1;while (n ~= m)
n = m;m = a75(n);a75(n) = k;end
end
end
for j=2:a70
m = k;k = k + a80; % move a77 one a51
bm=bk;bk=B(k);if bk
a75(k) = k;if bm==bk
a75(m) = k;end
m = k - 1;if B(m)==bk
n = -1;while (n ~= m)
n = m;m = a75(n);a75(n) = k;end
end
end
end
end
np = 1024;a24 = zeros(1,256);a20 = 0;for i=a80:-1:a26
k = i + a80*(a70-1);for j=a70:-1:1
if B(k)
m = a75(k);if m == k
a75(k) = a75(k) + np;else
while a75(m)<np
m = a75(m);end
a75(k) = mod(a75(m),np);a11 = floor(a75(m)/np) + 1;if a11 == 2
a20 = a20 + 1;a24(a20) = m;end
a75(m)= k + a11 * np;end
end
k = k - a80;end
end
a24 = a24(1:a20);a22 = zeros(1,a20);
for k = 1:a20
m = a24(k);t=floor(a75(m)/np);a24(k) = a75(m)-t*np;
a22(k) = (t * B(m))^1.73;a75(m) = m;
end;
function B=a21(B,dj,cell)
R=size(B,1);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;```