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

When will it end?

by Alan Chalker

Status: Passed
Results: 11812.6
CPU Time: 12.4178
Score: 118.131
Submitted at: 2006-12-06 16:56:36 UTC
Scored at: 2006-12-06 17:02:12 UTC

Current Rank: 4th
Basis for: BadCat 200 (diff)

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