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

End of it all

by Alan Chalker

Status: Passed
Results: 11653.1
CPU Time: 12.5361
Score: 116.536
Submitted at: 2006-12-06 16:57:39 UTC
Scored at: 2006-12-06 17:06:12 UTC

Current Rank: 1st

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