Cody

Problem 1982. Battleship - Seaman Level

Solution 357553

Submitted on 22 Nov 2013 by Alfonso Nieto-Castanon
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 1-Seaman r=[0 2 2 3 1 1 1 1 2 2 5 0]'; c=[0 1 0 1 1 2 6 0 5 0 4 0]; m(2,2)=1; m(2,6)=1; m(4,9)=3; 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.343679 seconds. Elapsed time is 0.356740 seconds.

2   Pass
%% m=zeros(12); m(2:end-1,2:end-1)=7; % December 2013 1-Seaman r=[0 1 1 1 1 2 3 3 3 1 4 0]'; c=[0 3 2 0 1 6 0 3 1 4 0 0]; m(2,3)=1; m(8,5)=1; m(7,8)=6; 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.087313 seconds. Elapsed time is 0.087861 seconds.

3   Pass
%% m=zeros(12); m(2:end-1,2:end-1)=7; % September 2013 1-Seaman r=[0 1 1 2 4 1 0 2 2 5 2 0]'; c=[0 1 1 1 1 4 0 7 0 2 3 0]; m(2,8)=0; m(8,3)=1; m(9,6)=0; m(5,11)=6; 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 2.165799 seconds. Elapsed time is 2.166476 seconds.

Suggested Problems

More from this Author241

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!