Cody

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

Solution 1669310

Submitted on 8 Nov 2018 by Oleksandr Denysenko
• Size: 33
• This is the leading solution.
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

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In unix (line 32) In solve_battleship (line 9) In ScoringEngineTestPoint1 (line 11) In solutionTest (line 3)] Elapsed time is 0.271644 seconds. Elapsed time is 0.289203 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

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In solve_battleship (line 7) In ScoringEngineTestPoint2 (line 12) In solutionTest (line 5)] Elapsed time is 0.053468 seconds. Elapsed time is 0.061946 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

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In solve_battleship (line 7) In ScoringEngineTestPoint3 (line 13) In solutionTest (line 7)] Elapsed time is 0.031214 seconds. Elapsed time is 0.035284 seconds.