Cody

Problem 44376. The sliding puzzle: 3D

Solution 1391301

Submitted on 23 Dec 2017 by Jens Kjærgaard Boldsen
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

This solution is outdated. To rescore this solution, sign in.

Test Suite

Test Status Code Input and Output
1   Pass
p = [0;2;3;1;5;6;4;7;9;10;11;12;13;17;14;16;8;18;19;20;21;22;23;15;25;26;24]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 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;16;17;18;19;20;21;22;23;24;25;26;0]))

p(:,:,1) = 1 0 4 2 5 7 3 6 9 p(:,:,2) = 10 13 16 11 17 8 12 14 18 p(:,:,3) = 19 22 25 20 23 26 21 15 24 ix = 1 p(:,:,1) = 1 4 0 2 5 7 3 6 9 p(:,:,2) = 10 13 16 11 17 8 12 14 18 p(:,:,3) = 19 22 25 20 23 26 21 15 24 ix = 4 p(:,:,1) = 1 4 7 2 5 0 3 6 9 p(:,:,2) = 10 13 16 11 17 8 12 14 18 p(:,:,3) = 19 22 25 20 23 26 21 15 24 ix = 7 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 17 0 12 14 18 p(:,:,3) = 19 22 25 20 23 26 21 15 24 ix = 8 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 0 18 p(:,:,3) = 19 22 25 20 23 26 21 15 24 ix = 14 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 0 24 ix = 15 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 24 0 ix = 24

2   Pass
p = [1;0;2;4;8;5;7;9;18;10;21;12;11;14;15;16;17;6;13;3;19;20;22;24;25;23;26]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 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;16;17;18;19;20;21;22;23;24;25;26;0]))

p(:,:,1) = 1 4 7 2 8 9 0 5 18 p(:,:,2) = 10 11 16 21 14 17 12 15 6 p(:,:,3) = 13 20 25 3 22 23 19 24 26 ix = 2 p(:,:,1) = 1 4 7 2 8 9 3 12 18 p(:,:,2) = 10 11 16 19 14 17 0 5 6 p(:,:,3) = 13 20 25 21 22 23 24 15 26 ix = 3 p(:,:,1) = 1 4 7 2 5 9 3 8 18 p(:,:,2) = 10 11 16 14 0 17 19 12 6 p(:,:,3) = 13 20 25 21 22 23 24 15 26 ix = 5 p(:,:,1) = 1 4 7 2 5 9 3 6 8 p(:,:,2) = 10 11 16 14 12 17 19 0 18 p(:,:,3) = 13 20 25 21 22 23 24 15 26 ix = 6 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 11 16 14 12 0 19 18 26 p(:,:,3) = 13 20 25 21 22 23 24 15 17 ix = 8 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 20 16 11 0 12 19 18 26 p(:,:,3) = 13 22 25 14 21 23 24 15 17 ix = 11 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 20 16 11 21 26 12 0 18 p(:,:,3) = 13 22 25 14 15 23 19 24 17 ix = 12 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 22 26 12 21 18 p(:,:,3) = 14 0 25 15 20 23 19 24 17 ix = 13 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 22 12 21 18 p(:,:,3) = 15 25 23 20 0 26 19 24 17 ix = 14 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 22 12 15 21 p(:,:,3) = 25 20 23 19 26 17 24 0 18 ix = 15 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 25 20 23 19 26 0 24 21 22 ix = 17 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 25 20 0 26 23 24 21 22 ix = 19 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 23 25 20 0 22 21 26 24 ix = 20 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 0 24 21 26 23 ix = 22 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 0 21 24 26 ix = 23 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 24 0 ix = 26

3   Pass
p = [1;4;3;19;10;6;7;2;9;20;11;0;16;14;12;5;13;18;22;23;21;17;15;24;25;8;26]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 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;16;17;18;19;20;21;22;23;24;25;26;0]))

