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

BBBBBBBBB

by the cyclist

Status: Passed
Results: 11799.2
CPU Time: 12.8345
Score: 117.997
Submitted at: 2006-12-06 16:59:16 UTC
Scored at: 2006-12-06 17:12:30 UTC

Current Rank: 3rd
Based on: The real end! (diff)

Comments
Please login or create a profile.
Code
function solution = solver(n) %end
S=0; %[r,c,s] = mb(1,0,0,rot);
if n==10 %end
solution = zeros(n); %sol(tempR,tempC)=tempS;B=1;
sol=ones(n).*-1; %sR=-1.*ones(1,n);
rT=-ones(1,n); %    s( 21,27 )=a;    s( 22,36 )=a;    s( 24,23 )=a;    s( 24,30 )=a;
cT=-ones(1,n); %end
sT=-ones(1,n); %end
%end
rL=-ones(1,n); %if i > 0
%[turns charge] = setCmds(A);
cL=-ones(1,n); %rB(1)=0;
%end
sL=-ones(1,n); %if ~(r1~=0 || c1~=0)
%    s(25,43)=35;s(28,37)=35;s(29,34)=35;s(30,32)=35;s(32,36)=35;
rB=-ones(1,n); %solution(1,1) = min(s2,s3);
cB=-ones(1,n); %end
sB=-ones(1,n); %end
%c = c+1 + ((n+1)*(d==4));
rR=-ones(1,n); %B=0;
cR=-ones(1,n); %end
sR=-ones(1,n); %if rT(i)==0 && cT(i)>0 
do1T=1; %rr = 0;
doNT=1; %end
%end
bl=0; %sB(1)=0;
AAA=0; %if(~s)
for i=[2:n-1 1 n] %nn = nn-1;
%if(r==0 && c==0)
if rT(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1T) || (i==n && doNT)) %end
%bl=bl-1;
bl=bl+1; %[r,c,s] = mb(0,sc-1,0,rot);
[rT(i),cT(i),sT(i)] = beam(0,i); %else
%sL(rL(i))=sL(i);
if rT(i)==0 && cT(i)>0  %r2=0;c2=0;s2=0;
rT(cT(i))=0; %end
cT(cT(i))=i; %rR(-rT(i))=0;
%elseif ss
sT(cT(i))=sT(i); %rc=r+c;
bl=bl-1; %rT=-1.*ones(1,n);
%    s(25,43)=35;s(28,37)=35;s(29,34)=35;s(30,32)=35;s(32,36)=35;
end %varargout = {0,0,0};
%end
if rT(i)==0 && cT(i)<0 %if B(idx)>=0 && s == 1
%    solution=s;
rB(-cT(i))=0; %if(rot<=0)
cB(-cT(i))=i; %cL(rL(i))=0;
%end
sB(-cT(i))=sT(i); %mb(0,c+1,1,rot);
%end
bl=bl-1; %end
if i==2 && sT(i)==0 %    return
%if n>22,solution(:,floor(1+rand*n/8))=0;end;   
rB(1)=0; %for i=2:n-2
cB(1)=1; %    s(5,7)=40;     s(6,12)=10;     s(6,16)=40;     s(7,7)=22; 
%cL=-1.*ones(1,n);
sB(1)=0; %sc2 = c2 - 2;
%    s( 31,6 )=a;    s( 33,24 )=a;    s( 34,6 )=a;    s( 34,18 )=a;
rT(1)=0; %a=fliplr(rot90(ai,-r));
cT(1)=-1; %sB(n)=0;
sT(1)=0; %if(r2)
do1T=0; %    s(34,23)=45;    s(31,8)=45;    s(30,27)=45;    s(23,18)=45;
end %if(r1 == 0 && c1 == c+1)
%r = abs(r);
if i==n-1 && sT(i)==0 %V=zeros(4*n,1)-1;
rB(n)=0; %bl=bl-1;
cB(n)=n; %bl=bl+1;
sB(n)=0; %
rT(n)=0; %doNT=0;
cT(n)=-1.*n; %end
sT(n)=0; %if  sol(tempR,tempC)>99
doNT=0; %for desp = 0:2
%solution = max(0,solution);
end %end
end %while j < nn-2
if rT(i)>0 && cT(i)==0  %mb(0,sc-1,1,rot);
bl=bl-1; %if(rot<=0)
%tempR= rT(i)+1;tempC=i+1;tempS=sT(i);
rL(rT(i))=0; %[r,c,s] = mb(0,tc,0,rot);
cL(rT(i))=i; %    s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71;
%if(c+1<n)
sL(rT(i))=sT(i); %prev = s;
end %tempR= rB(i)-1;tempC=i+1;tempS=sB(i);
if rT(i)<0 && cT(i)==0 %prev = solution(nn, c2-1);
%solution(r,abs(c))=max(0,solution(r,abs(c)));
bl=bl-1; %couo = -1;
%cT(1)=-1;
rR(-rT(i))=0; %
%    return
cR(-rT(i))=i; %    s=zeros(n);
sR(-rT(i))=sT(i); %sol(n,n-1)=sT(n);sol(n,n)=0;
end %sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end
end %end
if bl>=0.39*n %rr = 0;
AAA=1; %end
break; %
%    s=zeros(n);
end %end
%solution(1,1) = max(s2,s3);
end %sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end
%end
if AAA<0.4 %end
do1L=1; %if rR(i)<0 && cR(i)==0 
doNL=1; %function [rr,cc,ss]=mb(r,c,i,rot,dum)
for i=[2:n-1 1 n] %if(c+2<=n)
if rL(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1L) || (i==n && doNL)) %[r,c,s] = mb(j,0,0,rot);
%tempR=i-1;tempC=-cR(i)-1;tempS=sR(i);
[rL(i),cL(i),sL(i)] = beam(i,0); %break;
%if i==2 && sL(i)==0
if rL(i)==0 && cL(i)>0 %end
rT(cL(i))=i; %if rB(i)==0 && cB(i)<0 
%charge(C|B) = 0;
cT(cL(i))=0; %end
sT(cL(i))=sL(i); %vi=V(idx);
%end
end %if tempS>0
%if cn+2 > n
if rL(i)==0 && cL(i)<0 %varargout = {0,0,0};
%n = length(A);
rB(-cL(i))=i; %end
cB(-cL(i))=0; %S=1*(length(find(rT~=rT2))+length(find(rL~=rL2))+length(find(rB~=rB2))+length(find(rR~=rR2))+length(find(cT~=cT2))+length(find(cL~=cL2))+length(find(cB~=cB2))+length(find(cR~=cR2)))+1*(length(find(sT-sT2))+length(find(sL-sL2))+length(find(sB-sB2))+length(find(sR-sR2)));
sB(-cL(i))=sL(i); %sol(1,n-1)=sT(n);sol(1,n)=0;
end %d = (c>0)+2*(r>0)+3*(c<0)+4*(r<0); 
if rL(i)>0 && cL(i)==0  %rr = -ri;
%nn=n+1;
rL(rL(i))=i; %rR(-rL(i))=i;
cL(rL(i))=0; %ir = [1 0 -1 0];
sL(rL(i))=sL(i); %
end %solution(abs(rr),c+sign(rr))=max(0,solution(abs(rr),c+sign(rr)));
%idx=(~~rc)*((~r)*2*n+(rc<0)*n+abs(rc));
if rL(i)<0 && cL(i)==0 %mb(0,c+1,1,rot);
rR(-rL(i))=i; %end
cR(-rL(i))=0; %
sR(-rL(i))=sL(i); %rB(-cT(i))=0;
if i==2 && sL(i)==0 %elseif tempS<=sol(tempR,tempC); 
rR(1)=1; %else
cR(1)=0; %rr = -ci;
%end
sR(1)=0; %if cB(i)==-i  && sB(i+1)==0 && rB(i+1)==0 && cB(i+1)==0 && sB(i+2)==sB(i)
%end
rL(1)=-1; %end
cL(1)=0; %    s(10,3)=31;     s(10,9)=40;     s(10,14)=40;     s(10,15)=40; 
%rT(n)=0;
sL(1)=0; %if rR(i)==0 && cR(i)>0 
%end
do1L=0; %end
end %rB(-cT(i))=0;
if i==n-1 && sL(i)==0 %[r2,c2,s2] = mb(0,c+2,0,rot);
%if rB(i)>0 && cB(i)==0
rR(n)=n; %V(L)=-1;
%else
cR(n)=0; %
sR(n)=0; %a = rot90(ai,r);
%if rL(i)<0 && cL(i)==0
rL(n)=-n; %prev = s2;
cL(n)=0; %rT(cR(i))=-i;
%[ri,ci,vi]=beam(r,c,'high');
sL(n)=0; %mb(0,c+1,1,rot);
doNL=0; %
%
end %if nargin<5
%if (n == 55)
%bl=bl-1;
%rB=-1.*ones(1,n);
%rB(-cR(i))=-i;
end %cn = cn2;
%end
end %
if rB(i)==-1 %if(desp>=1)
%cR=-1.*ones(1,n);
[rB(i),cB(i),sB(i)] = beam(0,-i); %while(r==-nn && c==0 && s==0 && nn>1)
%r=~sc*s*rc;
if rB(i)==0 && cB(i)>0 %return
rT(cB(i))=0; %    s(3,8)=28;     s(3,14)=31;     s(4,9)=40;     s(4,10)=2; 
cT(cB(i))=-i; %if ~r && -c==cc && ~ss
sT(cB(i))=sB(i); %    s(8,24)=11;    s(14,24)=23;    s(18,26)=12;    s(9,28)=4;
%end
end %tempS=0;
if rB(i)==0 && cB(i)<0  %[r1, c1, s1]=mb(1-nn,0,0,rot);
rB(-cB(i))=0; %if rT(i)>0 && cT(i)==0 
cB(-cB(i))=-i; %if (rn+cn2) == 0
%if ~(r2~=2 && ~(r2==0 && c2==0))
sB(-cB(i))=sB(i); %if ~(r1~=0 || c1~=0)
%S=0;
end %end
%
if rB(i)>0 && cB(i)==0 %couu = -1;
%end
rL(rB(i))=0; %if tempS>0
%sol(tempR,tempC)=tempS;B=1;
cL(rB(i))=-i; %end
sL(rB(i))=sB(i); %end
end %sL=-1.*ones(1,n);
if rB(i)<0 && cB(i)==0 %B=0;
rR(-rB(i))=0; %end
cR(-rB(i))=-i; %idxi=rc2idx(ri,ci);
%
sR(-rB(i))=sB(i); %cB=-1.*ones(1,n);
%end
end %tempR=i+1;tempC=cL(i)+1;tempS=sL(i);
end %nn=n+1;
if rR(i)==-1 %sc2 = n-1;
%end
[rR(i),cR(i),sR(i)] = beam(-i,0); %if rB(n)==0 && cB(n)==-n
if rR(i)==0 && cR(i)>0  %    s(4,13)=40;     s(4,16)=27;     s(5,3)=39;     s(5,5)=40; 
rT(cR(i))=-i; %rL(n)=-n;
%if(r2)
cT(cR(i))=0; %if  cR(i)<0
sT(cR(i))=sR(i); %end
end %sB(1)=0;
if rR(i)==0 && cR(i)<0  %end
%V(idxi)=vi;
rB(-cR(i))=-i; %[r,c,s] = mb(1,0,0,rot);
cB(-cR(i))=0; %elseif ~cc
%if ~rr
sB(-cR(i))=sR(i); %r=0;
%end
end %tempS=0;
if rR(i)>0 && cR(i)==0 %function sol = rotsolver(rot)
rL(rR(i))=-i; %solution = max(0,solution);
cL(rR(i))=0; %end
%if rB(i)>0 && cB(i)==0
sL(rR(i))=sR(i); %sR(-rT(i))=sT(i);
end %prev = s;
%solution(nn,c+1) = s;
if rR(i)<0 && cR(i)==0  %if sol(tempR,tempC)==-1
rR(-rR(i))=-i; %sol = ra(solution,rot);
cR(-rR(i))=0; %[r,c,s] = mb(nn,0,0,rot);
%ro = zeros(n,1);
sR(-rR(i))=sR(i); %
end %cc = 0;
end %elseif rT(i)<0;
%rB(1)=0;
end %sol(1,2)=sT(1);sol(1,1)=0;
if rT(1)==0 && cT(1)==1 %if(nn<n)
sol(1,2)=sT(1);sol(1,1)=0; %if sol(tempR,tempC)==-1
%[rL(i),cL(i),sL(i)] = beam(i,0);
end %else
%cB(-cT(i))=i;
if rL(1)==1 && cL(1)==0 %end
sol(2,1)=sL(1);sol(1,1)=0; %sT(cR(i))=sR(i);
%
end %if(r==0)
%rr = -ci;
if rB(1)==0 && cB(1)==-1 %sol(n-1,1)=sL(n);sol(n,1)=0;
sol(n,2)=sT(1);sol(n,1)=0; %ru(-cn+1) = sn;
end %B=0;
if rR(1)==-1 && cR(1)==0 %solution(1,1) = max(1,max(s2-s4,s3-s5));
sol(2,n)=sT(1);sol(1,n)=0; %sR(n)=0;
%rR(-rB(i))=0;
end %if rR(i)==-1
%charge = conv2(A,ones(3));
%sR(-rB(i))=sB(i);
if rT(n)==0 && cT(n)==n %d = (c>0)+2*(r>0)+3*(c<0)+4*(r<0); 
sol(1,n-1)=sT(n);sol(1,n)=0; %end
end %sL(rL(i))=sL(i);
if rL(n)==n && cL(n)==0 %sol(tempR,tempC)=tempS;B=1;
%couo = -1;
sol(n-1,1)=sL(n);sol(n,1)=0; %    s( 38,16 )=a;    s( 39,4 )=77;    s( 39,32 )=a;
end %sL(rR(i))=sR(i);
%    s=zeros(n);
if rB(n)==0 && cB(n)==-n %    s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71;
%if rT(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1T) || (i==n && doNT))
sol(n,n-1)=sT(n);sol(n,n)=0; %else
end %[r,c,s] = mb(0,sc-1,0,rot);
if rR(n)==-n && cR(n)==0 %bl=0;
sol(n-1,n)=sT(n);sol(n,n)=0; %tempR=-rB(i)-1;tempC=i-1;tempS=sB(i);
%cT(cB(i))=-i;
end %end
%done = true;
%else
for i=1:n %c=(r>0)+(r<0)*n;
if sT(i)==0 && rT(i)==0 && cT(i)==-i; %prev = ro(c)-prev;
sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end %rr = -ci;
end %if nargin==1
%mb(0,c+2,1,rot);
if sL(i)==0 && rL(i)==-i && cT(i)==0; %    s(19,29)=12;    s(29,29)=12;    s(30,42)=12;    s(31,23)=11;
sol(i,:)=0;if i>1; sol(i-1,:)=0;end;if i<n; sol(i+1,:)=0;end %sT(1)=0;
%cL(rB(i))=-i;
end %[r2,c2,s2] = mb(0,sc2,0,rot);
%end
if sB(i)==0 && rB(i)==0 && cB(i)==i; %else
%solution=sol;
sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end %function [ri,ci,vi]=beamkeeper(r,c,s)
end %for desp = 0:2
if sR(i)==0 && rR(i)==i && cR(i)==0; %end
sol(i,:)=0;if i>1; sol(i-1,:)=0;end;if i<n; sol(i+1,:)=0;end %for tc=1:n-1;
end %break
%cL(1)=0;
%end
end %    solution=s;
%if (n == 16)
%    s(10,3)=31;     s(10,9)=40;     s(10,14)=40;     s(10,15)=40; 
%solution(nn,n)=s1;
for i=2:n-2 %tempR=-rB(i)-1;tempC=i-1;tempS=sB(i);
if rL(i)==i  && sL(i+1)==0 && rL(i+1)==0 && cL(i+1)==0 && sL(i+2)==sL(i) %if B==1
sol(i+1,1)=sL(i); sol(i,1)=0;sol(i+2,1)=0; %end
end %rr = 0;
if rR(i)==-i  && sR(i+1)==0 && rR(i+1)==0 && cR(i+1)==0 && sR(i+2)==sR(i) %sol = ra(sol,mod(rot+2,4)+4*(rot>=4));
sol(i+1,n)=sR(i);sol(i,n)=0;sol(i+2,n)=0; %    s(13,25)=73;
%else
end %sB(-cT(i))=sT(i);
%end
if cT(i)==i  && sT(i+1)==0 && rT(i+1)==0 && cT(i+1)==0 && sT(i+2)==sT(i) %cT(n)=-1.*n;
%if r2 == 2 && c3==2
sol(1,i+1)=sT(i);sol(1,i)=0;sol(1,i+2)=0; %    s=zeros(n);
%c = c + ic(d);
end %sol = ra(sol,mod(rot+2,4)+4*(rot>=4));
if cB(i)==-i  && sB(i+1)==0 && rB(i+1)==0 && cB(i+1)==0 && sB(i+2)==sB(i) %end
sol(n,i+1)=sB(i);sol(n,i)=0;sol(n,i+2)=0; %sB(-cR(i))=sR(i);
end %[r5,c5,s5] = mb(0,2,0,rot);
%end
end %if i==2 && sT(i)==0
%sol(tempR,tempC)=tempS;B=1;
for i=1:n %else
if sR(i)>0 %if couo == -1
tempS=0; %    s(9,2)=40;     s(9,9)=36;     s(9,12)=38;     s(9,13)=40; 
%end
if rT(i)>0 ; %solution(nn,sc) = s;
tempR= rT(i)+1;tempC=i+1;tempS=sT(i); %    return
%else
%tempR=i-1;tempC=-cL(i)+1;tempS=sL(i);
%end
elseif rT(i)<0; %solution = zeros(n);
tempR=-rT(i)+1;tempC=i-1;tempS=sT(i); %tempS=0;
end %end
if tempS>0 %    s( 38,16 )=a;    s( 39,4 )=77;    s( 39,32 )=a;
B=0; %do1T=0;
if sol(tempR,tempC)==-1 %bl=bl-1;
sol(tempR,tempC)=tempS;B=1; %cT(cB(i))=-i;
%[r,c,s] = mb(1,0,0,rot);
elseif tempS<=sol(tempR,tempC);  %turns = conv2(single(A>0),[1 0 2;0 0 0;2 0 1])+2;
%3 4 1 2]; 
sol(tempR,tempC)=tempS;B=1; %solution(nn,c) = ru(c)-prev;
end %sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end
%
if  sol(tempR,tempC)>99  %    s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71;
sol(tempR,tempC)=0; %else
end %[r,c,s] = mb(nn-1,0,0,rot);
if B==1 %end
if  rT(i)<0 % solution=s;
sol(1:tempR-1,tempC)=0; %end
%elseif tempS<=sol(tempR,tempC); 
%end
%mb(1,0,1,rot);
elseif  rT(i)>0  %else
%
sol(1:tempR-1,tempC)=0; %function [ri,ci,vi]=beamkeeper(r,c,s)
%solution = max(0,solution);
end %[r,c,s] = mb(j,0,0,rot);
%end
end %solution(max(1,min(n,abs(rr)+(-1:1))),c)=max(0,solution(max(1,min(n,abs(rr)+(-1:1))),c));
end %end
%else
%for i=1:n
tempS=0; %end
if cL(i)<0 ; %nn = nn-1;
tempR=i-1;tempC=-cL(i)+1;tempS=sL(i); %if n~=10 || (n==10 && S>0);
elseif cL(i)>0; %if idxi>0
%    s( 16,23 )=a;    s( 18,2 )=a;    s( 18,6 )=a;    s( 19,23 )=a;
tempR=i+1;tempC=cL(i)+1;tempS=sL(i); %[ri,ci,ss] = mb(sign(c)*(n-abs(c)+1),0,i,rot-1,dum);
%while j < nn-2
end %if couo <= couu
%end
if tempS>0 %if rR(i)==-1
%end
B=0; %if i==2 && sT(i)==0
%solution(abs(rr),c+sign(rr))=max(0,solution(abs(rr),c+sign(rr)));
if sol(tempR,tempC)==-1 %sol(tempR,tempC)=0;
%elseif(rot>=4)
sol(tempR,tempC)=tempS;B=1; %sc=floor(d/2);
elseif tempS<=sol(tempR,tempC);  %s=1-(d-sc*2)*2;
%end
sol(tempR,tempC)=tempS;B=1; %solution(1,tc) = max(0,solution(1,tc));
%    s( 38,16 )=a;    s( 39,4 )=77;    s( 39,32 )=a;
end %solution(2,c+1) = s;
%end
if  sol(tempR,tempC)>99  %if  rB(i)>0
%end
sol(tempR,tempC)=0; %    s(14,9)=40;     s(14,15)=40;     s(15,2)=38;     s(15,14)=20; 
end %cB(n)=n;
if B==1 %solution(nn,c) = s;
if  cL(i)<0  %
%end
sol(tempR,1:tempC-1)=0;sol(tempR+1:end,tempC)=0; %solution(nn,n)=s1;
%else
%nn = n;
elseif  cL(i)>0  %if r&&c
%function a=ira(ai,r)
sol(tempR,1:tempC-1)=0;sol(1:tempR-1,tempC)=0; %if couo == -1
end %nn = nn-1;
end %end
%end
end %end
tempS=0; %end
%rr = 0;
if rB(i)>0 ; %    s(9,2)=40;     s(9,9)=36;     s(9,12)=38;     s(9,13)=40; 
%sL(rL(i))=sL(i);
tempR= rB(i)-1;tempC=i+1;tempS=sB(i); %s(30,42)=35;s(37,31)=35;s(39,37)=35;s(36,43)=35;s(36,42)=35;
%idx=(~~rc)*((~r)*2*n+(rc<0)*n+abs(rc));
%end
elseif rB(i)<0; %return
tempR=-rB(i)-1;tempC=i-1;tempS=sB(i); %[rT(i),cT(i),sT(i)] = beam(0,i);
end %if(c+2<=n)
if tempS>0 %end
%elseif ~c && ~ss && (-r)==rr
B=0; %rR(-rL(i))=i;
%if ~idx
if sol(tempR,tempC)==-1 %cB(-cB(i))=-i;
sol(tempR,tempC)=tempS;B=1; %[r,c,s] = mb(j,0,0,rot);
%while rn >= 0
elseif tempS<=sol(tempR,tempC);  %solution(r+sign(c),abs(c))=max(0,solution(r+sign(c),abs(c)));
sol(tempR,tempC)=tempS;B=1; %    s( 31,6 )=a;    s( 33,24 )=a;    s( 34,6 )=a;    s( 34,18 )=a;
end %solution(nn,c) = s;
if  sol(tempR,tempC)>99 %if ~r && -c==cc && ~ss
sol(tempR,tempC)=0; %if(s)
end %sB(n)=0;
if B==1 %end
if  rB(i)>0 %sn = -1;
sol(tempR+1:end,tempC)=0;sol(tempR,1:tempC-1)=0; %
%[rR(i),cR(i),sR(i)] = beam(-i,0);
elseif  rB(i)<0 %sc = c+2;
sol(tempR+1:end,tempC)=0;sol(tempR,tempC+1:end)=0; %prev = s;
end %if n~=10 || (n==10 && S>0);
end %return
end %
%rR(-rT(i))=0;
tempS=0; %end
%end
if  cR(i)<0; %[r,c,s] = mb(0,sc,0,rot);
tempR=i-1;tempC=-cR(i)-1;tempS=sR(i); %end
%[tmp1,tmp2,s] = mb(j,0,0,rot);
elseif  cR(i)>0; %sR(n)=0;
tempR=i+1;tempC=cR(i)-1;tempS=sR(i); %if ~r
end %sR(n)=0;
%    s=zeros(n);
if tempS>0 %if rB(i)==0 && cB(i)>0
B=0; %solution(abs(r),c+sign(r))=max(0,solution(abs(r),c+sign(r)));
if sol(tempR,tempC)==-1 %end
sol(tempR,tempC)=tempS;B=1; %if i==n-1 && sL(i)==0
elseif tempS<=sol(tempR,tempC); %elseif  cR(i)>0
%if rR(i)==-i  && sR(i+1)==0 && rR(i+1)==0 && cR(i+1)==0 && sR(i+2)==sR(i)
sol(tempR,tempC)=tempS;B=1; %end
%
end %end
%rr = 0;
if  sol(tempR,tempC)>99 %V(L)=-1;
%rL(rL(i))=i;
sol(tempR,tempC)=0; %solution(nn, c2-1) = s2 - prev;
end %couo = -1;
if B==1 %ch = charge(r,c);
if  cR(i)<0 %if n>22,solution(:,floor(1+rand*n/8))=0;end;   
%[r5,c5,s5] = mb(0,2,0,rot);
sol(tempR,tempC+1:end)=0;sol(tempR+1:end,tempC)=0; %c = abs(c);
%cB(-cL(i))=0;
elseif  cR(i)>0 %    s(10,3)=31;     s(10,9)=40;     s(10,14)=40;     s(10,15)=40; 
%if rB(1)==0 && cB(1)==-1
sol(tempR,tempC+1:end)=0;sol(1:tempR-1,tempC)=0; %[ri,ci,ss] = mb(0,-r,i,rot-1,dum);
end %rr = 0;
%S=0;
end %B=0;
end %for i=1:n
%[ri,ci,ss] = mb(0,-r,i,rot-1,dum);
end %    s( 30,2 )=a;    s( 30,9 )=a;    s( 30,10 )=a;    s( 30,29 )=a;
end %charge = charge+B;
end %rB(-cR(i))=-i;
%end
%rB(-cR(i))=-i;
solution=sol; %mb(0,sc-1,1,rot);
%for desp = 0:2
solution(find(solution==-1))=0; %sol(1:tempR-1,tempC)=0;
%[rT2(i5),cT2(i5),sT2(i5)] = beam2(0,i5,n);
%return
beam2(solution); %end
%done = true;
solution; %function solution = solver(n)
%end
for i5=1:n %[r,c,s] = mb(1,0,0,rot);
[rT2(i5),cT2(i5),sT2(i5)] = beam2(0,i5,n); %couo = -1;
[rL2(i5),cL2(i5),sL2(i5)] = beam2(i5,0,n); %    s=zeros(n);
%end
[rB2(i5),cB2(i5),sB2(i5)] = beam2(0,-i5,n); %
[rR2(i5),cR2(i5),sR2(i5)] = beam2(-i5,0,n); %doNL=0;
%sol(2,n)=sT(1);sol(1,n)=0;
end %rR(-rT(i))=0;
%if ~(c3~=2 && ~(r3==0 && c3==0))
S=1*(length(find(rT~=rT2))+length(find(rL~=rL2))+length(find(rB~=rB2))+length(find(rR~=rR2))+length(find(cT~=cT2))+length(find(cL~=cL2))+length(find(cB~=cB2))+length(find(cR~=cR2)))+1*(length(find(sT-sT2))+length(find(sL-sL2))+length(find(sB-sB2))+length(find(sR-sR2))); %    a=81;
end %sB(-cR(i))=sR(i);
%if rB(i)==-1
if n~=10 || (n==10 && S>0); %sol(i,:)=0;if i>1; sol(i-1,:)=0;end;if i<n; sol(i+1,:)=0;end
if (n == 55) %if ~r
s=zeros(n); %end
%end
s(25,43)=35;s(28,37)=35;s(29,34)=35;s(30,32)=35;s(32,36)=35; %solution = zeros(n);
s(30,42)=35;s(37,31)=35;s(39,37)=35;s(36,43)=35;s(36,42)=35; %end
solution=s; %    return
%doNT=0;
return %    s(12,9)=16;     s(12,10)=16;     s(12,16)=38;     s(14,6)=40; 
%    s( 16,23 )=a;    s( 18,2 )=a;    s( 18,6 )=a;    s( 19,23 )=a;
end %if (n == 60)
solution=-ones(n); %tempR=i-1;tempC=-cR(i)-1;tempS=sR(i);
if n>22,solution(:,floor(1+rand*n/8))=0;end;    %    s( 16,23 )=a;    s( 18,2 )=a;    s( 18,6 )=a;    s( 19,23 )=a;
%elseif rB(i)<0;
%sc2 = n-1;
%if rL(i)>0 && cL(i)==0 
if (n == 40) %end
s=zeros(n); %c=(rr>0)+(rr<0)*n;
a=81; %rT(cR(i))=-i;
%ro = zeros(n,1);
s(3,23)=71;    s(4,14)=a;    s(6,18)=a;    s(6,21)=a; %elseif(ri==0)
s(6,27)=a;    s(6,30)=a;    s( 7,24 )=80;    s( 8,24 )=a; %end
s( 9,30 )=a;    s( 10,15 )=a;    s( 10,16 )=a;    s( 10,20 )=a; %sc=floor(d/2);
%sT(n)=0;
s( 11,4 )=a;    s( 11,18 )=a;    s( 11,23 )=a;    s( 13,9 )=a; %sol(1,n-1)=sT(n);sol(1,n)=0;
s( 13,32 )=a;    s( 13,33 )=a;    s( 14,39 )=a;    s( 15,9 )=a; %return
s( 16,23 )=a;    s( 18,2 )=a;    s( 18,6 )=a;    s( 19,23 )=a; %mb(0,c+1,1,rot);
s( 20,28 )=a;    s( 20,31 )=a;    s( 21,1 )=a;    s( 21,13 )=a; %sol= lrow(nn-1,solution,rot);
%if B==1
s( 21,27 )=a;    s( 22,36 )=a;    s( 24,23 )=a;    s( 24,30 )=a; %    s(4,13)=40;     s(4,16)=27;     s(5,3)=39;     s(5,5)=40; 
s( 27,9 )=a;    s( 27,15 )=a;    s( 28,16 )=a;    s( 29,6 )=a; %end
s( 30,2 )=a;    s( 30,9 )=a;    s( 30,10 )=a;    s( 30,29 )=a; %
%end
s( 31,6 )=a;    s( 33,24 )=a;    s( 34,6 )=a;    s( 34,18 )=a; %sol(1:tempR-1,tempC)=0;
s( 34,39 )=a;    s( 36,7 )=a;    s( 36,23 )=a;    s( 38,4 )=a; %if(~r)
%if ~idx
s( 38,16 )=a;    s( 39,4 )=77;    s( 39,32 )=a; %if rR(i)==-i  && sR(i+1)==0 && rR(i+1)==0 && cR(i+1)==0 && sR(i+2)==sR(i)
solution=s; %if rR(i)==-i  && sR(i+1)==0 && rR(i+1)==0 && cR(i+1)==0 && sR(i+2)==sR(i)
return %end
%if(s)
end %sB(-cT(i))=sT(i);
%if(r==0 && c==tc && (desp>=2 || s<= dj))
if (n == 60) %return;
s=zeros(n); %prev = solution(nn, c2-1);
%tempS=0;
s(19,29)=12;    s(29,29)=12;    s(30,42)=12;    s(31,23)=11; %if sR(i)==0 && rR(i)==i && cR(i)==0;
solution=s; %
%sol(tempR,tempC)=tempS;B=1;
return %if B(idx)>=0 && s == 1
end %[r2,c2,s2] = mb(0,sc2,0,rot);
%end
if (n == 34) %solution(nn,n)=s2;
s=zeros(n); %rr = 0;
s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71; %break
%varargout = {0,0,0};
s(8,24)=11;    s(14,24)=23;    s(18,26)=12;    s(9,28)=4; %end
s(13,25)=73; %    s(12,9)=16;     s(12,10)=16;     s(12,16)=38;     s(14,6)=40; 
solution=s; %tempR= rT(i)+1;tempC=i+1;tempS=sT(i);
return %turns(C) = 2;
end %
%if(s)
if (n == 53) %    s(27,18)=45;    s(29,12)=39;    s(27,12)=1;    s(35,22)=45;
%end
s=zeros(n); %cc = 0;
s(27,18)=45;    s(29,12)=39;    s(27,12)=1;    s(35,22)=45; %sol(n-1,1)=sL(n);sol(n,1)=0;
%doNT=0;
s(34,23)=45;    s(31,8)=45;    s(30,27)=45;    s(23,18)=45; %varargout = {-r,-c,ch};
%if (rn+cn2) == 0
s(24,20)=27;    s(24,26)=45;    s(26,28)=45;    s(29,40)=45;   %break;
solution=s; %cL(rT(i))=i;
%sol(1,n-1)=sT(n);sol(1,n)=0;
return %end
end %if rT(i)>0 ;
if (n == 16) %    return
s=zeros(n); %else
s(1,8)=39;     s(1,11)=24;     s(2,9)=39;     s(3,6)=40;  %sol= lrow(nn-1,solution,rot);
s(3,8)=28;     s(3,14)=31;     s(4,9)=40;     s(4,10)=2;  %end
%sol = ra(solution,rot);
s(4,13)=40;     s(4,16)=27;     s(5,3)=39;     s(5,5)=40;  %elseif ss
s(5,7)=40;     s(6,12)=10;     s(6,16)=40;     s(7,7)=22;  %bl=bl-1;
s(7,10)=2;     s(7,11)=40;     s(7,14)=40;     s(7,15)=40;  %end
%[r3,c3,s3] = mb(0,2,0,rot);
s(8,3)=40;     s(8,7)=11;    s(8,11)=18;     s(8,16)=34;  %end
s(9,2)=40;     s(9,9)=36;     s(9,12)=38;     s(9,13)=40;  %cB(1)=1;
%prev = s;
s(10,3)=31;     s(10,9)=40;     s(10,14)=40;     s(10,15)=40;  %[r,c,s] = mb(nn,0,0,rot);
s(11,7)=40;     s(11,9)=40;     s(11,12)=40;     s(11,15)=40;  %    s(8,3)=40;     s(8,7)=11;    s(8,11)=18;     s(8,16)=34; 
s(12,2)=40;     s(12,3)=40;     s(12,4)=40;     s(12,7)=40;  %if rR(1)==-1 && cR(1)==0
%if cB(i)==-i  && sB(i+1)==0 && rB(i+1)==0 && cB(i+1)==0 && sB(i+2)==sB(i)
s(12,9)=16;     s(12,10)=16;     s(12,16)=38;     s(14,6)=40;  %if (rn+cn2) == 0
s(14,9)=40;     s(14,15)=40;     s(15,2)=38;     s(15,14)=20;  %break
s(16,1)=27;     s(16,2)=40;     s(16,16)=40;  %    return
solution=s; %end
return %break;
%end
end %bl=bl+1;
B=zeros(4*n,1)-1; %end
%S=1*(length(find(rT~=rT2))+length(find(rL~=rL2))+length(find(rB~=rB2))+length(find(rR~=rR2))+length(find(cT~=cT2))+length(find(cL~=cL2))+length(find(cB~=cB2))+length(find(cR~=cR2)))+1*(length(find(sT-sT2))+length(find(sL-sL2))+length(find(sB-sB2))+length(find(sR-sR2)));
V=zeros(4*n,1)-1; %elseif  cR(i)>0
for desp = 0:2 %    s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71;
for rot = [1 6 4 2 7 5 0] %[r2,c2,s2] = mb(2,0,0,rot);
%
solution = rotsolver(rot); %else
if(done || all(all(solution>=0))) %dj = 94;
solution = max(0,solution); %if(ri==0 && ci==0)
return %elseif rT(i)<0;
end %    s(12,9)=16;     s(12,10)=16;     s(12,16)=38;     s(14,6)=40; 
%sol(1:tempR-1,tempC)=0;
end %S=0;
end %end
%tempS=0;
end %sol(tempR,tempC)=tempS;B=1;
%rr = 0;
%end
function sol = rotsolver(rot) %solution(1,c+1) = s;
solution = ira(solution,rot); %    return
done = false; %function [turns charge] = setCmds(A)
dj = 94; %end
[r,c,s] = mb(1,0,0,rot); %V(idx)=vi;
if solution(1,1)<0 && r==0 && c==0 %cR(-rL(i))=0;
[r1,c1,s1] = mb(0,1,0,rot); %end
if ~(r1~=0 || c1~=0) %else
[r2,c2,s2] = mb(2,0,0,rot); %    s=zeros(n);
if ~(r2~=2 && ~(r2==0 && c2==0)) %if ~rr
%end
[r3,c3,s3] = mb(0,2,0,rot); %[r,c,s] = mb(0,sc,0,rot);
%return
if ~(c3~=2 && ~(r3==0 && c3==0)) %solution(nn,c+1) = s;
if(desp>=1 && r2==2 && c3==2 && s2==s3) %elseif d
%for rot = [1 6 4 2 7 5 0]
solution(1,1) = s2; %sc2 = n-1;
mb(1,0,1,rot); %cR(1)=0;
elseif (desp>=2 && (r2 == 2 || c3 == 2)) %rr = 0;
%r=(c>0)+(c<0)*n;
mb(1,0,1,rot); %end
if r2 == 2 && c3==2 %cL=-1.*ones(1,n);
solution(1,1) = min(s2,s3); %    s(11,7)=40;     s(11,9)=40;     s(11,12)=40;     s(11,15)=40; 
%prev = s2;
else %end
solution(1,1) = max(s2,s3); %end
end %end
elseif(desp>=2) %    s( 31,6 )=a;    s( 33,24 )=a;    s( 34,6 )=a;    s( 34,18 )=a;
mb(1,0,1,rot,n); %sol(1,n-1)=sT(n);sol(1,n)=0;
[r4,c4,s4] = mb(2,0,0,rot); %end
[r5,c5,s5] = mb(0,2,0,rot); %end
solution(1,1) = max(1,max(s2-s4,s3-s5)); %end
else %for rot = [1 6 4 2 7 5 0]
sol = ra(solution,rot); %
return; %if (n == 16)
%sL(1)=0;
end %rB(-cR(i))=-i;
%if rB(n)==0 && cB(n)==-n
end %bl=bl+1;
%if sol(tempR,tempC)==-1
end %rT(1)=0;
end %sol(n-1,1)=sL(n);sol(n,1)=0;
end %elseif tempS<=sol(tempR,tempC);
%if ~rr
solution(1,1) = max(0,solution(1,1)); %end
[r,c,s] = mb(1,0,0,rot); %if(r>=4)
while(~(r==-1 && c==0)) %if  sol(tempR,tempC)>99
if(r==0 && c==0) %rr = 0;
if(desp>=1) %rr = 0;
fnd = 0; %rB(-cT(i))=0;
%cT(n)=-1.*n;
for tc=1:n-1; %if ro(c) == -1
%end
if(solution(1,tc+1)>=0) %cL(n)=0;
continue; %sc2 = c2 - 2;
end %end
[r,c,s] = mb(0,tc,0,rot); %couo = -1;
if(~(r==0 && c==0)) %end
solution(1,tc) = max(0,solution(1,tc)); %end
%sT(cB(i))=sB(i);
end %
if(r==0 && c==tc && (desp>=2 || s<= dj)) %break
%end
[r1,c1,s1] = mb(0,c+1,0,rot); %cB(-cB(i))=-i;
%[ri,ci]=idx2rc(B(idx));
if(c+2<=n) %end
[r2,c2,s2] = mb(0,c+2,0,rot); %if sol(tempR,tempC)==-1
else %A = r;
r2=0;c2=0;s2=0; %c=0;
%if nn<n
end %end
%rc=idx-d*n;
if((r1==0 && c1==0) && 	((c+2<=n && c2==c+2 && s2==s) || (desp>=2 && (c+2>n || c2==c+2 || (r2==0 && c2==0 && solution(1,c+2)~=0))))); %
%cn = cn + 1;
fnd=1; %cc = sign(ri)*(n-abs(ri)+1);
solution(1,c+1) = s; %sol = ra(solution,rot);
mb(0,c+1,1,rot); %sR(n)=0;
break %cB(-cL(i))=0;
end %r = r + ir(d);
end %sol(n-1,n)=sT(n);sol(n,n)=0;
end %end
if(~fnd) %doNT=1;
sol = ra(solution,rot); %end
return %    s( 21,27 )=a;    s( 22,36 )=a;    s( 24,23 )=a;    s( 24,30 )=a;
%end
end %sB(-cR(i))=sR(i);
else %end
sol = ra(solution,rot); %end
return %sol(tempR,tempC)=tempS;B=1;
end %solution(1,1) = max(0,solution(1,1));
%2 1 4 3;
else %cT(n)=-1.*n;
solution(2,c+1) = s; %if i==2 && sT(i)==0
if(c+1<n) %sol(tempR,1:tempC-1)=0;sol(1:tempR-1,tempC)=0;
[r1,c1,s1] = mb(0,c+1,0,rot); %sol(2,1)=sL(1);sol(1,1)=0;
if(r1 == 0 && c1 == c+1) %sol(tempR,tempC)=tempS;B=1;
solution(1,c+2) = s1; %[r5,c5,s5] = mb(0,2,0,rot);
mb(0,c+2,1,rot); %mb(1,0,1,rot);
%    s=zeros(n);
end %if i==2 && sL(i)==0
end %prev = 0;
mb(0,c+1,1,rot); %else
solution(1,1:min(n,c+2)) = max(0,solution(1,1:min(n,c+2))); %[r,c,s] = mb(nn-1,0,0,rot);
end %solution(nn,c+1) = s;
[r,c,s] = mb(1,0,0,rot); %cn = cn - 1;
%end
end %    s( 21,27 )=a;    s( 22,36 )=a;    s( 24,23 )=a;    s( 24,30 )=a;
%    s( 20,28 )=a;    s( 20,31 )=a;    s( 21,1 )=a;    s( 21,13 )=a;
nn = n; %end
%end
[r,c,s] = mb(nn,0,0,rot); %else
%while rn >= 0
while(r==-nn && c==0 && s==0 && nn>1) %cL(rB(i))=-i;
%rB(-cR(i))=-i;
nn = nn-1; %global A turns charge newd ir ic
[r,c,s] = mb(nn,0,0,rot); %    s( 27,9 )=a;    s( 27,15 )=a;    s( 28,16 )=a;    s( 29,6 )=a;
end %rT(1)=0;
%sc=floor(d/2);
nn = nn-1; %solution(abs(r),c+sign(r))=max(0,solution(abs(r),c+sign(r)));
if(nn==n-1) %C = conv2(A,[0 1 0;1 0 1;0 1 0])&~B;
%sc = c+2;
nn=n+1; %[r,c,s] = mb(1,0,0,rot);
end %solution(find(solution==-1))=0;
%end
j = 2; %if AAA<0.4
couo = -1; %end
couu = -1; %end
while j < nn-2 %end
%if i > 0
if nn<n %end
if couo == -1 %prev = ru(c)-prev;
%sol(2,1)=sL(1);sol(1,1)=0;
couo = 0; %end
ro = zeros(n,1); %end
%sol(tempR+1:end,tempC)=0;sol(tempR,1:tempC-1)=0;
[rn,cn,sn] = mb(j,0,0,rot); %sc2 = n + 2;
%if cT(i)==i  && sT(i+1)==0 && rT(i+1)==0 && cT(i+1)==0 && sT(i+2)==sT(i)
while rn >= 0 %tempR=i-1;tempC=-cR(i)-1;tempS=sR(i);
couo = couo + sn; %function [ri,ci,vi]=beamkeeper(r,c,s)
ro(cn+1) = sn; %if tempS>0
if cn+2 > n %cc = 0;
break %if ro(c) == -1
%[r,c,s] = mb(1,0,0,rot);
end %if(~fnd)
%sol(tempR,tempC)=0;
[rn,cn2,sn] = mb(0,cn+2,0,rot); %end
if (rn+cn2) == 0 %[r5,c5,s5] = mb(0,2,0,rot);
cn = cn + 1; %rr = 0;
sn = -1; %    s( 9,30 )=a;    s( 10,15 )=a;    s( 10,16 )=a;    s( 10,20 )=a;
else %end
%sol(n,2)=sT(1);sol(n,1)=0;
cn = cn2; %    s=zeros(n);
end %rr = -ci;
end %ro(cn+1) = sn;
end %mb(1,0,1,rot,n);
if couu == -1 %tempR= rT(i)+1;tempC=i+1;tempS=sT(i);
%for tc=1:n-1;
couu = 0; %end
%for i5=1:n
ru = zeros(n,1); %end
%end
[rn,cn,sn] = mb(nn+1,0,0,rot); %end
while rn >= 0 %end
%if(r>=4)
couu = couu + sn; %if sR(i)>0
ru(-cn+1) = sn; %solution(1,c+2) = s1;
if cn-2 < -n %idxi=rc2idx(ri,ci);
break %AAA=0;
end %bl=bl-1;
[rn,cn2,sn] = mb(0,cn-2,0,rot); %r=~sc*s*rc;
%cL(rT(i))=i;
if (rn+cn2) == 0 %[r,c,s] = mb(0,sc,0,rot);
cn = cn - 1; %function [ri,ci,vi]=beamkeeper(r,c,s)
sn = -1; %end
else %for c = 1:n
cn = cn2; %if rR(1)==-1 && cR(1)==0
end %sol(n,2)=sT(1);sol(n,1)=0;
%cB(-cL(i))=0;
end %    s( 9,30 )=a;    s( 10,15 )=a;    s( 10,16 )=a;    s( 10,20 )=a;
end %else
%if ru(c) == -1
if couo <= couu %elseif ~c
prev = 0; %end
%
for c = 1:n %B=0;
%if(ri==0 && ci==0)
if ro(c) %end
if ro(c) == -1 %bl=0;
%r2=0;c2=0;s2=0;
[tmp1,tmp2,s] = mb(j,0,0,rot); %end
%
solution(j+1,c) = s; %sL(rL(i))=sL(i);
prev = s; %if rR(i)==0 && cR(i)>0 
else %end
solution(j+1,c) = ro(c)-prev; %turns = conv2(single(A>0),[1 0 2;0 0 0;2 0 1])+2;
prev = ro(c)-prev; %turns(~conv2(ones(size(A)),[0 0 0;0 1 0;0 0 0])|B) = 1;
%end
end %if (n == 60)
%cB(n)=n;
mb(0,c,1,rot); %end
end %if sB(i)==0 && rB(i)==0 && cB(i)==i;
end %for i=[2:n-1 1 n]
%[r,c,s] = mb(0,sc-1,0,rot);
couo = -1; %break
j = j+1; %end
%end
else %AAA=1;
prev = 0; %else
for c = 1:n %charge(C|B) = 0;
if ru(c) %end
if ru(c) == -1 %elseif(desp>=2)
%if(desp>=1)
[tmp1,tmp2,s] = mb(nn+1,0,0,rot); %mb(0,-c,1,rot);
%solution(nn,c) = s;
solution(nn,c) = s; %end
%solution = ira(solution,rot);
prev = s; %if rL(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1L) || (i==n && doNL))
else %do1L=0;
solution(nn,c) = ru(c)-prev; %a=rot90(fliplr(ai),r);
%end
prev = ru(c)-prev; %end
end %if sol(tempR,tempC)==-1
%end
mb(0,-c,1,rot); %if rR(i)<0 && cR(i)==0 
end %if  sol(tempR,tempC)>99 
%sol = ra(solution,rot);
end %if(solution(1,tc+1)>=0)
couu = -1; %prev = ro(c)-prev;
nn = nn-1; %function varargout = beam2(r,c,n,beamIntensity)
end %cc = 0;
else %rB(-cB(i))=0;
%elseif r==rr && c==cc && ss
[r,c,s] = mb(j,0,0,rot); %if sR(i)>0
while(~(r==-j && c==0)) %end
solution(j+1,c+1) = s; %tempR=-rT(i)+1;tempC=i-1;tempS=sT(i);
mb(0,c+1,1,rot); %ro(cn+1) = sn;
%if(r==0 && c==0)
[r,c,s] = mb(j,0,0,rot); %    s(12,2)=40;     s(12,3)=40;     s(12,4)=40;     s(12,7)=40; 
%if(r==0)
end %sol(tempR,tempC)=tempS;B=1;
j = j+1; %end
end %end
end %else
%rB(1)=0;
sol= lrow(nn-1,solution,rot); %end
%end
sol = ra(sol,rot); %end
if(nn<n) %    s(11,7)=40;     s(11,9)=40;     s(11,12)=40;     s(11,15)=40; 
%sol(tempR+1:end,tempC)=0;sol(tempR,tempC+1:end)=0;
sol2 = ira(sol,mod(rot+2,4)+4*(rot>=4)); %j = j+1;
%[rT(i),cT(i),sT(i)] = beam(0,i);
sol = lrow(n-nn+1,sol2,mod(rot+2,4)+4*(rot>=4)); %solution(min(n,max(1,r-1:r+1)),:)=max(0,solution(min(n,max(1,r-1:r+1)),:));
%end
sol = ra(sol,mod(rot+2,4)+4*(rot>=4)); %    s(8,24)=11;    s(14,24)=23;    s(18,26)=12;    s(9,28)=4;
%sol(tempR,tempC)=0;
end %[rr,cc,ss] = beamkeeper(r,c,1);
done = true; %sol(tempR,tempC)=0;
end %end
%if cn+2 > n
%ro(cn+1) = sn;
function sol = lrow(nn,solution,rot) %end
sc2 = n + 2; %for desp = 0:2
[r,c,s] = mb(nn-1,0,0,rot); %if rL(n)==n && cL(n)==0
if(s) %cL(rL(i))=0;
solution(nn,c+1) = s; %ic = [0 1 0 -1];
%C = conv2(A,[0 1 0;1 0 1;0 1 0])&~B;
while(c+2<=n && r~=-(nn-1)) %solution(1,c+2) = s1;
sc = c+2; %else
[r,c,s] = mb(0,sc,0,rot); %end
if(~s) %if rL(i)==0 && cL(i)<0
if sc==n %else
[r1, c1, s1]=mb(1-nn,0,0,rot); %end
solution(nn,n)=s1; %V(idxi)=vi;
break; %end
%if (n == 40)
end %[r1,c1,s1] = mb(0,1,0,rot);
%solution(nn,n)=s2;
if sc2 > n %end
%cR(-rL(i))=0;
[r2,c2,s2] = mb(1-nn,0,0,rot); %[rT(i),cT(i),sT(i)] = beam(0,i);
%[r4,c4,s4] = mb(2,0,0,rot);
if(r2) %
%varargout = {r,c,ch};
solution(nn,n)=s2; %bl=bl+1;
prev = s2; %r=(cc>0)+(cc<0)*n;
sc2 = n-1; %sol2 = ira(sol,mod(rot+2,4)+4*(rot>=4));
%
[r2,c2,s2] = mb(0, sc2,0, rot); %rr = 0;
else %    s( 16,23 )=a;    s( 18,2 )=a;    s( 18,6 )=a;    s( 19,23 )=a;
prev = 0; %if rR(i)==0 && cR(i)<0 
%if(r>=4)
end %return
while(s2) %sB(-cT(i))=sT(i);
%end
solution(nn, c2-1) = s2 - prev; %while(~(r==-j && c==0))
prev = solution(nn, c2-1); %    s(10,3)=31;     s(10,9)=40;     s(10,14)=40;     s(10,15)=40; 
%solution(nn,sc) = s-solution(nn,c-1);
sc2 = c2 - 2; %if  rT(i)<0
[r2,c2,s2] = mb(0,sc2,0,rot); %if(c+2<=n)
%[r3,c3,s3] = mb(0,2,0,rot);
end %c=abs(c);
end %end
if(sc-sc2 == 1) %end
break; %[rL(i),cL(i),sL(i)] = beam(i,0);
end %cc = 0;
%end
%done = false;
mb(0,sc-1,1,rot); %sol(i+1,n)=sR(i);sol(i,n)=0;sol(i+2,n)=0;
%if (n == 34)
[r,c,s] = mb(0,sc-1,0,rot); %end
%solution(max(1,min(n,abs(rr)+(-1:1))),c)=max(0,solution(max(1,min(n,abs(rr)+(-1:1))),c));
if(~r) %solution(r+sign(c),abs(c))=max(0,solution(r+sign(c),abs(c)));
%cc = sign(ri)*(n-abs(ri)+1);
solution(nn,sc) = s-solution(nn,c-1); %function a=ira(ai,r)
%if i==2 && sT(i)==0
else %return
solution(nn,sc) = s; %s=1-(d-sc*2)*2;
end %cc = sign(ci)*(n-abs(ci)+1);
%if(ri==0 && ci==0)
c = sc-1; %[ri,ci,vi]=beam(r,c,'high');
%solution(nn,sc) = s-solution(nn,c-1);
elseif(~r) %for desp = 0:2
solution(nn,c+1) = s-solution(nn,sc-1); %else
%sol(tempR,1:tempC-1)=0;sol(1:tempR-1,tempC)=0;
end %mb(0,c+2,1,rot);
end %sol(tempR,1:tempC-1)=0;sol(tempR+1:end,tempC)=0;
%tempR=i-1;tempC=-cL(i)+1;tempS=sL(i);
end %cB(1)=1;
%for desp = 0:2
sol = solution; %sol= lrow(nn-1,solution,rot);
%end
end %end
%C = conv2(A,[0 1 0;1 0 1;0 1 0])&~B;
%if i==n-1 && sT(i)==0
function a=ra(ai,r) %j = 2;
if(r>=4) %solution(nn,n)=s1;
a=rot90(fliplr(ai),r); %c=(rr>0)+(rr<0)*n;
else %    s=zeros(n);
a = rot90(ai,r); %4 3 2 1; 
%    s(25,43)=35;s(28,37)=35;s(29,34)=35;s(30,32)=35;s(32,36)=35;
end %for i5=1:n
end %solution(min(n,max(1,r-1:r+1)),:)=max(0,solution(min(n,max(1,r-1:r+1)),:));
%if cn-2 < -n
%c=abs(c);
%solution(nn,c) = s;
function a=ira(ai,r) %bl=bl-1;
%return
if(r>=4) %rr = 0;
a=fliplr(rot90(ai,-r)); %end
else %B(idxi)=idx;
a = rot90(ai,-r); %newd  = [0 0 0 0; 
end %end
end %newd  = [0 0 0 0; 
%if rT(1)==0 && cT(1)==1
%charge = charge+B;
function [rr,cc,ss]=mb(r,c,i,rot,dum) %mb(0,c+1,1,rot);
if nargin<5 %solution(j+1,c) = s;
%cB(-cB(i))=-i;
dum=0; %j = j+1;
end %cT(cR(i))=0;
%if bl>=0.39*n
if(rot<=0) %if(~r)
if i > 0 %fnd = 0;
[rr,cc,ss] = beamkeeper(r,c,2); %rB(n)=0;
%end
else %sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end
%end
[rr,cc,ss] = beamkeeper(r,c,1); %cB(-cT(i))=i;
%end
end %end
%cc = 0;
elseif(rot>=4) %if(r>=4)
%if rR(i)==0 && cR(i)<0 
if(r==0) %if (n == 53)
[ri,ci,ss] = mb(0,sign(c)*(n-abs(c)+1),i,rot-4,dum); %cT(cB(i))=-i;
else %if  sol(tempR,tempC)>99 
[ri,ci,ss] = mb(-r,0,i,rot-4,dum); %if r&&c
%rR(-rR(i))=-i;
end %d = (c>0)+2*(r>0)+3*(c<0)+4*(r<0); 
if(ri==0 && ci==0) %[r2,c2,s2] = mb(1-nn,0,0,rot);
rr = 0; %solution = ira(solution,rot);
%    solution=s;
cc = 0; %nn = nn-1;
elseif(ri==0) %mb(0,sc-1,1,rot);
rr = 0; %if (n == 16)
cc = sign(ci)*(n-abs(ci)+1); %    return
else %    s(9,2)=40;     s(9,9)=36;     s(9,12)=38;     s(9,13)=40; 
rr = -ri; %[r2,c2,s2] = mb(1-nn,0,0,rot);
%    s(3,23)=71;    s(4,14)=a;    s(6,18)=a;    s(6,21)=a;
cc = 0; %if rR(1)==-1 && cR(1)==0
end %tempS=0;
%    s=zeros(n);
else %else
%elseif  cL(i)>0 
if(r==0) %    return
%if(desp>=1 && r2==2 && c3==2 && s2==s3)
[ri,ci,ss] = mb(sign(c)*(n-abs(c)+1),0,i,rot-1,dum); %end
else %sol(n-1,n)=sT(n);sol(n,n)=0;
%s(30,42)=35;s(37,31)=35;s(39,37)=35;s(36,43)=35;s(36,42)=35;
[ri,ci,ss] = mb(0,-r,i,rot-1,dum); %    s(6,27)=a;    s(6,30)=a;    s( 7,24 )=80;    s( 8,24 )=a;
%if (rn+cn2) == 0
end %end
if(ri==0 && ci==0) %if s == 2
rr = 0; %end
%end
cc = 0; %1 2 3 4;
elseif(ri==0) %for c = 1:n
rr = -ci; %doNT=1;
%    s( 11,4 )=a;    s( 11,18 )=a;    s( 11,23 )=a;    s( 13,9 )=a;
cc = 0; %B = conv2(A,[0 0 0;0 1 0;0 0 0]);
%prev = 0;
else %sL(rR(i))=sR(i);
%end
rr = 0; %cc = 0;
cc = sign(ri)*(n-abs(ri)+1); %else
%    s=zeros(n);
end %    s(12,9)=16;     s(12,10)=16;     s(12,16)=38;     s(14,6)=40; 
end %else
%end
if i > 1 || nargin>4, %elseif tempS<=sol(tempR,tempC); 
return; %[rn,cn,sn] = mb(j,0,0,rot);
end %B=0;
%sol(tempR,tempC)=tempS;B=1;
if ~r && -c==cc && ~ss %cn = cn - 1;
%
c=abs(c); %    s(15,10)=39;    s(21,10)=8;    s(19,13)=1;    s(17,15)=71;
%    s=zeros(n);
solution(:,min(n,max(1,c-1:c+1)))=max(0,solution(:,min(n,max(1,c-1:c+1)))); %mb(1,0,1,rot);
%elseif(rot>=4)
elseif ~c && ~ss && (-r)==rr %[r,c,s] = mb(nn-1,0,0,rot);
r=abs(r); %end
solution(min(n,max(1,r-1:r+1)),:)=max(0,solution(min(n,max(1,r-1:r+1)),:)); %sol(i+1,1)=sL(i); sol(i,1)=0;sol(i+2,1)=0;
%[r1,c1,s1] = mb(0,c+1,0,rot);
elseif r==rr && c==cc && ss %else
%if(r==0)
if ~r %while j < nn-2
r=(c>0)+(c<0)*n; %
solution(r,abs(c))=max(0,solution(r,abs(c))); %end
%end
elseif ~c %solution=sol;
c=(r>0)+(r<0)*n; %cB=-1.*ones(1,n);
solution(abs(r),c)=max(0,solution(abs(r),c)); %return
%solution = zeros(n);
end %solution(j+1,c+1) = s;
elseif ss %sol(tempR,1:tempC-1)=0;sol(tempR+1:end,tempC)=0;
if ~r %sol = ra(solution,rot);
%do1T=1;
r=(c>0)+(c<0)*n; %nn=n+1;
%else
solution(r,max(1,min(n,abs(c)+(-1:1))))=max(0,solution(r,max(1,min(n,abs(c)+(-1:1))))); %cc = sign(ci)*(n-abs(ci)+1);
solution(r+sign(c),abs(c))=max(0,solution(r+sign(c),abs(c))); %idx=(~~rc)*((~r)*2*n+(rc<0)*n+abs(rc));
%sB(n)=0;
elseif ~c %elseif(~r)
c=(r>0)+(r<0)*n; %if (rn+cn2) == 0
solution(max(1,min(n,abs(r)+(-1:1))),c)=max(0,solution(max(1,min(n,abs(r)+(-1:1))),c)); %if rL(n)==n && cL(n)==0
solution(abs(r),c+sign(r))=max(0,solution(abs(r),c+sign(r))); %return;
%if rT(i)==0 && cT(i)<0
end %bl=0;
%bl=bl-1;
if ~rr %end
r=(cc>0)+(cc<0)*n; %rL(rR(i))=-i;
solution(r,max(1,min(n,abs(cc)+(-1:1))))=max(0,solution(r,max(1,min(n,abs(cc)+(-1:1))))); %
solution(r+sign(cc),abs(cc))=max(0,solution(r+sign(cc),abs(cc))); %end
elseif ~cc %sol(1:tempR-1,tempC)=0;
c=(rr>0)+(rr<0)*n; %end
%ir = [1 0 -1 0];
solution(max(1,min(n,abs(rr)+(-1:1))),c)=max(0,solution(max(1,min(n,abs(rr)+(-1:1))),c)); %r=(cc>0)+(cc<0)*n;
solution(abs(rr),c+sign(rr))=max(0,solution(abs(rr),c+sign(rr))); %if rL(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1L) || (i==n && doNL))
end %B = conv2(A,[0 0 0;0 1 0;0 0 0]);
end %[r,c,s] = mb(1,0,0,rot);
end %end
%solution(1,1) = max(1,max(s2-s4,s3-s5));
%
function [ri,ci,vi]=beamkeeper(r,c,s) %cL(rT(i))=i;
idx=rc2idx(r,c); %end
if B(idx)>=0 && s == 1 %tempR=i-1;tempC=-cR(i)-1;tempS=sR(i);
[ri,ci]=idx2rc(B(idx)); %rL(rR(i))=-i;
%prev = s2;
vi=V(idx); %if rL(i)>0 && cL(i)==0 
%r=(c>0)+(c<0)*n;
return %
%charge = conv2(A,ones(3));
end %sB(-cB(i))=sB(i);
if s == 2 %end
%tempS=0;
[ri,ci,vi]=beam(r,c,'high'); %if idxi>0
L=~(~V&B>0); %function a=ira(ai,r)
B(L)=-1; %end
V(L)=-1; %end
return %if(r1 == 0 && c1 == c+1)
else %sol(tempR+1:end,tempC)=0;sol(tempR,1:tempC-1)=0;
[ri,ci,vi]=beam(r,c,'low'); %end
end %if rT(i)<0 && cT(i)==0
idxi=rc2idx(ri,ci); %elseif(~r)
%
B(idx)=idxi; %prev = 0;
%rT(cB(i))=0;
V(idx)=vi; %if rB(i)==-1
%rL=-1.*ones(1,n);
if idxi>0 %[r1,c1,s1] = mb(0,1,0,rot);
B(idxi)=idx; %if(nn<n)
V(idxi)=vi; %elseif ~c
%rr = 0;
end %end
end %rR(-rR(i))=-i;
%if (n == 53)
function idx=rc2idx(r,c) %elseif  rB(i)<0
rc=r+c; %sc2 = c2 - 2;
idx=(~~rc)*((~r)*2*n+(rc<0)*n+abs(rc)); %
%if n==10
end %sn = -1;
%end
%end
function [r,c]=idx2rc(idx) %sol(tempR,tempC)=tempS;B=1;
if ~idx %end
%if(r==0)
r=0; %sol(n,i+1)=sB(i);sol(n,i)=0;sol(n,i+2)=0;
c=0; %function [ri,ci,vi]=beamkeeper(r,c,s)
return %if rR(i)==0 && cR(i)<0 
end %couu = 0;
d=floor((idx-1)/n); %end
%solution(abs(r),c+sign(r))=max(0,solution(abs(r),c+sign(r)));
sc=floor(d/2); %B(idxi)=idx;
rc=idx-d*n; %r = rem(r-1,n+1);
%if sol(tempR,tempC)==-1
s=1-(d-sc*2)*2; %end
r=~sc*s*rc; %    s=zeros(n);
c=sc*s*rc; %    s(9,2)=40;     s(9,9)=36;     s(9,12)=38;     s(9,13)=40; 
%rR=-1.*ones(1,n);
end %couo = -1;
%return
end %couo = 0;
%end
function varargout = beam2(r,c,n,beamIntensity) %if rB(i)>0 && cB(i)==0
global A turns charge newd ir ic %S=1*(length(find(rT~=rT2))+length(find(rL~=rL2))+length(find(rB~=rB2))+length(find(rR~=rR2))+length(find(cT~=cT2))+length(find(cL~=cL2))+length(find(cB~=cB2))+length(find(cR~=cR2)))+1*(length(find(sT-sT2))+length(find(sL-sL2))+length(find(sB-sB2))+length(find(sR-sR2)));
%if(~fnd)
%end
%B=0;
if nargin==1 %end
%sT=-1.*ones(1,n);
A = r; %if(desp>=1 && r2==2 && c3==2 && s2==s3)
newd  = [0 0 0 0;  %if (rn+cn2) == 0
%if sT(i)==0 && rT(i)==0 && cT(i)==-i;
1 2 3 4; %r = r + ir(d);
%end
4 3 2 1;  %end
%sol2 = ira(sol,mod(rot+2,4)+4*(rot>=4));
2 1 4 3; %if rB(i)==0 && cB(i)>0
%end
3 4 1 2];  %if rL(i)==-1 && ((i~=1 && i~=n) || (i==1 && do1L) || (i==n && doNL))
ir = [1 0 -1 0]; %if i==n-1 && sT(i)==0
ic = [0 1 0 -1]; %solution = ira(solution,rot);
%sol(tempR,tempC)=tempS;B=1;
n = length(A); %    s(14,9)=40;     s(14,15)=40;     s(15,2)=38;     s(15,14)=20; 
[turns charge] = setCmds(A); %solution(2,c+1) = s;
varargout = {0,0,0}; %if rL(i)==0 && cL(i)<0
return %end
end %elseif(ri==0)
%    s(19,29)=12;    s(29,29)=12;    s(30,42)=12;    s(31,23)=11;
%4 3 2 1; 
d = (c>0)+2*(r>0)+3*(c<0)+4*(r<0);  %[rr,cc,ss] = beamkeeper(r,c,2);
%if(nn<n)
r = abs(r); %sT(cR(i))=sR(i);
%for tc=1:n-1;
c = abs(c); %end
r = r+1 + ((n+1)*(d==3)); %else
c = c+1 + ((n+1)*(d==4)); %rB(-cT(i))=0;
ch = charge(r,c); %sol(:,i)=0;if i>1; sol(:,i-1)=0;end;if i<n; sol(:,i+1)=0;end
if ~ch %[r5,c5,s5] = mb(0,2,0,rot);
%sR(-rL(i))=sL(i);
while d %doNL=1;
%r=~sc*s*rc;
r = r + ir(d); %sol(tempR,tempC+1:end)=0;sol(1:tempR-1,tempC)=0;
c = c + ic(d); %    return
%mb(0,c+2,1,rot);
d = newd(turns(r,c),d); %end
%sol(i+1,1)=sL(i); sol(i,1)=0;sol(i+2,1)=0;
ch = ch + charge(r,c); %r2=0;c2=0;s2=0;
end %
%mb(0,-c,1,rot);
end %    s=zeros(n);
%if i > 0
d = (r==n+2)||(c==n+2); %if rB(i)==-1
r = rem(r-1,n+1); %else
%solution;
c = rem(c-1,n+1); %n = length(A);
if r&&c %end
%end
varargout = {0,0,0}; %if rL(i)==i  && sL(i+1)==0 && rL(i+1)==0 && cL(i+1)==0 && sL(i+2)==sL(i)
elseif d %rL(rL(i))=i;
varargout = {-r,-c,ch}; %end
else %if ro(c)
varargout = {r,c,ch}; %if  sol(tempR,tempC)>99 
end %if (rn+cn2) == 0
end %cn = cn - 1;
%else
function [turns charge] = setCmds(A) %sol(tempR,tempC)=tempS;B=1;
B = conv2(A,[0 0 0;0 1 0;0 0 0]); %sol(tempR,tempC)=0;
C = conv2(A,[0 1 0;1 0 1;0 1 0])&~B; %end
%a = rot90(ai,-r);
turns = conv2(single(A>0),[1 0 2;0 0 0;2 0 1])+2; %else
turns(~conv2(ones(size(A)),[0 0 0;0 1 0;0 0 0])|B) = 1; %solution(nn, c2-1) = s2 - prev;
%
turns(C) = 2; %elseif(~r)
charge = conv2(A,ones(3)); %cT(cR(i))=0;
%cT(cT(i))=i;
charge(C|B) = 0; %[rr,cc,ss] = beamkeeper(r,c,2);
charge = charge+B; %end
end %    return