Cody

# Problem 2004. BattleShip - Petty Officer (Level 2)

Solution 1841978

Submitted on 7 Jun 2019 by William
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
m=zeros(12); m(2:end-1,2:end-1)=7; % Games August 2013 2-Petty Officer % r=[0 0 1 4 1 3 3 3 3 2 0 0]'; c=[0 2 3 2 0 5 0 4 0 2 2 0]; m(5,4)=3; m(6,11)=3; m(9,8)=0; tic b=solve_battleship(m,r,c); toc b(b>1)=0; b(b<0)=0; br=sum(b,2); bc=sum(b); assert(isequal(r,br)) assert(isequal(c,bc)) % find battleship,cruisers,destroyers,subs % conv2 to locate pieces % bsh,bsv % ch,cv,dh,dv,s mconvsub=conv2(b,[2 2 2;2 1 2;2 2 2],'same'); subs_ptr=find(mconvsub==1); % Isolated valid subs assert(size(subs_ptr,1)==4) % Qty of subs_ptr must be 4 mconvBH=conv2(b,[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); mconvBV=conv2(b',[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); BS_ptr=[find(mconvBH==4);find(mconvBV==4)]; assert(size(BS_ptr,1)==1) % Qty of BS_ptr must be 1 mconvCH=conv2(b,[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); mconvCV=conv2(b',[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); CS_ptr=[find(mconvCH==3);find(mconvCV==3)]; assert(size(CS_ptr,1)==2) % Qty of CS_ptr must be 2 mconvDH=conv2(b,[5 5 5 5;5 1 1 5;5 5 5 5],'same'); mconvDV=conv2(b',[5 5 5 5;5 1 1 5;5 5 5 5],'same'); DS_ptr=[find(mconvDH==2);find(mconvDV==2)]; assert(size(DS_ptr,1)==3) % Qty of DS_ptr must be 3 toc

Elapsed time is 4.120891 seconds. Elapsed time is 4.127991 seconds.

2   Pass
m=zeros(12); m(2:end-1,2:end-1)=7; % December 2013 2-Petty % r=[0 2 2 2 3 2 0 0 7 0 2 0]'; c=[0 2 5 1 4 1 4 0 2 1 0 0]; m(3,3)=3; m(5,7)=1; m(9,4)=0; tic b=solve_battleship(m,r,c); toc b(b>1)=0; b(b<0)=0; br=sum(b,2); bc=sum(b); assert(isequal(r,br)) assert(isequal(c,bc)) % find battleship,cruisers,destroyers,subs % conv2 to locate pieces % bsh,bsv % ch,cv,dh,dv,s mconvsub=conv2(b,[2 2 2;2 1 2;2 2 2],'same'); subs_ptr=find(mconvsub==1); % Isolated valid subs assert(size(subs_ptr,1)==4) % Qty of subs_ptr must be 4 mconvBH=conv2(b,[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); mconvBV=conv2(b',[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); BS_ptr=[find(mconvBH==4);find(mconvBV==4)]; assert(size(BS_ptr,1)==1) % Qty of BS_ptr must be 1 mconvCH=conv2(b,[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); mconvCV=conv2(b',[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); CS_ptr=[find(mconvCH==3);find(mconvCV==3)]; assert(size(CS_ptr,1)==2) % Qty of CS_ptr must be 2 mconvDH=conv2(b,[5 5 5 5;5 1 1 5;5 5 5 5],'same'); mconvDV=conv2(b',[5 5 5 5;5 1 1 5;5 5 5 5],'same'); DS_ptr=[find(mconvDH==2);find(mconvDV==2)]; assert(size(DS_ptr,1)==3) % Qty of DS_ptr must be 3 toc

Elapsed time is 0.677779 seconds. Elapsed time is 0.680222 seconds.

3   Pass
m=zeros(12); m(2:end-1,2:end-1)=7; % September 2013 2-Petty r=[0 5 1 4 1 0 5 1 2 1 0 0]'; c=[0 2 3 3 2 0 5 0 3 1 1 0]; m(9,2)=1; m(2,7)=0; m(3,9)=1; tic b=solve_battleship(m,r,c); toc b(b>1)=0; b(b<0)=0; br=sum(b,2); bc=sum(b); assert(isequal(r,br)) assert(isequal(c,bc)) % find battleship,cruisers,destroyers,subs % conv2 to locate pieces % bsh,bsv % ch,cv,dh,dv,s mconvsub=conv2(b,[2 2 2;2 1 2;2 2 2],'same'); subs_ptr=find(mconvsub==1); % Isolated valid subs assert(size(subs_ptr,1)==4) % Qty of subs_ptr must be 4 mconvBH=conv2(b,[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); mconvBV=conv2(b',[5 5 5 5 5 5;5 1 1 1 1 5;5 5 5 5 5 5],'same'); BS_ptr=[find(mconvBH==4);find(mconvBV==4)]; assert(size(BS_ptr,1)==1) % Qty of BS_ptr must be 1 mconvCH=conv2(b,[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); mconvCV=conv2(b',[5 5 5 5 5;5 1 1 1 5;5 5 5 5 5],'same'); CS_ptr=[find(mconvCH==3);find(mconvCV==3)]; assert(size(CS_ptr,1)==2) % Qty of CS_ptr must be 2 mconvDH=conv2(b,[5 5 5 5;5 1 1 5;5 5 5 5],'same'); mconvDV=conv2(b',[5 5 5 5;5 1 1 5;5 5 5 5],'same'); DS_ptr=[find(mconvDH==2);find(mconvDV==2)]; assert(size(DS_ptr,1)==3) % Qty of DS_ptr must be 3 toc

Elapsed time is 0.008755 seconds. Elapsed time is 0.011241 seconds.