Cody

# Problem 42842. The sliding puzzle: 15

Solution 2115419

Submitted on 3 Feb 2020 by ME
• Size: 100
• 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
filetext = fileread('sliding.m'); assert(isempty(strfind(filetext,'6 10 14 15 16'))) assert(isempty(strfind(filetext,'6,10,14,15,16')))

freepass granted, CONGRATULATIONS!

2   Fail
p = [1 2 3 4;5 10 6 7;9 0 11 8;13 14 15 12]; m = sliding(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero = find(p == 0); [rzero czero] = ind2sub(size(p),zero); [rmove cmove] = ind2sub(size(p),m(i)); if abs(rzero + czero - rmove - cmove) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 0]))

freepass granted, CONGRATULATIONS!

exit

3   Fail
p = [6 3 0 11;7 14 8 5;15 1 2 4;13 9 10 12]; m = sliding(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero = find(p == 0); [rzero czero] = ind2sub(size(p),zero); [rmove cmove] = ind2sub(size(p),m(i)); if abs(rzero + czero - rmove - cmove) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 0]))

freepass granted, CONGRATULATIONS!

exit

4   Fail
p = [8 2 3 13;1 6 10 9;15 14 0 5;11 12 4 7]; m = sliding(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero = find(p == 0); [rzero czero] = ind2sub(size(p),zero); [rmove cmove] = ind2sub(size(p),m(i)); if abs(rzero + czero - rmove - cmove) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 0]))

freepass granted, CONGRATULATIONS!

exit

5   Fail
p = [11 7 15 9;3 1 0 8;5 12 13 4;14 2 10 6]; m = sliding(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero = find(p == 0); [rzero czero] = ind2sub(size(p),zero); [rmove cmove] = ind2sub(size(p),m(i)); if abs(rzero + czero - rmove - cmove) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 0]))

freepass granted, CONGRATULATIONS!

exit