2006-12-06 09:00:00 UTC

# So much todo in generality 1

by JohanH

Status: Passed
Results: 111916.8
CPU Time: 9.9938
Score: 1119.17
Submitted at: 2006-12-06 16:54:46 UTC
Scored at: 2006-12-06 16:55:57 UTC

Current Rank: 3480th
Basis for: So much todo in generality 2 (diff)

JohanH
06 Dec 2006
No guns, no chances taken...
Code
```function secure = solver(n)
%secure = zeros(n);
%return
beams = -ones(4*n,5);
possible = zeros(n+2); % 0 not ini or not anything, 1 possibly something
secure = -ones(n+2); % -1 = not sure, 0 = nothing, >0=something
secure([1 n+2],:) = 0;
secure(:,[1 n+2]) = 0;
ph = nan(n,5);
nph = 0;

n2 = 2*n;

zr = zeros(n+2,1); % known zero rows (1=all in row == 0,0=unknown)
zc = zeros(1,n+2); % known zero cols (1=all in col == 0,0=unknown)
zr([1 n+2]) = 1;
zc([1 n+2]) = 1;

imin = 2; % index to first (possible) non zero row, refererar till secure o possible
imax = n+1;
jmin = 2;
jmax = n+1;

lr = NaN(n,3);
rl = NaN(n,3);
ud = NaN(n,3);
du = NaN(n,3);

if n>=6 % dancing three-step %TODO ubeans f?r alla bounces & refl och genom
for i = 2:3:n % loopa ?ver orginalmatrisen
[r c s] = mybeam(i,0,'low');
if r==-i && s==0 % straight through
secure(i:i+2,:) = 0;
zr(i:i+2) = 1;
lr(1:i,1) = -1:-1:-i;
lr(1:i,2:3) = 0;
%rl(1:i,:) = -lr(1:i,:);
imin = imin + 3;
else
nph = 1;
ph(1,:) = [i 0 r c s];
break
end
end

prev = true;
for j = n-1:-3:i+1
[r c s] = mybeam(j,0,'low');
if r==-j && s==0 % straight through
secure(j:j+2,:) = 0;
zr(j:j+2) = 1;
lr(j,:) = [r 0 0];
if prev
lr(j:n,1) = -j:-1:-n;
lr(j:n,2:3) = 0;
%rl(j:n,:) = -lr(j:n,:); %TODO kolla!
end
prev = true;
else
nph = nph + 1;
ph(nph,:) = [j 0 r c s];
prev = false;
end
end
rl = -lr;

for i = 2:3:n % loopa ?ver orginalmatrisen
[r c s] = mybeam(0,i,'low');
if c==-i && s==0 % straight through
secure(:,i:i+2) = 0;
zc(i:i+2) = 1;
ud(1:i,1) = -1:-1:-i;
ud(1:i,2:3) = 0;
%du(1:i,:) = -ud(1:i,:);
jmin = jmin + 3;
else
nph = nph + 1;
ph(nph,:) = [0 i r c s];
break
end
end

prev = true;
for j = n-1:-3:i+1
[r c s] = mybeam(0,j,'low');
if c==-j && s==0 % straight through
secure(:,j:j+2) = 0;
zc(j:j+2) = 1;
ud(j,:) = [0 c 0];
if prev
ud(j:n,1) = -j:-1:-n;
ud(j:n,2:3) = 0;
%du(j:n,:) = -ud(j:n,:);
end
prev = true;
else
nph = nph + 1;
ph(nph,:) = [0 j r c s];
prev = false;
end
end
du = -ud;

end

dzr = diff(zr);
dzc = diff(zc);
imax = find(dzr==+1,1,'last'); % currently known, might be smaller!
jmax = find(dzc==+1,1,'last');

%TODO uppdatera secure, speglingar mm f?r allt utom -i 0 0 och 0 -j 0

% ======================================================================================================

%% probe outer edges, max 3 shots per edge, do double shots (from each direction), increase row/col once
ii = max(1,imin-2);
[r1 c1 s1] = lra(ii);
if r1==-ii && s1==0
ii = max(1,imin-2);
[r1 c1 s1] = lra(ii);
end
if c1 && c1<n-1
[r2 c2 s2] = rla(ii);
if ii==1 && s2>0 && r2==0 && c2 <= c1+3
zr(imin) = 1;
%zr(imin) = ~any(secure(imin,:))
imin = imin + 1;
end
end
%dzr = diff(zr);
%imin = find(dzr==-1,1) + 1;

ii = min(imax,n);
[r1 c1 s1] = lrb(ii);
if r1==-ii && s1==0
ii = min(imax,n);
[r1 c1 s1] = lrb(ii);
end
if c1 && c1<n-1 % c1<0
[r2 c2 s2] = rlb(ii);
if ii==n && s2>0 && r2==0 && c2 >= c1-3
zr(imax) = 1;
imax = imax - 1;
end
end
%dzr = diff(zr);
%imax = find(dzr==+1,1,'last');

ii = max(1,jmin-2);
[r1 c1 s1] = udl(ii);
if c1==-ii && s1==0
ii = max(1,jmin-2);
[r1 c1 s1] = udl(ii);
end
if r1 && r1<n-1
[r2 c2 s2] = dul(ii);
if ii==1 && s2>0 && c2==0 && r2 <= r1+3
zc(jmin) = 1;
%zr(imin) = ~any(secure(imin,:))
jmin = jmin + 1;
end
end
%dzr = diff(zr);
%imin = find(dzr==-1,1) + 1;

ii = min(jmax,n);
[r1 c1 s1] = udr(ii);
if c1==-ii && s1==0
ii = min(jmax,n);
[r1 c1 s1] = udr(ii);
end
if r1 && r1<n-1 % c1<0
[r2 c2 s2] = dur(ii);
if ii==n && s2>0 && c2==0 && r2 >= r1-3
zc(jmax) = 1;
jmax = jmax - 1;
end
end
%dzr = diff(zr);
%imax = find(dzr==+1,1,'last');

secure1 = secure;
possible1 = possible;

%% --- Find holes via one-step
if 1
for i = imin-1:imax-1 % loopa ?ver orginalmatrisen
[r c s] = mybeam(i,0,'low');
if r==-i && s==0 % straight through
secure(i:i+2,:) = 0;
zr(i:i+2) = 1;
lr(i,:) = [-i 0 0];
rl(i,:) = [ i 0 0];
else
nph = nph + 1;
ph(nph,:) = [i 0 r c s];
end
end
%rl = -lr;

for i = jmin-1:jmax-1 % loopa ?ver orginalmatrisen
[r c s] = mybeam(0,i,'low');
if c==-i && s==0 % straight through
secure(:,i:i+2) = 0;
zc(i:i+2) = 1;
ud(i,:) = [0 -i 0];
du(i,:) = [0  i 0];
else
nph = nph + 1;
ph(nph,:) = [0 i r c s];
end
end
%du = -ud;
end

secure2 = secure;
possible2 = possible;

dzr = diff(zr);
dzc = diff(zc);
iue = find(dzr==-1) - 1;
ide = find(dzr== 1);
ile = find(dzc==-1) - 1;
ire = find(dzc== 1);

for i = 1:length(iue)
ii = iue(i);
if ii==0
continue % already taken care of outer border
end
[r c s m] = lra2(ii);
if m
[r2 c2 s2 m2] = rla2(ii);
end
end

for i = 1:length(ide)
ii = ide(i);
if ii==n+1
continue % already taken care of outer border
end
[r c s m] = lrb2(ii);
if m
[r2 c2 s2 m2] = rlb2(ii);
end
end

for i = 1:length(ile)
ii = ile(i);
if ii==0
continue % already taken care of outer border
end
%[r c s m] = ud3(ii,0);
[r c s m] = udl2(ii);
if m
[r2 c2 s2 m2] = dul2(ii);
end
end

for i = 1:length(ire)
ii = ire(i);
if ii==n+1
continue % already taken care of outer border
end
[r c s m] = udr2(ii);
if m
[r2 c2 s2 m2] = dur2(ii);
end
end

secure3   = secure;
possible3 = possible;

secureupdated = true;
while secureupdated
secureupdated = false;
for i = 1:length(iue)
for ii = iue(i)+1:ide(i)-1
[r c s m su] = lr3(ii,0);
if su
secureupdated = true;
end
%          if m
%             [r2 c2 s2 m2] = rl3(ii);
%          end
%          [r c s m] = rl3(ii);
%          if m
%             [r2 c2 s2 m2] = lr3(ii);
%          end
end
end
end

secureupdated = true;
while secureupdated
secureupdated = false;
for i = 1:length(ile)
for ii = ile(i)+1:ire(i)-1
[r c s m su] = ud3(ii,0);
if su
secureupdated = true;
end
%          if m
%             [r2 c2 s2 m2] = rl3(ii);
%          end
%          [r c s m] = rl3(ii);
%          if m
%             [r2 c2 s2 m2] = lr3(ii);
%          end
end
end
end

secureupdated = true;
while secureupdated
secureupdated = false;
for i = 1:length(iue)
for ii = iue(i)+1:ide(i)-1
[r c s m su] = lr3(ii,0);
if su
secureupdated = true;
end
%          if m
%             [r2 c2 s2 m2] = rl3(ii);
%          end
%          [r c s m] = rl3(ii);
%          if m
%             [r2 c2 s2 m2] = lr3(ii);
%          end
end
end
end

secureupdated = true;
while secureupdated
secureupdated = false;
for i = 1:length(ile)
for ii = ile(i)+1:ire(i)-1
[r c s m su] = ud3(ii,0);
if su
secureupdated = true;
end
%          if m
%             [r2 c2 s2 m2] = rl3(ii);
%          end
%          [r c s m] = rl3(ii);
%          if m
%             [r2 c2 s2 m2] = lr3(ii);
%          end
end
end
end

secure = secure(2:n+1,2:n+1);
secure(secure==-1) = 0;

% =====================================================================
%% --- Nested functions
% =====================================================================

function [r c s] = mybeam(row,col,int) %TODO high beam
ind = n + n2*(row==0) + row + (row<0) + col + (col<0);
if beams(ind,1) ~= -1
r = beams(ind,3);
c = beams(ind,4);
s = beams(ind,5);
else
[r c s] = beam(row,col,'low');
beams(ind,:) = [row col r c s];
end
end

% Condition to call these functions: ONLY ZEROS OUTSIDE the beam!!!

function [r c s] = lra(ii)
[r,c,s] = mybeam(ii,0);
lr(ii,:) = [r c s];
if r==-ii && s==0 % straight through
if ii==1
rl(ii,:) = -lr(ii,:);
imin = imin + 2;
else
lr(ii-1,:) = [r+1 0 0]; %TODO on?dig?
rl(ii-1:ii,:) = -lr(ii-1:ii,:);
imin = imin + 1;
end
secure(ii:ii+2,:) = 0;
zr(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [ii 0 s];
secure(ii+1,1:c+2) = 0;
secure(ii+2,1:c+1) = 0;
secure(ii+2,c+2) = s;
elseif r==ii % reflected
secure(ii+1,2) = 0;
secure(ii+2,2) = s;
else
%100
end
end

function [r c s] = rla(ii)
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==1
lr(ii,:) = -rl(ii,:)
imin = imin + 2
else
rl(ii-1,:) = [r-1 0 0]
lr(ii-1:ii,:) = -rl(ii-1:ii,:)
imin = imin + 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [-ii 0 s];
secure(ii+1,c:end) = 0;
secure(ii+2,c+1:end) = 0;
secure(ii+2,c) = s;
elseif r==-ii % reflected
secure(ii+1,n+1) = 0;
secure(ii+2,n+1) = s;
else
%101
end
end

function [r c s] = lrb(ii)
[r,c,s] = mybeam(ii,0);
lr(ii,:) = [r c s];
if r==-ii && s==0 % straight through
if ii==n
rl(ii,:) = -lr(ii,:);
imax = imax - 2;
else
lr(ii+1,:) = [r-1 0 0]; %TODO on?dig?
rl(ii:ii+1,:) = -lr(ii:ii+1,:);
imax = imax - 1;
end
secure(ii:ii+2,:) = 0;
zr(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [ii 0 s];
secure(ii+1,1:-c+2) = 0;
secure(ii,1:-c+1) = 0;
secure(ii,-c+2) = s;
elseif r==ii % reflected
secure(ii,2) = s;
secure(ii+1,2) = 0;
else % multi bounce
%102
end
end

function [r c s] = rlb(ii)
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==n
lr(ii,:) = -rl(ii,:)
imax = imax - 2
else
rl(ii+1,:) = [r+1 0 0]
lr(ii:ii+1,:) = -rl(ii:ii+1,:)
imax = imax - 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [-ii 0 s];
secure(ii+1,-c:end) = 0;
secure(ii,-c+1:end) = 0;
secure(ii,-c) = s;
elseif r==-ii % reflected
secure(ii,n+1) = s;
secure(ii+1,n+1) = 0;
else
%103
end
end

%=======================================================

function [r c s] = udl(ii)
[r,c,s] = mybeam(0,ii);
ud(ii,:) = [r c s];
if c==-ii && s==0 % straight through
if ii==1
du(ii,:) = -ud(ii,:);
jmin = jmin + 2;
else
ud(ii-1,:) = [0 c+1 0]; %TODO on?dig?
du(ii-1:ii,:) = -ud(ii-1:ii,:);
jmin = jmin + 1;
end
secure(:,ii:ii+2) = 0;
zc(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r>0 % r>0 c=0, bounce left
ud(r,:) = [0 ii s];
secure(1:r+2,ii+1) = 0;
secure(1:r+1,ii+2) = 0;
secure(r+2,ii+2) = s;
elseif c==ii % reflected
secure(2,ii+1) = 0;
secure(2,ii+2) = s;
else
%200
end
end

function [r c s] = dul(ii)
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==1
ud(ii,:) = -du(ii,:)
jmin = jmin + 2
else
du(ii-1,:) = [0 c-1 0]
ud(ii-1:ii,:) = -du(ii-1:ii,:)
jmin = jmin + 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r>0 % r>0 c=0, bounce left
ud(r,:) = [0 -ii s];
secure(r:end,ii+1) = 0;
secure(r+1:end,ii+2) = 0;
secure(r,ii+2) = s;
elseif c==-ii % reflected
secure(n+1,ii+1) = 0;
secure(n+1,ii+2) = s;
else
%201
end
end

function [r c s] = udr(ii)
[r,c,s] = mybeam(0,ii);
ud(ii,:) = [r c s];
if c==-ii && s==0 % straight through
if ii==n
du(ii,:) = -ud(ii,:);
jmax = jmax - 2;
else
ud(ii+1,:) = [0 c-1 0]; %TODO on?dig?
du(ii:ii+1,:) = -ud(ii:ii+1,:);
jmax = jmax - 1;
end
secure(:,ii:ii+2) = 0;
zc(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r<0 % r<0 c=0, bounce right
du(-r,:) = [0 ii s];
secure(1:-r+2,ii+1) = 0;
secure(1:-r+1,ii) = 0;
secure(-r+2,ii) = s;
elseif c==ii % reflected
secure(2,ii) = s;
secure(2,ii+1) = 0;
else
%204
end
end

function [r c s] = dur(ii)
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==n
ud(ii,:) = -du(ii,:)
jmax = jmax - 2
else
du(ii+1,:) = [0 c+1 0]
ud(ii:ii+1,:) = -du(ii:ii+1,:)
jmax = jmax - 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r<0 % r<0 c=0, bounce right
du(-r,:) = [0 -ii s];
secure(-r:end,ii+1) = 0;
secure(-r+1:end,ii) = 0;
secure(-r,ii) = s;
elseif c==-ii % reflected
secure(n+1,ii) = s;
secure(n+1,ii+1) = 0;
else
%203
end
end

% Condition to call these functions: at least one zero row/col outside beam!!!
% ==================================================================

function [r c s m] = lra2(ii)
m = false;
[r,c,s] = mybeam(ii,0);
lr(ii,:) = [r c s];
if r==-ii && s==0 % straight through
if ii==1
rl(ii,:) = -lr(ii,:);
imin = imin + 2;
else
lr(ii-1,:) = [r+1 0 0]; %TODO on?dig?
rl(ii-1:ii,:) = -lr(ii-1:ii,:);
imin = imin + 1;
end
secure(ii:ii+2,:) = 0;
zr(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [ii 0 s];
secure(ii+1,1:c+2) = 0;
secure(ii+2,1:c+1) = 0;
secure(ii+2,c+2) = s;
elseif r==ii % reflected
secure(ii+1,2) = 0;
secure(ii+2,2) = s;
else % multi
m = true;
%2100
end
end

function [r c s m] = rla2(ii)
m = false;
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==1
lr(ii,:) = -rl(ii,:)
imin = imin + 2
else
rl(ii-1,:) = [r-1 0 0]
lr(ii-1:ii,:) = -rl(ii-1:ii,:)
imin = imin + 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [-ii 0 s];
secure(ii+1,c:end) = 0;
secure(ii+2,c+1:end) = 0;
secure(ii+2,c) = s;
elseif r==-ii % reflected
secure(ii+1,n+1) = 0;
secure(ii+2,n+1) = s;
else % multi
m = true;
%2101
end
end

function [r c s m] = lrb2(ii)
m = false;
[r,c,s] = mybeam(ii,0);
lr(ii,:) = [r c s];
if r==-ii && s==0 % straight through
if ii==n
rl(ii,:) = -lr(ii,:);
imax = imax - 2;
else
lr(ii+1,:) = [r-1 0 0]; %TODO on?dig?
rl(ii:ii+1,:) = -lr(ii:ii+1,:);
imax = imax - 1;
end
secure(ii:ii+2,:) = 0;
zr(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [ii 0 s];
secure(ii+1,1:-c+2) = 0;
secure(ii,1:-c+1) = 0;
secure(ii,-c+2) = s;
elseif r==ii % reflected
secure(ii,2) = s;
secure(ii+1,2) = 0;
else % multi
m = true;
%2102
end
end

function [r c s m] = rlb2(ii)
m = false;
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==n
lr(ii,:) = -rl(ii,:)
imax = imax - 2
else
rl(ii+1,:) = [r+1 0 0]
lr(ii:ii+1,:) = -rl(ii:ii+1,:)
imax = imax - 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 && s==0 % absorbed %TODO FIXME!!!
if zr(ii+1)
m = true;
else
possible(ii+1,jmin:jmax) = 1;
end
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [-ii 0 s];
secure(ii+1,-c:end) = 0;
secure(ii,-c+1:end) = 0;
secure(ii,-c) = s;
elseif r==-ii % reflected
secure(ii,n+1) = s;
secure(ii+1,n+1) = 0;
else % multi
m = true;
%2103
end
end

%========================================

function [r c s m] = udl2(ii)
m = false;
[r,c,s] = mybeam(0,ii);
ud(ii,:) = [r c s];
if c==-ii && s==0 % straight through
if ii==1
du(ii,:) = -ud(ii,:);
jmin = jmin + 2;
else
ud(ii-1,:) = [0 c+1 0]; %TODO on?dig?
du(ii-1:ii,:) = -ud(ii-1:ii,:);
jmin = jmin + 1;
end
secure(:,ii:ii+2) = 0;
zc(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r>0 % r>0 c=0, bounce left
ud(r,:) = [0 ii s];
secure(1:r+2,ii+1) = 0;
secure(1:r+1,ii+2) = 0;
secure(r+2,ii+2) = s;
elseif c==ii % reflected
secure(2,ii+1) = 0;
secure(2,ii+2) = s;
else % multi
m = true;
%2200
end
end

function [r c s m] = dul2(ii)
m = false;
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==1
ud(ii,:) = -du(ii,:)
jmin = jmin + 2
else
du(ii-1,:) = [0 c-1 0]
ud(ii-1:ii,:) = -du(ii-1:ii,:)
jmin = jmin + 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r>0 % r>0 c=0, bounce left
ud(r,:) = [0 -ii s];
secure(r:end,ii+1) = 0;
secure(r+1:end,ii+2) = 0;
secure(r,ii+2) = s;
elseif c==-ii % reflected
secure(n+1,ii+1) = 0;
secure(n+1,ii+2) = s;
else % multi
m = true;
%2201
end
end

function [r c s m] = udr2(ii)
m = false;
[r,c,s] = mybeam(0,ii);
ud(ii,:) = [r c s];
if c==-ii && s==0 % straight through
if ii==n
du(ii,:) = -ud(ii,:);
jmax = jmax - 2;
else
ud(ii+1,:) = [0 c-1 0]; %TODO on?dig?
du(ii:ii+1,:) = -ud(ii:ii+1,:);
jmax = jmax - 1;
end
secure(:,ii:ii+2) = 0;
zc(ii:ii+2) = 1;
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r<0 % r<0 c=0, bounce right, misses some multi...
du(-r,:) = [0 ii s];
secure(1:-r+2,ii+1) = 0;
secure(1:-r+1,ii) = 0;
secure(-r+2,ii) = s;
elseif c==ii % reflected
secure(2,ii) = s;
secure(2,ii+1) = 0;
else % multi
m = true;
%2202
end
end

function [r c s m] = dur2(ii)
m = false;
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==n
ud(ii,:) = -du(ii,:)
jmax = jmax - 2
else
du(ii+1,:) = [0 c+1 0]
ud(ii:ii+1,:) = -du(ii:ii+1,:)
jmax = jmax - 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r<0 % r<0 c=0, bounce right
du(-r,:) = [0 -ii s];
secure(-r:end,ii+1) = 0;
secure(-r+1:end,ii) = 0;
secure(-r,ii) = s;
elseif c==-ii % reflected
secure(n+1,ii) = s;
secure(n+1,ii+1) = 0;
else % multi
m = true;
%2203
end
end

% ================================================================================================
% ================================================================================================

% No prerequsities to call these functions!!!

function [r c s m su] = lr3(ii,chance)
m = false;
su = false;
[r,c,s] = mybeam(ii,0);
lr(ii,:) = [r c s];
if r==-ii && c==0 && s==0 % straight through
if any(any(secure(ii:ii+2,:)==-1))
su = true;
secure(ii:ii+2,:) = 0;
end
rl(ii,:) = -lr(ii,:);
zr(ii:ii+2) = 1;
elseif r==0 && c==0 && s==0 % absorbed
if zr(ii+1) %TODO fler krav som inneb?r multi!?
m = true;
else
possible(ii+1,jmin:jmax) = 1;
end
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [ii 0 s];
if ii==1 || ii<5 && all(secure(ii,:)==0) ... %|| ~(n>ii+1 && any(secure(ii,4:c-1)~=0)) ... % sth above, && n>c always true if here %|| ~(ii>1 && any(secure(ii+2,4:c+1)~=0) && any(any(secure(2:ii-1,2:c)~=0))) ... % sth below, && n>c
|| all(all(secure(2:ii,:)==0))
if any(secure(ii+1,1:c+2)~=0) && any(secure(ii+2,1:c+1)~=0) && secure(ii+2,c+2)~=s
su = true;
secure(ii+1,1:c+2) = 0;
secure(ii+2,1:c+1) = 0;
secure(ii+2,c+2) = s;
end
end
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [ii 0 s];
if ii==n || ii>n-4 && all(secure(ii+2,:)==0) ... %|| ~(ii>2 && any(secure(ii+2,4:abs(c)-1)~=0)) ... % sth below,  % && n>c always true if here %|| ~(ii<n && any(secure(ii,4:abs(c)+1)~=0) && any(any(secure(ii+2:n+1,2:abs(c))~=0))) ... % sth above, && n>c
|| all(all(secure(ii+2:n+1,:)==0))
if any(secure(ii+1,1:-c+2)~=0) && any(secure(ii,1:-c+1)~=0) && secure(ii,-c+2)~=s
su = true;
secure(ii+1,1:-c+2) = 0;
secure(ii,1:-c+1) = 0;
secure(ii,-c+2) = s;
end
end
end

if r==ii % reflected
if secure(ii+1,2)==-1
su = true;
end
if chance
if secure(ii,2)==0 && secure(ii+2,2)==-1
su = true;
secure(ii+2,2) = s; %TODO Take a chance...
elseif secure(ii,2)==-1 && secure(ii+2,2)==0
su = true;
secure(ii,2) = s; %TODO Take a chance...
elseif secure(ii,2)==-1 && secure(ii+2,2)==-1
su = true;
secure(ii,2)   = round(s/2); %TODO Take a chance...
secure(ii+2,2) = round(s/2); %TODO Take a chance...
elseif secure(ii,2)==0 && secure(ii+2,2)==0
c1 = find(secure(ii+1,:)~=0,1);
%TODO Analysis...
else
%300
end
end
elseif r~=ii && r>0 && c==0
m = true;
elseif ~(r==0 && c==0 && s==0) % not reflected, not absorbed
if any(secure(ii:ii+2,2)==-1)
%secure(ii-1:ii+2,2:4)
%[r c s]
su = true;
secure(ii:ii+2,2) = 0;
%secure(ii-1:ii+2,2:4)
end
end
end

function [r c s m] = rla3(ii)
m = false;
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==1
lr(ii,:) = -rl(ii,:)
imin = imin + 2
else
rl(ii-1,:) = [r-1 0 0]
lr(ii-1:ii,:) = -rl(ii-1:ii,:)
imin = imin + 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c>0 % r=0 c>0, bounce up
ud(c,:) = [-ii 0 s];
secure(ii+1,c:end) = 0;
secure(ii+2,c+1:end) = 0;
secure(ii+2,c) = s;
elseif r==-ii % reflected
secure(ii+1,n+1) = 0;
secure(ii+2,n+1) = s;
else
%101
end
end

function [r c s m] = rlb3(ii)
m = false;
[r,c,s] = mybeam(-ii,0);
rl(ii,:) = [r c s];
if r==ii && s==0 % straight through
if ii==n
lr(ii,:) = -rl(ii,:)
imax = imax - 2
else
rl(ii+1,:) = [r+1 0 0]
lr(ii:ii+1,:) = -rl(ii:ii+1,:)
imax = imax - 1
end
secure(ii:ii+2,:) = 0
zr(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(ii+1,jmin:jmax) = 1;
elseif c<0 % r=0 c<0, bounce down
du(-c,:) = [-ii 0 s];
secure(ii+1,-c:end) = 0;
secure(ii,-c+1:end) = 0;
secure(ii,-c) = s;
elseif r==-ii % reflected
secure(ii,n+1) = s;
secure(ii+1,n+1) = 0;
else
103
end
end

%=======================================

function [r c s m su] = ud3(ii,chance)
m = false;
su = false;
[r,c,s] = mybeam(0,ii);
ud(ii,:) = [r c s];
if r==0 && c==-ii && s==0 % straight through
if any(any(secure(:,ii:ii+2)==-1))
su = true;
secure(:,ii:ii+2) = 0;
end
du(ii,:) = -ud(ii,:);
zc(ii:ii+2) = 1;
elseif r==0 && c==0 && s==0 % absorbed
if zc(ii+1) %TODO fler krav som inneb?r multi!?
m = true;
else
possible(imin:imax,ii+1) = 1;
end
elseif r>0 % r=0 c>0, bounce left
lr(r,:) = [0 ii s];
if ii==1 || ii<5 && all(secure(:,ii)==0) ... %|| ~(n>ii+1 && any(secure(ii,4:c-1)~=0)) ... % sth above, && n>c always true if here %|| ~(ii>1 && any(secure(ii+2,4:c+1)~=0) && any(any(secure(2:ii-1,2:c)~=0))) ... % sth below, && n>c
|| all(all(secure(:,2:ii)==0))
if any(secure(1:r+2,ii+1)~=0) && secure(1:r+1,ii+2) && secure(r+2,ii+2)~=s
su = true;
secure(1:r+2,ii+1) = 0;
secure(1:r+1,ii+2) = 0;
secure(r+2,ii+2) = s;
end
end
elseif r<0 % r=0 c<0, bounce right
rl(-r,:) = [0 ii s];
if ii==n || ii>n-4 && all(secure(:,ii+2)==0) ... %|| ~(ii>2 && any(secure(ii+2,4:abs(c)-1)~=0)) ... % sth below,  % && n>c always true if here %|| ~(ii<n && any(secure(ii,4:abs(c)+1)~=0) && any(any(secure(ii+2:n+1,2:abs(c))~=0))) ... % sth above, && n>c
|| all(all(secure(:,ii+2:n+1)==0))
if any(secure(ii+1,1:-c+2)~=0) && any(secure(ii,1:-c+1)~=0) && secure(ii,-c+2)~=s
su = true;
secure(1:-r+2,ii+1) = 0;
secure(1:-r+1,ii) = 0;
secure(-r+2,ii) = s;
end
end
end

if c==ii % reflected
if secure(2,ii+1)==-1
su = true;
end
if chance
if secure(2,ii)==0 && secure(2,ii+2)==-1
su = true;
secure(2,ii+2) = s; %TODO Take a chance...
elseif secure(2,ii)==-1 && secure(2,ii+2)==0
su = true;
secure(2,ii) = s; %TODO Take a chance...
elseif secure(2,ii)==-1 && secure(2,ii+2)==-1
su = true;
secure(2,ii)   = round(s/2); %TODO Take a chance...
secure(2,ii+2) = round(s/2); %TODO Take a chance...
elseif secure(2,ii)==0 && secure(2,ii+2)==0
%r1 = find(secure(:,ii+1)~=0,1);
%TODO Analysis...
else
%300
end
end
elseif c~=ii && c>0 && r==0
m = true;
elseif ~(r==0 && c==0 && s==0) % not reflected, not absorbed
if any(secure(2,ii:ii+2)==-1)
%secure(ii-1:ii+2,2:4)
%[r c s]
su = true;
secure(2,ii:ii+2) = 0;
%secure(ii-1:ii+2,2:4)
end
end
end

function [r c s m] = dul3(ii)
m = false;
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==1
ud(ii,:) = -du(ii,:)
jmin = jmin + 2
else
du(ii-1,:) = [0 c-1 0]
ud(ii-1:ii,:) = -du(ii-1:ii,:)
jmin = jmin + 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r>0 % r>0 c=0, bounce left
ud(r,:) = [0 -ii s];
secure(r:end,ii+1) = 0;
secure(r+1:end,ii+2) = 0;
secure(r,ii+2) = s;
elseif c==-ii % reflected
secure(n+1,ii+1) = 0;
secure(n+1,ii+2) = s;
else
201
end
end

function [r c s m] = dur3(ii)
m = false;
[r,c,s] = mybeam(0,-ii);
du(ii,:) = [r c s];
if c==ii && s==0 % straight through
if ii==n
ud(ii,:) = -du(ii,:)
jmax = jmax - 2
else
du(ii+1,:) = [0 c+1 0]
ud(ii:ii+1,:) = -du(ii:ii+1,:)
jmax = jmax - 1
end
secure(:,ii:ii+2) = 0
zc(ii:ii+2) = 1
elseif r==0 && c==0 % absorbed
possible(imin:imax,ii+1) = 1;
elseif r<0 % r<0 c=0, bounce right
du(-r,:) = [0 -ii s];
secure(-r:end,ii+1) = 0;
secure(-r+1:end,ii) = 0;
secure(-r,ii) = s;
elseif c==-ii % reflected
secure(n+1,ii) = s;
secure(n+1,ii+1) = 0;
else
203
end
end

end
```