Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Correct

926Size
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
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 0.069657 seconds.
Elapsed time is 0.070554 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.622861 seconds.
Elapsed time is 0.623495 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.004456 seconds.
Elapsed time is 0.004958 seconds.