p(:,:,1) = 1 19 7 2 0 9 4 3 6 p(:,:,2) = 20 16 5 14 10 13 11 12 18 p(:,:,3) = 22 17 25 23 15 8 21 24 26 ix = 2 p(:,:,1) = 1 19 7 2 10 9 3 0 6 p(:,:,2) = 20 16 5 14 12 13 4 11 18 p(:,:,3) = 22 17 25 23 15 8 21 24 26 ix = 3 p(:,:,1) = 1 4 7 2 0 9 3 10 6 p(:,:,2) = 20 19 5 12 16 13 14 11 18 p(:,:,3) = 22 17 25 23 15 8 21 24 26 ix = 4 p(:,:,1) = 1 4 7 2 5 9 3 10 6 p(:,:,2) = 20 16 19 12 0 13 14 11 18 p(:,:,3) = 22 17 25 23 15 8 21 24 26 ix = 5 p(:,:,1) = 1 4 7 2 5 9 3 6 0 p(:,:,2) = 20 16 19 12 11 13 14 10 18 p(:,:,3) = 22 17 25 23 15 8 21 24 26 ix = 6 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 20 16 19 12 11 0 14 10 18 p(:,:,3) = 22 17 25 23 15 26 21 24 13 ix = 8 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 20 19 0 16 11 12 14 18 p(:,:,3) = 22 17 25 23 15 26 21 24 13 ix = 10 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 20 19 11 0 18 16 12 14 p(:,:,3) = 22 17 25 23 15 26 21 24 13 ix = 11 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 20 19 11 15 18 12 0 14 p(:,:,3) = 22 17 25 23 24 26 16 21 13 ix = 12 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 19 11 0 18 12 15 14 p(:,:,3) = 22 20 25 23 17 24 16 21 26 ix = 13 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 19 11 14 0 12 18 15 p(:,:,3) = 22 20 25 23 17 24 16 21 26 ix = 14 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 19 11 14 24 12 15 0 p(:,:,3) = 22 20 25 23 17 26 16 18 21 ix = 15 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 19 12 15 21 p(:,:,3) = 22 25 0 17 20 24 23 18 26 ix = 16 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 25 24 26 22 20 0 23 21 19 ix = 17 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 0 24 25 22 26 23 20 21 ix = 19 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 24 20 0 26 21 25 23 ix = 20 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 0 26 21 23 24 ix = 23 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 0 24 ix = 23 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 24 0 ix = 24

4   Pass
p = [1;5;3;10;7;0;13;9;24;22;2;15;16;11;17;12;8;6;19;20;21;26;14;23;4;25;18]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 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;16;17;18;19;20;21;22;23;24;25;26;0]))

p(:,:,1) = 1 10 13 2 5 9 3 7 24 p(:,:,2) = 22 16 12 0 11 8 15 17 6 p(:,:,3) = 19 26 4 20 14 25 21 23 18 ix = 2 p(:,:,1) = 1 4 13 2 10 9 3 7 24 p(:,:,2) = 16 0 26 22 5 8 15 17 6 p(:,:,3) = 19 14 12 20 11 25 21 23 18 ix = 4 p(:,:,1) = 1 4 13 2 5 10 3 7 24 p(:,:,2) = 16 26 8 22 0 9 15 17 6 p(:,:,3) = 19 14 12 20 11 25 21 23 18 ix = 5 p(:,:,1) = 1 4 13 2 5 10 3 6 7 p(:,:,2) = 16 26 8 22 17 9 15 0 24 p(:,:,3) = 19 14 12 20 11 25 21 23 18 ix = 6 p(:,:,1) = 1 4 7 2 5 0 3 6 24 p(:,:,2) = 16 26 13 22 9 8 15 17 10 p(:,:,3) = 19 14 12 20 11 25 21 23 18 ix = 7 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 16 26 13 22 17 0 15 10 18 p(:,:,3) = 19 14 12 20 11 25 21 23 24 ix = 8 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 16 13 0 26 17 22 15 18 p(:,:,3) = 19 14 12 20 11 25 21 23 24 ix = 10 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 16 13 11 20 17 22 15 18 p(:,:,3) = 19 14 12 0 26 25 21 23 24 ix = 11 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 16 13 11 20 17 12 22 18 p(:,:,3) = 19 14 25 23 21 26 0 15 24 ix = 12 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 0 11 20 17 12 22 18 p(:,:,3) = 14 16 25 19 21 26 23 15 24 ix = 13 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 25 11 14 20 12 22 18 p(:,:,3) = 19 16 26 21 0 17 23 15 24 ix = 14 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 25 11 14 20 12 15 22 p(:,:,3) = 19 16 26 21 17 24 23 0 18 ix = 15 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 25 12 15 22 p(:,:,3) = 19 24 0 21 26 20 23 17 18 ix = 16 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 24 20 21 22 0 23 26 25 ix = 17 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 24 20 0 25 21 23 26 ix = 20 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 0 26 21 23 24 ix = 23 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 0 24 ix = 23 p(:,:,1) = 1 4 7 2 5 8 3 6 9 p(:,:,2) = 10 13 16 11 14 17 12 15 18 p(:,:,3) = 19 22 25 20 23 26 21 24 0 ix = 24

Suggested Problems

More from this Author34

Community Treasure Hunt

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

Start Hunting!