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

trythis30 - generality

by Ralf Sauermann

Status: Passed
Results: 15284.6
CPU Time: 14.8775
Score: 152.852
Submitted at: 2006-12-06 14:32:15 UTC
Scored at: 2006-12-06 14:32:50 UTC

Current Rank: 2967th
Based on: trythis30 (diff)

Comments
Ralf Sauermann
06 Dec 2006
Same as trythis30
Please login or create a profile.
Code
function s=f(n)

global n2 vl er ec
n2 = n;
vl = [];

s = zeros(n);

% m = s;
% for i = 1:n
%     [r, c, p] = beam2(i,0);
%     if r == -i && c == 0 && p == 0
% %         m(max(i-1,1):min(i+1,n),:) = 1;
%         m(i,:) = 1;
%     end
%     [r, c, p] = beam2(0,i);
%     if r == 0 && c == -i && p == 0
% %         m(:,max(i-1,1):min(i+1,n)) = 1;
%         m(:,i) = 1;
%     end
% end
% er = any(m');
% ec = any(m);
% % figure
% % imagesc(m)
% % pause

[r, c, p] = beam2(1,0);
if (r == -1 && c == 0 && p == 0) || (r == 0 && c == n-1)
    s = work(s,n);
    return
else
    [r2, c2, p2] = beam2(-1,0);
    if r2 == 0 && c2 == c+2;
        s = work(s,n);
        return
    end
end

[r, c, p] = beam2(n,0);
if (r == -n && c == 0 && p == 0) || (r == 0 && c == n-1)
    s = work2(s,n);
    return
else
    [r2, c2, p2] = beam2(-n,0);
    if r2 == 0 && c2 == c-2;
        s = work2(s,n);
        return
    end
end

[r, c, p] = beam2(0,1);
if (r == 0 && c == -1 && p == 0) || (r == n-1 && c == 0)
    s = work3(s,n);
    return
else
    [r2, c2, p2] = beam2(0,-1);
    if r2 == r+2 && c2 == 0;
        s = work3(s,n);
        return
    end
end

[r, c, p] = beam2(0,n);
if (r == 0 && c == -n && p == 0) || (r == n-1 && c == 0)
    s = work4(s,n);
    return
else
    [r2, c2, p2] = beam2(0,-n);
    if r2 == r-2 && c2 == 0;
        s = work4(s,n);
        return
    end
end


r = zeros(1,4);
c = r;
p = r;
[r(1), c(1), p(1)] = beam2(2,0);
[r(2), c(2), p(2)] = beam2(1,0);
[r(3), c(3), p(3)] = beam2(0,1);
[r(4), c(4), p(4)] = beam2(0,2);
if all(r == [2 0 0 0]) && all(c == [0 0 0 2]) && p(1) == p(4) && all(p(2:3) == 0)
    s(1,1) = p(1);
    beam2(0,1,'high');
end

r = zeros(1,4);
c = r;
p = r;
[r(1), c(1), p(1)] = beam2(-2,0);
[r(2), c(2), p(2)] = beam2(-1,0);
[r(3), c(3), p(3)] = beam2(0,n);
[r(4), c(4), p(4)] = beam2(0,n-1);
if all(r == [-2 0 0 0]) && all(c == [0 0 0 n-1]) && p(1) == p(4) && all(p(2:3) == 0)
    s(1,n) = p(1);
    beam2(0,n,'high');
end


% [r, c, p] = beam2(1,0);
% [r2, c2, p2] = beam2(0,1);
% if all([r, c, p, r2, c2, p2] == 0)
%     i = 1;
%     d = 1;
%     while true
%         i = i + d;
%         if i < 1 || i > n
%             break
%         end
%         [r, c, p] = beam2(0,i);
%         if all([r, c, p] == 0)
%             continue
%         end
%         if r == 0 && c == i && p > 0
%             s(1,i-1) = p;
%             if s(1,i-1) ~= A(1,i-1)
%                 zzz = 0;
%             end
%             beam2(0,i-1,'high');
%             d = -1;
%         else
%             break
%         end
%     end
% end


is = 1;
[r, c, p] = beam2(1,0);
if r == 0 && c > 0
    is = c;
end
ie = n;
[r, c, p] = beam2(-1,0);
if r == 0 && c > 0
    ie = c;
end
r = zeros(1,n);
c = r;
p = r;
for i = is:ie
    [r(i), c(i), p(i)] = beam2(0,i);
    if i > 2
        if all(r(i-2:i) == 0) && all(c(i-2:i) == [i-2, 0, i]) && p(i-2) == p(i) && p(i-1) == 0
            s(1,i-1) = p(i);
            beam2(0,i-1,'high');
            r(i-2:i) = 0;
            c(i-2:i) = 0;
            p(i-2:i) = 0;
        end
    end
    if i > 3
        if all(r(i-3:i) == 0) && all(c(i-3:i) == [i-3, 0, 0, i]) && all(p(i-2:i-1) == 0)
            s(1,i-2) = p(i-3);
            s(1,i-1) = p(i);
            beam2(0,i-2,'high');
            beam2(0,i-1,'high');
            r(i-3:i) = 0;
            c(i-3:i) = 0;
            p(i-3:i) = 0;
        end
    end
    if i > 4
        if all(r(i-4:i) == 0) && all(c(i-4:i) == [i-4, 0, i-2, 0, i]) && all(p(i-3:i-1) == [0, p(i-4)+p(i), 0])
            s(1,i-3) = p(i-4);
            s(1,i-1) = p(i);
            beam2(0,i-3,'high');
            beam2(0,i-1,'high');
            r(i-4:i) = 0;
            c(i-4:i) = 0;
            p(i-4:i) = 0;
        end
    end
    if i > 6
        if all(r(i-6:i) == 0) && all(c(i-6:i) == [i-6, 0, i-4, 0, i-2, 0, i]) && all(p(i-5:i-1) == [0, p(i-6)+p(i-2)-p(i), 0, p(i)+p(i-4)-p(i-6), 0])
            s(1,i-5) = p(i-6);
            s(1,i-3) = p(i-2)-p(i);
            s(1,i-1) = p(i);
            beam2(0,i-5,'high');
            beam2(0,i-3,'high');
            beam2(0,i-1,'high');
            r(i-6:i) = 0;
            c(i-6:i) = 0;
            p(i-6:i) = 0;
        end
    end
end

[r, c, p] = beam2(1,0);
if r == -1 && c == 0 && p == 0
    s = work(s,n);
    return
end

% if nnz(A(1,:))
%     zzz = 0;
%     figure
%     subplot(121)
%     imagesc(A2)
%     subplot(122)
%     imagesc(A)
%     pause
% end

% r = zeros(1,4);
% c = r;
% p = r;
% d = r;
% [r(1), c(1), p(1)] = beam2(1,0);
% [r(2), c(2), p(2)] = beam2(n,0);
% [r(3), c(3), p(3)] = beam2(0,1);
% [r(4), c(4), p(4)] = beam2(0,n);
% 
% if r(1) == 0 && c(1) > 0 && p(1) > 0
%     d(1) = c(1);
% end
% if r(2) == 0 && c(2) < 0 && p(2) > 0
%     d(2) = -c(2);
% end
% if r(3) > 0 && c(3) == 0 && p(3) > 0
%     d(3) = r(3);
% end
% if r(4) < 0 && c(4) == 0 && p(4) > 0
%     d(4) = -r(4);
% end
% 
% [d,ndx] = max(d);
% switch ndx
%     case 1
%         s = work(s,n);
%     case 2
%         s = work2(s,n);
%     case 3
%         s = work3(s,n);
%     case 4
%         s = work4(s,n);
% end
% 
% return


[r, c, p] = beam2(1,0);
if r == 0 && c == 0 && p == 0
    [r, c, p] = beam2(n,0);
    if r == 0 && c == 0 && p == 0
        [r, c, p] = beam2(0,1);
        if r == 0 && c == 0 && p == 0
            [r, c, p] = beam2(0,n);
            if r == 0 && c == 0 && p == 0
                s = work(s,n);
            else
                s = work4(s,n);
            end
        else
            s = work3(s,n);
        end
    else
        s = work2(s,n);
    end
else
    s = work(s,n);
end


function s = work(s,n)
a = false;
ref = false;
k = 0;
i = 1;
while true
    [r, c, p] = beam2(i+k,0);
    if r == 0 && c == 0 && p == 0
        beam2(i+k,0,'high');
        continue
    end
    if r == i+k && c == 0
        s(i+k+1,1) = p;
        beam2(i+k+1,0,'high');
        if k > 0
            k = k - 1;
        end
        continue
    end
    if r == 0 && c > 0
        s(i+k+1,c+1) = p;
        
        if i == n-1 && c+2 <= n
            % don't delete stuff in last row
            is = c + 2;
            [r, c, p] = beam2(-i,0);
            if c == 0
                c = n;
            end
            ie = c;
            if is == ie
                break
            end
            i = is;
            while true
                if i > ie
                    break
                end
                [r, c, p] = beam2(0,i);
                if all([r, c, p] == 0)
                    if i == n
                        break
                    end
                    beam2(0,i-1,'high');
                    i = i - 1;
                elseif r > 0 && c == 0
                    s(n,i+1) = p;
                    i = i + 2;
                elseif r == 0 && c > 0
                    if c >= i
                        s(n,c+1) = p - s(n,i-1);
                        i = c + 2;
                    else
                        s(n,i+1) = p - s(n,c-1);
                        i = i + 2;
                    end
                elseif r == 0
                    i = i + 1;
                elseif r < 0
                    break
                end
            end
%             while true
%                 if c >= n-1
%                     break
%                 end
%                 pp = p;
%                 [r, c, p] = beam2(0,c+2);
%                 if all([r, c, p] == 0)
%                     beam2(0,c+1,'high');
%                 if r == 0 && c > 0
%                     s(n,c+1) = p - pp;
%                 else
%                     break
%                 end
%             end
            break
        end
        
        
        
        k = k + 1;
        beam2(i+k,0,'high');
        [r2, c2, p2] = beam2(i+k-1,0);
        if ~(r == r2 && c == c2 && p == p2)
            for j = k-1:-1:1
                beam2(i+j,0,'high');
            end
            k = 0;
        end
        continue
    end
    i = i + 1;
    if i > n
        break
    end
end

function s = work2(s,n)
k = 0;
i = n;
while true
    [r, c, p] = beam2(i-k,0);
    if r == 0 && c == 0 && p == 0
        beam2(i-k,0,'high');
        continue
    end
    if r == i-k && c == 0
        s(i-k-1,1) = p;
        beam2(i-k-1,0,'high');
        if k > 0
            k = k - 1;
        end
        continue
    end
    if r == 0 && c < 0
        s(i-k-1,-c+1) = p;
        k = k + 1;
        beam2(i-k,0,'high');
        [r2, c2, p2] = beam2(i-k+1,0);
        if ~(r == r2 && c == c2 && p == p2)
            for j = k-1:-1:1
                beam2(i-j,0,'high');
            end
            k = 0;
        end
        continue
    end
    i = i - 1;
    if i < 1
        break
    end
end

function s = work3(s,n)
k = 0;
i = 1;
while true
    [r, c, p] = beam2(0,i+k);
    if r == 0 && c == 0 && p == 0
        beam2(0,i+k,'high');
        continue
    end
    if c == i+k && r == 0
        s(1,i+k+1) = p;
        beam2(0,i+k+1,'high');
        if k > 0
            k = k - 1;
        end
        continue
    end
    if c == 0 && r > 0
        s(r+1,i+k+1) = p;
        k = k + 1;
        beam2(0,i+k,'high');
        [r2, c2, p2] = beam2(0,i+k-1);
        if ~(r == r2 && c == c2 && p == p2)
            for j = k-1:-1:1
                beam2(0,i+j,'high');
            end
            k = 0;
        end
        continue
    end
    i = i + 1;
    if i > n
        break
    end
end

function s = work4(s,n)
k = 0;
i = n;
while true
    [r, c, p] = beam2(0,i-k);
    if r == 0 && c == 0 && p == 0
        beam2(0,i-k,'high');
        continue
    end
    if c == i-k && r == 0
        s(1,i-k-1) = p;
        beam2(0,i-k-1,'high');
        if k > 0
            k = k - 1;
        end
        continue
    end
    if c == 0 && r < 0
        s(-r+1,i-k-1) = p;
        k = k + 1;
        beam2(0,i-k,'high');
        [r2, c2, p2] = beam2(0,i-k+1);
        if ~(r == r2 && c == c2 && p == p2)
            for j = k-1:-1:1
                beam2(0,i-j,'high');
            end
            k = 0;
        end
        continue
    end
    i = i - 1;
    if i < 1
        break
    end
end

function [r,c,p] = beam2(m,n,pow)

global n2 rl cl pl vl er ec

if nargin > 2
    beam(m,n,'high');
    vl(:) = 0;
    return
end

if isempty(vl)
    rl = zeros(n2,4);
    cl = rl;
    pl = rl;
    vl = rl;
end

if m > 0
    d = 1;
    k = m;
elseif m < 0
    d = 2;
    k = -m;
elseif n > 0
    d = 3;
    k = n;
else
    d = 4;
    k = -n;
end

if vl(k,d)
    r = rl(k,d);
    c = cl(k,d);
    p = pl(k,d);
else
    [r,c,p] = beam(m,n);
    rl(k,d) = r;
    cl(k,d) = c;
    pl(k,d) = p;
    vl(k,d) = 1;
end