Finish 2012-04-11 12:00:00 UTC

próbálkozás a koppintás után

by Tamás Király

Status: Passed
Results: 128992 (cyc: 5, node: 656)
CPU Time: 12.747
Score: 12910.3
Submitted at: 2012-04-06 17:03:16 UTC
Scored at: 2012-04-06 17:04:23 UTC

Current Rank: 1046th (Highest: 13th )
Based on: vamos ver se melhora (diff)

Comments
Please login or create a profile.
Code
function [board, orientation] = solver(tiles, boardSize)
orientation = ones(size(tiles,1), 1);
ntiles = size(tiles,1);
board = zeros(boardSize);

played = discardUnplayed(ntiles-numel(board), tiles);

if (ntiles-numel(board)<0)
    height=min([ceil(sqrt(ntiles)) boardSize(1)]);
    if (ntiles/boardSize(2))>height
        height=ceil(ntiles/boardSize(2));
    end
else
    ntiles=numel(board);
    height=boardSize(1);
end

for k=1:ntiles
    [y,x]=ind2sub([height,boardSize(2)],k);
    board(y,x)=-1;
end
last = [y,x];

list = played;
[board,orientation,list] = place([1 height],1:last(2),board,played,list,orientation,ntiles);
[board,orientation,list] = place(1:height,[1 last(2)],board,played,list,orientation,ntiles);
[board,orientation,list] = place(height-1:-1:2,boardSize(2)-1:-1:2,board,played,list,orientation,ntiles);

end

function [brd, ort, lst] = place(rowRange, colRange, board, tiles, list, orientation, ntiles)
    k = 1;
    brd = board;
    ort = orientation;
    lst = list;
    for y=rowRange
        for x=colRange
            if (brd(y,x)==-1)
                [n,e,s,w] = findBoundaries(brd, tiles, ort, y, x);
                [t, o] = findBestTile(lst, n, e, s, w);
                ort(t) = o;
                brd(y,x)=t;
                lst(t,1)=nan;
                k = k + 1;
                if (k > ntiles)
                    return;
                end
            end
        end
    end
end

function [played] = discardUnplayed(numUnplayed, tiles)
    played = tiles;
    sums = sum(tiles(:,[1 3 4]),2);
    for i = 1:numUnplayed
        [~, m]=min(sums);
        played(m,1)=nan;
        sums(m)=nan;
    end
end

function [tile, orientation] = findBestTile(list, north, east, south, west)
    score = zeros(size(list,1),4);
    cs = [1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
    grid = ones(size(list,1),1)*[north east south west];
    for i=1:4
        temp = abs(list(:,cs(i,:))-grid);
        temp(isinf(temp))=0;
        score(:,i) = sum(temp,2);
    end
    [ms, t] = min(score);
    [~, orientation] = min(ms);
    tile = t(orientation);

%     [t, o] = find(score==min(score(:)));
%     %[~, which] = max(mean(t,2));
%     which = 1;%round(length(t)/2);%round(rand*(length(t)-1)+1);
%     tile = t(which);
%     orientation = o(which);
end

function [north, east, south, west] = findBoundaries(board, tiles, orientation, row, col)
    north = boundary(board,tiles,orientation,row-1,col,3);
    south = boundary(board,tiles,orientation,row+1,col,1);
    west = boundary(board,tiles,orientation,row,col-1,2);
    east = boundary(board,tiles,orientation,row,col+1,4);
end

function [value] = boundary(board,tiles,orientation,row,col,id)
    cs = [1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3];
    try
        if (board(row,col)==0)
            value = 0;
        elseif (board(row,col)==-1)
            value = inf;
        else
            tile = tiles(board(row,col),cs(orientation(board(row,col)),:));
            value = tile(id);
        end
    catch
        value = 0;
    end
end