2006-12-06 09:00:00 UTC

tcg5

Status: Failed
Results:

Based on: End game? (diff)

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