Doesn't this kind of trick get boring after a while?
actually, this is the first time i've used it without essentially copying Alfonso's code, so it's still new and fresh for me =P
Can someone explain to me how this function works (even if it is a trick)? I'm trying to parse through the code, but how it works isn't clear to me.
the sparse function places the values (third input) into the row and column specified, 1st and second inputs respectively. if there are any overlapping coordinates then it sums them, so I created a matrix that, when fed into sparse, would sum the rows, sum the columns, and sum the diagonals in each direction. Then I Cody-fied it (teehee) by changing these indices into a string of characters, which Cody sizes as small, and subtracting a base character from it. does that make sense now?
it might make more sense to look at one of my other solutions that uses the str2num function instead of the letter thing...
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%% Eight Queens Solution Checker Test Suite
|
2 | Pass |
%%
% Unique solution #6 from
% http://en.wikipedia.org/wiki/Eight_queens_puzzle
in1 = [ ...
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 ];
out1 = isEightQueensSolution(in1);
assert(islogical(out1));
assert(isequal(out1, 1));
|
3 | Pass |
%%
% Unique solution #7
in2 = [ ...
0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 ];
out2 = isEightQueensSolution(in2);
assert(isequal(out2, 1));
|
4 | Pass |
%%
% Unique solution #10
in3 = [ ...
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 ];
out3 = isEightQueensSolution(in3);
assert(isequal(out3, 1));
|
5 | Pass |
%%
% Unique solution #11
in4 = [ ...
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 ];
out4 = isEightQueensSolution(in4);
assert(isequal(out4, 1));
|
6 | Pass |
%%
in5 = [ ...
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 ];
out5 = isEightQueensSolution(in5);
assert(isequal(out5, 0));
|
7 | Pass |
%%
in6 = [ ...
0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 ];
out6 = isEightQueensSolution(in6);
assert(isequal(out6, 0));
|
8 | Pass |
%%
in7 = [ ...
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 ];
out7 = isEightQueensSolution(in7);
assert(isequal(out7, 0));
|
9 | Pass |
%%
in8 = [ ...
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 ];
out8 = isEightQueensSolution(in8);
assert(isequal(out8, 0));
|
10 | Pass |
%%
% Only 7 queens
in9 = [ ...
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 ];
out9 = isEightQueensSolution(in9);
assert(isequal(out9, 0));
|
11 | Pass |
%%
% Row and column constraint satisfied but
% not diagonal constraint.
in10 = eye(8);
out10 = isEightQueensSolution(in10);
assert(isequal(out10, 0));
|
829 Solvers
6283 Solvers
163 Solvers
1763 Solvers
486 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!