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

Solution 331124

Submitted on 8 Oct 2013 by Richard Zapor

Correct

2223Size
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
 
%%
% test cases
randn('seed',0);
rand('seed',0);
N=randi([1000 4000],50,1);
S=randi([1,50],50,1);
Boards=arrayfun(@(s)convn(randn(100),ones(s)/s^2,'same'),S,'uni',0);
FLAGSLEFT=0;
DOPLOT=false;
tic;
for board=1:50
 B=Boards{board};
 sB=sort(B(:));
 B=double(B>sB(round(numel(sB)*.9)));
 n=N(board);
 path=capture_the_flag(B,n);
 assert(size(path,1)<=n,'too many steps');
 assert(all(sum(abs(diff([1,1;path])),2)<=1),'no jumping allowed');
 if DOPLOT
    imagesc(B);
    hold on;
    plot(path(:,2),path(:,1),'y-');
    hold off;
    axis equal;
    axis off;
    set(gcf,'color',0*[1 1 1]);
    colormap(.5*gray);
    drawnow;
 end
 B(1)=0;
 B((path-1)*[1;size(B,1)]+1)=0;
 fprintf('test %d; left %d flags\n',board,nnz(B));
 FLAGSLEFT=FLAGSLEFT+nnz(B);
end
toc;
assignin('caller','score',10000);
assert(FLAGSLEFT<=10000,sprintf('Left %d flags',FLAGSLEFT));
assignin('caller','score',FLAGSLEFT);
ans =
     0
test 1; left 62 flags
ans =
     0
test 2; left 254 flags
test 3; left 0 flags
test 4; left 0 flags
test 5; left 0 flags
test 6; left 0 flags
test 7; left 0 flags
test 8; left 0 flags
ans =
     0
test 9; left 281 flags
ans =
     0
test 10; left 55 flags
test 11; left 0 flags
test 12; left 0 flags
ans =
     0
test 13; left 443 flags
test 14; left 0 flags
ans =
     0
test 15; left 367 flags
test 16; left 0 flags
test 17; left 0 flags
test 18; left 0 flags
test 19; left 0 flags
test 20; left 0 flags
test 21; left 0 flags
ans =
     0
test 22; left 73 flags
test 23; left 0 flags
test 24; left 0 flags
test 25; left 0 flags
test 26; left 0 flags
test 27; left 0 flags
ans =
   130
test 28; left 392 flags
ans =
     0
test 29; left 197 flags
test 30; left 0 flags
test 31; left 0 flags
test 32; left 0 flags
test 33; left 0 flags
test 34; left 0 flags
test 35; left 0 flags
ans =
     4
test 36; left 24 flags
test 37; left 0 flags
test 38; left 0 flags
test 39; left 0 flags
test 40; left 0 flags
ans =
     0
test 41; left 63 flags
test 42; left 0 flags
test 43; left 0 flags
ans =
   147
test 44; left 1 flags
test 45; left 0 flags
test 46; left 0 flags
test 47; left 0 flags
ans =
     0
test 48; left 5 flags
ans =
     0
test 49; left 6 flags
ans =
   161
test 50; left 0 flags
Elapsed time is 42.862772 seconds.