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 198772

Submitted on 31 Jan 2013 by Alfonso Nieto-Castanon

Correct

358Size
Leading solution size is 294.
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
 
%%
feval(@assignin,'caller','score',2000);

                    
2
Pass
 
%%
map=[...
      repmat('a',1,28);
      'accccccccccccaacccccccccccca';
      'acaaaacaaaaacaacaaaaacaaaaca';
      'acaaaacaaaaacaacaaaaacaaaaca';
      'acaaaacaaaaacaacaaaaacaaaaca';
      'acccccccccccccccccccccccccca';
      'acaaaacaacaaaaaaaacaacaaaaca';
      'acaaaacaacaaaaaaaacaacaaaaca';
      'accccccaaccccaaccccaacccccca';
      'aaaaaacaaaaabaabaaaaacaaaaaa';
      'aaaaaacaaaaabaabaaaaacaaaaaa';
      'aaaaaacaabbbbbbbbbbaacaaaaaa';
      'aaaaaacaabaaabbaaabaacaaaaaa';
      'aaaaaacaabalbbbblabaacaaaaaa';
      'bbbbbbcbbbabbbbbbabbbcbbbbbb';
      'aaaaaacaabalbbbblabaacaaaaaa';
      'aaaaaacaabaaaaaaaabaacaaaaaa';
      'aaaaaacaabbbbbbbbbbaacaaaaaa';
      'aaaaaacaabaaaaaaaabaacaaaaaa';
      'aaaaaacaabaaaaaaaabaacaaaaaa';
      'accccccccccccaacccccccccccca';
      'acaaaacaaaaacaacaaaaacaaaaca';
      'acaaaacaaaaacaacaaaaacaaaaca';
      'acccaacccccccbdcccccccaaccca';
      'aaacaacaacaaaaaaaacaacaacaaa';
      'aaacaacaacaaaaaaaacaacaacaaa';
      'accccccaaccccaaccccaacccccca';
      'acaaaaaaaaaacaacaaaaaaaaaaca';
      'acaaaaaaaaaacaacaaaaaaaaaaca';
      'acccccccccccccccccccccccccca';
      repmat('a',1,28);];
  map=map-'b';
  [nr, nc]=size(map);
  mapdelta=[-1 nr 1 -nr]; % Valid as long as not on an edge
  tunnel=find(map(:,1)==0); % tunnelptr
  tunnel=[tunnel tunnel+nr*(nc-1)]; % Entrance/Exit Tunnel
  [pmr, pmc]=find(map==2); % pi 24 row  pj 15 column of map
   ptrpac=find(map==2);
 lives=1; % Lives
  movepac=0;
while lives && any(mod(map(:),10)==1) && movepac<5000 && ~isempty(find(map(:)==2))
   while ~isempty(find(map(:)==2)) && movepac<5000
     movepac=movepac+1;
 if isempty(find(map==1,1)),break;end %
 [curdir]=pacmat(map);
  if curdir==0,continue;end
 if map(ptrpac+mapdelta(curdir))==-1
     % Do nothing - Ran into a Wall
    elseif map(ptrpac+mapdelta(curdir))>2 % ran into ghost
      map(ptrpac)=0; % remove PAC from the board
      lives=0;
      break; % Lose
    else % legal move
      map(ptrpac)=0; % Eat Dot and clear PAC
      ptrpac=ptrpac+mapdelta(curdir);
      if ptrpac==tunnel(1),ptrpac=tunnel(2)-nr;end
      if ptrpac==tunnel(2),ptrpac=tunnel(1)+nr;end
      map(ptrpac)=2;
    end
  end % PAC Move while
  if isempty(find(map==1,1)),break;end %
   if lives==0,break;end
   lives=lives-1;
 end % while alive
fprintf('moves %i\n',movepac)
assert(lives>0)
assert(isempty(find(map==1)))
feval( @assignin,'caller','score',floor(min( 2000,movepac )) );
moves 358