Cody

Problem 46579. Shape Shifter - Part 1

Solution 2943532

Submitted on 13 Sep 2020 by William
  • Size: 185
  • 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
functions={'!', 'rehash', 'regexp', 'feval','eval','str2func','regex','system','dos','unix','perl','assert','fopen','write','save','setenv','path','please',}; assessFunctionAbsence(functions, 'FileName', 'shapeShifter.m');

2   Pass
fh=fopen('main.m','wt'); fprintf(fh, '%s \n', 'function out = main(N)'); fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'out = 0;'); fprintf(fh, '%s \n', 'm = 4; n = 3;'); fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'for I = 1:N '); % number of tests fprintf(fh, '%s \n', ' switch I '); fprintf(fh, '%s \n', ' case 1'); % LEVEL 1 fprintf(fh, '%s \n', ' board = [0 1 0; 1 1 1; 1 1 1; 1 1 1]; '); fprintf(fh, '%s \n', ' shapes = [{[1]}, {[1 1 1]}]; '); fprintf(fh, '%s \n', ' case 2'); % LEVEL 2 fprintf(fh, '%s \n', ' board = [1 0 1; 0 1 0; 1 1 1; 1 1 1]; '); fprintf(fh, '%s \n', ' shapes = [{[1 0; 1 1; 0 1]}, {[1 1 0; 0 1 1]}, {[1]}]; '); fprintf(fh, '%s \n', ' case 3'); % LEVEL 3 fprintf(fh, '%s \n', ' board = [1 1 1; 0 1 0; 1 1 0; 1 0 0]; '); fprintf(fh, '%s \n', ' shapes = [{[1 0; 1 1; 0 1]}, {[1]}, {[1 1 1]}, {[1; 1; 1]}]; '); fprintf(fh, '%s \n', ' case 4'); % LEVEL 4 fprintf(fh, '%s \n', ' board = [1 1 1; 1 1 0; 0 0 0; 1 0 1]; '); fprintf(fh, '%s \n', ' shapes = [{[1; 1]}, {[1 1 1; 0 1 0]}, {[1 0; 1 1; 0 1]}, {[1; 1]}, {[1 1; 1 0]}]; '); fprintf(fh, '%s \n', ' case 5'); % LEVEL 5 fprintf(fh, '%s \n', ' board = [1 1 1; 1 1 1; 0 1 1; 1 0 1]; '); fprintf(fh, '%s \n', ' shapes = [{[1; 1]}, {[1 1 1]}, {[1; 1]}, {[0 1; 1 1]}, {[0 1; 1 1; 0 1]}, {[1 1 1; 0 1 0]}]; '); fprintf(fh, '%s \n', ' end '); % end switch I fprintf(fh, '%s \n', ' '); fprintf(fh, '%s \n', ' shapeLocs = shapeShifter(board, shapes); '); % get solution fprintf(fh, '%s \n', ' nShifts = length(shapes);'); fprintf(fh, '%s \n', ' board'); fprintf(fh, '%s \n', ' for J = 1:nShifts'); fprintf(fh, '%s \n', ' currInd = shapeLocs(J);'); fprintf(fh, '%s \n', ' currShape = shapes{J};'); fprintf(fh, '%s \n', ' tempShift = getTempShift(currShape, currInd);'); fprintf(fh, '%s \n', ' board = shiftBoard(board, tempShift);'); fprintf(fh, '%s \n', ' end'); % for J = 1:nShifts % if is all ones -> out = out+1 fprintf(fh, '%s \n', ' if sum(sum(board)) == m*n'); fprintf(fh, '%s \n', ' out = out+1;'); % for J = 1:nShifts fprintf(fh, '%s \n', ' end'); % if sum(sum(board)) == m*n fprintf(fh, '%s \n', ' '); fprintf(fh, '%s \n', 'end '); % for I = 1:2 fprintf(fh, '%s \n', 'end '); % end of function: main fprintf(fh, '%s \n', ' '); fprintf(fh, '%s \n', 'function YorN = isWinner(board)'); fprintf(fh, '%s \n', 'end '); % end of function: isWinner fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'function tempShift = getTempShift(currShape, linInd)'); fprintf(fh, '%s \n', '[m, n] = size(currShape);'); fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'tempShift = zeros(4,3);'); fprintf(fh, '%s \n', ' switch linInd'); fprintf(fh, '%s \n', ' case 1'); % row 1 - col 1 fprintf(fh, '%s \n', ' tempShift(1:m, 1:n) = currShape;'); fprintf(fh, '%s \n', ' case 2'); % row 1 - col 2 fprintf(fh, '%s \n', ' tempShift(1:m, 2:n+1) = currShape;'); fprintf(fh, '%s \n', ' case 3'); % row 1 - col 3 fprintf(fh, '%s \n', ' tempShift(1:m, 3) = currShape;'); fprintf(fh, '%s \n', ' case 4'); % row 2 - col 1 fprintf(fh, '%s \n', ' tempShift(2:m+1, 1:n) = currShape;'); fprintf(fh, '%s \n', ' case 5'); % row 2 - col 2 fprintf(fh, '%s \n', ' tempShift(2:m+1, 2:n+1) = currShape;'); fprintf(fh, '%s \n', ' case 6'); % row 2 - col 3 fprintf(fh, '%s \n', ' tempShift(2:m+1, 3) = currShape;'); fprintf(fh, '%s \n', ' case 7'); % row 3 - col 1 fprintf(fh, '%s \n', ' tempShift(3:m+2, 1:n) = currShape;'); fprintf(fh, '%s \n', ' case 8'); % row 3 - col 2 fprintf(fh, '%s \n', ' tempShift(3:m+2, 2:n+1) = currShape;'); fprintf(fh, '%s \n', ' case 9'); % row 3 - col 3 fprintf(fh, '%s \n', ' tempShift(3:m+2, 3) = currShape;'); fprintf(fh, '%s \n', ' case 10'); % row 4 - col 1 fprintf(fh, '%s \n', ' tempShift(14, 1:n) = currShape;'); fprintf(fh, '%s \n', ' case 11'); % row 4 - col 2 fprintf(fh, '%s \n', ' tempShift(4, 2:n+1) = currShape;'); fprintf(fh, '%s \n', ' case 12'); % row 4 - col 3 fprintf(fh, '%s \n', ' tempShift(4, 3) = currShape;'); fprintf(fh, '%s \n', ' end'); % switch linInd fprintf(fh, '%s \n', 'end '); % end of function: getTempShift fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'function newBoard = shiftBoard(board, appliedShift)'); fprintf(fh, '%s \n', 'm = 4; n = 3;'); fprintf(fh, '%s \n', 'newBoard = board;'); fprintf(fh, '%s \n', ''); fprintf(fh, '%s \n', 'for i = 1:m'); fprintf(fh, '%s \n', ' for j = 1:n'); fprintf(fh, '%s \n', ' currBoard = board(i, j);'); fprintf(fh, '%s \n', ' currShift = appliedShift(i, j);'); fprintf(fh, '%s \n', ' if currShift == 1'); fprintf(fh, '%s \n', ' switch currBoard'); fprintf(fh, '%s \n', ' case 1'); fprintf(fh, '%s \n', ' newBoard(i, j) = 0;'); fprintf(fh, '%s \n', ' case 0'); fprintf(fh, '%s \n', ' newBoard(i, j) = 1;'); fprintf(fh, '%s \n', ' end'); % switch currBoard fprintf(fh, '%s \n', ' end'); % if currShift == 1 fprintf(fh, '%s \n', ' end'); % for j = 1:n fprintf(fh, '%s \n', 'end'); % for i = 1:m fprintf(fh, '%s \n', 'end '); % end of function: shiftBoard fclose(fh); rehash path

3   Pass
n = 5; score = main(n) assert(score == n)

board = 0 1 0 1 1 1 1 1 1 1 1 1 board = 1 0 1 0 1 0 1 1 1 1 1 1 board = 1 1 1 0 1 0 1 1 0 1 0 0 board = 1 1 1 1 1 0 0 0 0 1 0 1 board = 1 1 1 1 1 1 0 1 1 1 0 1 score = 5

Suggested Problems

Community Treasure Hunt

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

Start Hunting!