Winner GUO (asdf1)

2004-11-10 09:00:00 UTC

# Strange Solver v0.92

Status: Passed
Results: 293017
CPU Time: 68.8989
Score: 294.245
Submitted at: 2004-11-04 09:04:18 UTC
Scored at: 2004-11-04 09:06:38 UTC

Current Rank: 1147th

Roman Akulov
04 Nov 2004
Code improved and fixed a bug of making some useless steps.
Code
```function mv = solver(Ai,Af,w)

n_blk = length(w);
[n_row,n_col] = size(Ai);
% 1 RIGHT, 2 DOWN, 3 LEFT, 4 UP
I = [0  1  0 -1];
J = [1  0 -1  0];

Ci = zeros(n_blk,2);
Cf = Ci;

for row = 1:n_row,
for col = 1:n_col,
b = Ai(row,col);
if b > 0,
Ci(b,:) = [row,col];
end
b = Af(row,col);
if b > 0,
Cf(b,:) = [row,col];
end
end
end

A = Ai;
C = Ci;
mv = zeros(0,2);
Def = sum(sum(abs(Cf - C)));

MA = A;
MC = C;
Mmv = mv;
MDef = Def;

[sw,ind] = sort(w);
ind = ind';

while 1,

while 1,
n_move = 0;
for b = ind,
while 1,
dir = sign(Cf(b,:) - C(b,:));
if dir == [0 0],
break
end
r = C(b,1);
c = C(b,2);

if rand > 0.5,
if (dir(1) ~= 0) && (A(r+dir(1),c) == 0),
dir = 3 - dir(1);
else
if (dir(2) ~= 0) && (A(r,c+dir(2)) == 0),
dir = 2 - dir(2);
else
break
end
end
else
if (dir(2) ~= 0) && (A(r,c+dir(2)) == 0),
dir = 2 - dir(2);
else
if (dir(1) ~= 0) && (A(r+dir(1),c) == 0),
dir = 3 - dir(1);
else
break
end
end
end
n_move = n_move + 1;
nr = r + I(dir);
nc = c + J(dir);
A(nr,nc) = b;
A(r,c) = 0;
C(b,:) = [nr,nc];
mv = [mv;[b,dir]];
end
end
if n_move == 0,
break;
end
end

Def = sum(sum(abs(Cf - C)));
if Def == 0,
break;
end
if Def >= MDef,
A = MA;
C = MC;
mv = Mmv;
Def = MDef;
else
MA = A;
MC = C;
Mmv = mv;
MDef = Def;
end

for j = 1:n_blk,
b = ceil(rand*n_blk);
r = C(b,1);
c = C(b,2);
dir = ceil(rand*4);
nr = r + I(dir);
nc = c + J(dir);
if (nr >= 1) && (nr <= n_row) && (nc >= 1) && (nc <= n_col),
if A(nr,nc) == 0,
A(nr,nc) = b;
A(r,c) = 0;
C(b,:) = [nr,nc];
mv = [mv;[b,dir]];
end
end
end

end
```