Consider the sequence of 9 non zero distinct digits arranged in a 3 by 3 grid , like
547
398
162
Clearly there are 9! (=362880) ways to do it.
If we demand that the consecutive numbers are next-door neighbours
( horizontally or vertically) like
167
258
349
then the number is significantly lower, say N.
Find N & provide your reasoning.
(In reply to
solution by Charlie)
clearvars, clc
global grid ct delta hist
ct=0; delta=[[-1,0];[0,-1];[1,0];[0,1]];
hist={};
for row=1:3
for col=1:3
grid=zeros(3);
addon(1,row,col);
end
end
ct
hist{:}
function addon(wh,row,col)
global grid ct delta hist
grid(row,col)=wh;
if wh==9
ct=ct+1;
hist{end+1}=grid;
else
for d=1:length(delta)
r=row+delta(d,1); c=col+delta(d,2);
if r>0 && r<4
if c>0 && c<4
if grid(r,c)==0
saveGrid=grid;
addon(wh+1,r,c)
grid=saveGrid;
end
end
end
end
end
end
ct =
40
And the grids in a compact form are:
1 6 7 1 8 7 1 8 9 1 4 5 1 2 9
2 5 8 2 9 6 2 7 6 2 3 6 4 3 8
3 4 9 3 4 5 3 4 5 9 8 7 5 6 7
1 2 3 1 2 3 1 2 3 3 2 1 3 2 1
6 5 4 8 7 4 8 9 4 4 7 8 4 9 8
7 8 9 9 6 5 7 6 5 5 6 9 5 6 7
3 2 1 9 2 1 5 4 1 7 6 1 7 8 1
4 5 6 8 3 4 6 3 2 8 5 2 6 9 2
9 8 7 7 6 5 7 8 9 9 4 3 5 4 3
9 8 1 3 2 9 9 2 3 3 4 5 9 8 7
6 7 2 4 1 8 8 1 4 2 1 6 2 1 6
5 4 3 5 6 7 7 6 5 9 8 7 3 4 5
5 6 7 7 6 5 5 4 3 7 8 9 3 4 9
4 1 8 8 1 4 6 1 2 6 1 2 2 5 8
3 2 9 9 2 3 7 8 9 5 4 3 1 6 7
3 4 5 3 4 5 9 8 7 5 6 7 7 6 5
2 7 6 2 9 6 2 3 6 4 3 8 8 9 4
1 8 9 1 8 7 1 4 5 1 2 9 1 2 3
9 6 5 7 8 9 5 4 3 5 4 3 9 4 3
8 7 4 6 5 4 6 9 2 6 7 2 8 5 2
1 2 3 1 2 3 7 8 1 9 8 1 7 6 1
7 8 9 7 6 5 5 6 9 5 6 7 9 8 7
6 3 2 8 3 4 4 7 8 4 9 8 4 5 6
5 4 1 9 2 1 3 2 1 3 2 1 3 2 1
|
Posted by Charlie
on 2022-10-27 11:23:23 |