+---+---+---+---+---+
A | 2 | 0 | 2 | 2 | 2 |
+---+---+---+---+---+
B | 1 | 2 | 0 | 2 | 0 |
+---+---+---+---+---+
C | 1 | 2 | 1 | 0 | 1 |
+---+---+---+---+---+
A B C D | 1 | 1 | 2 | 1 | 2 |
+---+---+---+---++===+===+===+===+===+
| 2 | 1 | 1 | 1 || C | D | A | B | A |
+---+---+---+---++---+---+---+---+---+
| 2 | 1 | 0 | 2 || A | B | D | A | D |
+---+---+---+---++---+---+---+---+---+
| 2 | 2 | 1 | 0 || B | C | A | B | A |
+---+---+---+---++---+---+---+---+---+
| 0 | 1 | 2 | 2 || D | B | C | D | C |
+---+---+---+---++---+---+---+---+---+
| 2 | 0 | 1 | 2 || A | C | D | A | D |
+---+---+---+---++---+---+---+---+---+
This was the hardest of this type of puzzle and I resorted to a program:
clc
grid=['abcde';'fghij';'klmno';'pqrst';'uvwxy'];
u=unique(sortrows(perms('aabcd')));
for i=1:length(u)
grid(1,:)=u(i,:);
if count(u(i,:),'aa')==0
v=unique(sortrows(perms('aabdd')));
for j=1:length(v)
grid(2,:)=v(j,:);
if count(v(j,:),'aa')==0 && count(v(j,:),'dd')==0
w=unique(sortrows(perms('aabbc')));
for k=1:length(w)
grid(3,:)=w(k,:);
if count(w(k,:),'aa')==0 && count(w(k,:),'bb')==0
x=unique(sortrows(perms('bccdd')));
for l=1:length(x)
grid(4,:)=x(l,:);
if count(x(l,:),'cc')==0 && count(x(l,:),'dd')==0
y=unique(sortrows(perms('aacdd')));
for m=1:length(y)
grid(5,:)=y(m,:);
if count(y(m,:),'aa')==0 && count(y(m,:),'dd')==0
g=grid';
good=true;
for ii=1:5
if count(g(ii,:),'aa')>0 || count(g(ii,:),'bb')>0 || ...
count(g(ii,:),'cc')>0 ||count(g(ii,:),'dd')>0
good=false;
break
end
end
if good
if sort(g(1,:))=='aabcd'
if sort(g(2,:))=='bbccd'
if sort(g(3,:))=='aacdd'
if sort(g(4,:))=='aabbd'
if sort(g(5,:))=='aacdd'
disp(grid)
disp(' ')
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
function a = unique(A)
prev=zeros(1,size(A,2)); new=[];
for i=1:length(A)
if ~isequal(A(i,:),prev)
new=[new ; A(i,:)];
prev=A(i,:);
end
end
a=new;
end
I learned a lot of MATLAB functions writing the program during six hours of developing it; it took 31 seconds to run.
|