Problem 1231. PACMAT Easy

Solution 1920416

Submitted on 3 Sep 2019 by Abdelfatah Teamah
  • Size: 45
  • 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

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 314

Suggested Problems

More from this Author246

Community Treasure Hunt

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

Start Hunting!