2011-11-09 12:00:00 UTC

# solver_F giddap

Status: Passed
Results: 77329414 (cyc: 16, node: 1008)
CPU Time: 53.562
Score: 773366.0
Submitted at: 2011-11-04 12:50:35 UTC
Scored at: 2011-11-04 12:52:16 UTC

Current Rank: 1229th (Highest: 224th )
Based on: solver_E will it run? (diff)
Basis for: solver_G marginally faster, marginally smarter (diff)

Code
```% submissions/solver_F.m
% James White
function [moves, vine] = solver(board, limit)

SCORE = -Inf * ones(1,25);
VINE = cell(1,25);
MOVES = cell(1,25);

%[MOVES{1}, VINE{1}, SCORE(1)]  = initial_solver(board, limit);

A = [];

% [MOVES{2}, VINE{2}, SCORE(2)] = dfs(board,A,7);
% %check(MOVES{2},VINE{2},board,limit, SCORE(2));
%
[MOVES{3}, VINE{3}, SCORE(3)]  = greedy(board,A,1);
% % check(MOVES{3},VINE{3},board,limit, SCORE(3));
[MOVES{4}, VINE{4}, SCORE(4)] = greedy(board,A, -1);
% % check(MOVES{4},VINE{4},board,limit, SCORE(4));

A = graph_from_board(board);

N=5;
%[MOVES{N}, VINE{N}, SCORE(N)] = warnsdorff3(board,A);
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

N=6;
%[MOVES{N}, VINE{N}, SCORE(N)] = reverse_warnsdorff3(board,A);
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

% why are these guys with A' doing better?
N=10;
%[MOVES{N}, VINE{N}, SCORE(N)] = reverse_warnsdorff3(board,A');
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

N=12;
%[MOVES{N}, VINE{N}, SCORE(N)] = warnsdorff3(board,A');
%check(MOVES{N},VINE{N},board,limit, SCORE(N));

%
%
N=8;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A, 1);
% %check(MOVES{8},VINE{8},board,limit, SCORE(8));
N=9;
[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A', -1);
% %check(MOVES{9},VINE{9},board,limit, SCORE(9));
N=20;
%[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A', 1);
N=21;
%[MOVES{N}, VINE{N}, SCORE(N)] = greedorf(board,A, -1);

[result, vine_idx]          = max(SCORE);
%fprintf('%i',vine_idx);
vine                   = VINE{vine_idx};
moves                  = MOVES{vine_idx};
%out = double(board);

end

%function [moves, vine, score] = initial_solver(board, limit)
%
%moves = [];
%[score,vine] = max(board(:));
%
%end

function A = graph_from_board(board)

A = sparse(numel(board),numel(board),0);
nrows = size(board,1);

for i = 1:numel(board)

if i+1 <= numel(board) && mod(i,nrows) % smaller index must not be at end of a column
A(i,i+1    ) = board(i) <= board(i+1);
end

if i > 1 && mod(i-1,nrows) % smaller index must not be at end of a column
A(i,i-1    ) = board(i) <= board(i-1);
end

if i + nrows <= numel(board)
A(i,i+nrows) = board(i) <= board(i+nrows);
end

if i > nrows
A(i,i-nrows) = board(i) <= board(i-nrows);
end

end

end

% function flag = isconnected(a,b,m)
% % a,b:  Absolute indices
% % m,n:  Size of board
% % flag: True if indices a and b are four connected
% d = abs(a(:)-b(:));
% flag = (d == m) | (d == 1 & mod(min(a(:),b(:)), m));
% end
function [moves, bestvine, bestscore] = greedy(board,A, UPDOWN)
% UPDOWN = 1 to walk uphill, -1 to walk downhill

board = UPDOWN*board;
VISITED = -Inf;
moves = [];
vine = zeros(numel(board),1);
bestscore = -Inf;

%startnodes = 1:numel(board); % all

startnodes = find(board(:) == min(board(:)))'; % maybe not the best strategy

if numel(startnodes) > 50,
startnodes = startnodes(1:floor(numel(startnodes)/50):end);
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];

original_board = board;

for i = startnodes
board = original_board;
vine(1) = i;
score = board(vine(1));
curval = board(vine(1));
board(vine(1)) = VISITED; % mark as visited
k = 1;

while(1)
%fprintf('round %i\n',k);
%fprintf('  vine(%i) = %i\n',k, vine(k));

hi = Inf;
next = 0;
for adj = vine(k) + nsew

end
end
end

if ~next
break;
end

curval = hi;
k = k+1;

score = score + hi;
vine(k) = next;
board(vine(k)) = VISITED; % mark as visited
end

score = UPDOWN*score;

if score > bestscore
bestscore = score;
bestvine = vine;
bestlength = k;
end

end

if UPDOWN == 1
bestvine = bestvine(1:bestlength);
else
bestvine = bestvine(bestlength:-1:1);
end

end

function [moves, bestvine, bestscore] = greedorf(board,A, UPDOWN, nsew)
% UPDOWN = 1 to walk uphill, -1 to walk downhill
% try to stay in current region and try to wind

board = UPDOWN*board;
%A = graph_from_board(board);

VISITED = -Inf;
moves = [];
vine = zeros(numel(board),1);
bestscore = -Inf;

%startnodes = 1:numel(board); % all

startnodes = find(board(:) == min(board(:)))'; % maybe not the best strategy

if numel(startnodes) > 50,
startnodes = startnodes(1:floor(numel(startnodes)/50):end);
end
nrows = size(board,1);
nsew = [ 1 -1 nrows -nrows];
%nsew = [ nrows -nrows 1 -1 ];
%nsew = [  -nrows 1 nrows -1 ];
original_board = board;

for i = startnodes

board = original_board;
edgecount = sum(A,2); % outdegree

vine(1) = i;
score = board(vine(1));
curval = board(vine(1));
neighbors = find(A(:, vine(1))); % nodes pointing to vine
edgecount(neighbors) = edgecount(neighbors)-1;

board(vine(1)) = VISITED; % mark as visited
k = 1;

%next = 0;
%hi = Inf;

while(1)
%fprintf('round %i\n',k);
%fprintf('  vine(%i) = %i\n',k, vine(k));
edge_count_lo = Inf;
hi = Inf;
next = 0;
%bob = find(A(vine(k),:));
for adj = vine(k) + nsew(randperm(4))
%assert( any( bob == adj ) );

%assert(n<4,'next_outdeg = %i, n = %i, vine(%i) = %i,adj = %i',next_outdeg, n,k, vine(k), adj);%%%%%%%%%%
%assert(next_outdeg == n, 'next_outdeg = %i, n = %i',next_outdeg, n);
%assert(next_outdeg<4,'next_outdeg = %i, n = %i',next_outdeg, n);%%%%%%%%%%
%assert(k < numel(board));

end

end
end
end
end

if ~next
break;
end

curval = board(next); % hi;
k = k+1;

score = score +  board(next); %hi;
vine(k) = next;

neighbors = find(A(:, vine(k))); % nodes pointing to vine
edgecount(neighbors) = edgecount(neighbors)-1;
board(vine(k)) = VISITED; % mark as visited
end

score = UPDOWN*score;

if score > bestscore
bestscore = score;
bestvine = vine;
bestlength = k;
end

end

if UPDOWN == 1
bestvine = bestvine(1:bestlength);
else
bestvine = bestvine(bestlength:-1:1);
end

end

```