It's quite impressive how fast the number of possible grids grow.
For a 3x3 grid we have the 322 possibilities as was identified by the other answers. Jumping up to 4x4 gives 70878 possible grids. Going for a two page spread of 3x6 the number increases to a barmy 314662 possible grids!
function [found] = makeGrids(found,grid,depth,x,y)
if (depth > (x*y))
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Otherwise%Show anothercurrent depth and found count during search.
if (depth<=(x*(y-1)))
disp([repmat('..> ',1,depth) num2str(depth) ' (found:' num2str(numel(found)) ')']);
end
%Another layer to do
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth,x,y)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1,x,y); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth,x,y)
success = false;
for ys=1:y
for xs=1:x
%Start at (1,1) and work through to (n,n)
expected = xs+(ys-1)*x; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=x:-1:xs
for ye=y:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Grid Size
x=3; %3x3
y=3;
%Enumerate all grids
grids=makeGrids({},zeros(x,y),1,x,y);
gridCount = numel(grids);
disp(['Grids Found: ' num2str(gridCount)]);
%Colour mapping for image - hot(x*y), hsv(x*y) and jet(x*y) all look good
map=[jet(x*y);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
nameFormat = ['%0' num2str(ceil(log10(gridCount))) 'd'];
for i=1:gridCount
img=grids{i};
img(x+1,:)=10;=(x*y)+1;
img(:,y+1)=10;=(x*y)+1;
[img, newmap]=imresize(img,map,32,'nearest');
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,'%03d'nameFormat) '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
vertical = max(factor(gridCount));
horizontal = gridCount/vertical;
montage(fileNames, 'Size', [vertical horizontal]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
function [found] = makeGrids(found,grid,depth,x,y)
if (depth > (x*y))
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Otherwise another layer to do
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth,x,y)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1,x,y); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth,x,y)
success = false;
for ys=1:y
for xs=1:x
%Start at (1,1) and work through to (n,n)
expected = xs+(ys-1)*x; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=x:-1:xs
for ye=y:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Grid Size
x=3; %3x3
y=3;
%Enumerate all grids
grids=makeGrids({},zeros(x,y),1,x,y);
gridCount = numel(grids);
disp(['Grids Found: ' num2str(gridCount)]);
%Colour mapping for image - hot(x*y), hsv(x*y) and jet(x*y) all look good
map=[jet(x*y);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
for i=1:gridCount
img=grids{i};
img(x+1,:)=10;
img(:,y+1)=10;
[img, newmap]=imresize(img,map,32,'nearest');
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,'%03d') '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
vertical = max(factor(gridCount));
horizontal = gridCount/vertical;
montage(fileNames, 'Size', [vertical horizontal]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
It's quite impressive how fast the number of possible grids grow.
For a 3x3 grid we have the 322 possibilities as was identified by the other answers. Jumping up to 4x4 gives 70878 possible grids. Going for a two page spread of 3x6 the number increases to a barmy 314662 possible grids!
function [found] = makeGrids(found,grid,depth,x,y)
if (depth > (x*y))
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Show current depth and found count during search.
if (depth<=(x*(y-1)))
disp([repmat('..> ',1,depth) num2str(depth) ' (found:' num2str(numel(found)) ')']);
end
%Another layer to do
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth,x,y)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1,x,y); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth,x,y)
success = false;
for ys=1:y
for xs=1:x
%Start at (1,1) and work through to (n,n)
expected = xs+(ys-1)*x; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=x:-1:xs
for ye=y:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Grid Size
x=3; %3x3
y=3;
%Enumerate all grids
grids=makeGrids({},zeros(x,y),1,x,y);
gridCount = numel(grids);
disp(['Grids Found: ' num2str(gridCount)]);
%Colour mapping for image - hot(x*y), hsv(x*y) and jet(x*y) all look good
map=[jet(x*y);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
nameFormat = ['%0' num2str(ceil(log10(gridCount))) 'd'];
for i=1:gridCount
img=grids{i};
img(x+1,:)=(x*y)+1;
img(:,y+1)=(x*y)+1;
[img, newmap]=imresize(img,map,32,'nearest');
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,nameFormat) '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
vertical = max(factor(gridCount));
horizontal = gridCount/vertical;
montage(fileNames, 'Size', [vertical horizontal]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
The following MATLAB functions are used to produce the grids.
function [found] = makeGrids(found,grid,depth,x,y)
if (depth ==> 10(x*y))
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Otherwise another layer to do
if(depth==9)
%BP
end
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth,x,y)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1,x,y); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth,x,y)
success = false;
for ys=1:3y
for xs=1:3x
%Start at (1,1) and work through to (3n,3n)
expected = xs+(ys-1)*3;*x; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=3xe=x:-1:xs
for ye=3ye=y:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Grid Size
x=3; %3x3
y=3;
%Enumerate all grids
grids=makeGrids({},zeros(3x,y),1,x,y);
gridCount = numel(grids);
disp(['Grids Found: ' num2str(gridCount)]);
%Colour mapping for image - hot(9x*y), hsv(9x*y) and jet(9x*y) all look good
map=[jet(9x*y);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
for i=1:numel(grids)gridCount
img=grids{i};
%Add border to bottom right
img(4x+1,:)=10;
img(:,4y+1)=10;
%Resize to 32x32 per square
[img, newmap]=imresize(img,map,32,'nearest');
%Save as PNG
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,'%03d') '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
vertical = max(factor(gridCount));
horizontal = gridCount/vertical;
montage(fileNames, 'Size', [23[vertical 14]horizontal]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
function [found] = makeGrids(found,grid,depth)
if (depth == 10)
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Otherwise another layer to do
if(depth==9)
%BP
end
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth)
success = false;
for ys=1:3
for xs=1:3
%Start at (1,1) and work through to (3,3)
expected = xs+(ys-1)*3; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=3:-1:xs
for ye=3:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Enumerate all grids
grids=makeGrids({},zeros(3),1);
%Colour mapping for image - hot(9), hsv(9) and jet(9) all look good
map=[jet(9);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
for i=1:numel(grids)
img=grids{i};
%Add border to bottom right
img(4,:)=10;
img(:,4)=10;
%Resize to 32x32 per square
[img, newmap]=imresize(img,map,32,'nearest');
%Save as PNG
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,'%03d') '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
montage(fileNames, 'Size', [23 14]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
The following MATLAB functions are used to produce the grids.
function [found] = makeGrids(found,grid,depth,x,y)
if (depth > (x*y))
%If at max depth then grid is valid.
found = [found grid]; %So add to list
disp(grid)
%found=found+1; %So one more found
else
%Otherwise another layer to do
for k=1:depth
%For each number in this layer
grid(depth)=k; %Update grid with new number. Depth is linear index
%Now we check to see if the current state of the grid is
%acceptable (if it isn't then no lower down ones possibly can)
if (checkPanels(grid,depth,x,y)) %If it is acceptable (i.e. there are no remaining values with no frame)
found=makeGrids(found,grid,depth+1,x,y); %Work through the next layer
end
end
end
end
function success = checkPanels(grid,depth,x,y)
success = false;
for ys=1:y
for xs=1:x
%Start at (1,1) and work through to (n,n)
expected = xs+(ys-1)*x; %Linear index of this cell
if(expected > depth)
%If the expected val is more than current depth
return; %Then we are done searching
end
panelFound=false;
for xe=x:-1:xs
for ye=y:-1:ys
%For each end value starting from largest to smallest
panel=grid(xs:xe,ys:ye); %Extract the current panel
panel(panel==expected)=0; %Cover all instances of expected value in panel
panelFound = all(all(~panel));%Check if every number in this panel is covered
if (panelFound) %If we have found a complete panel
grid(xs:xe,ys:ye) = -1; %then mark panel a covered in the grid.
break; %We can only have one panel for any given number, so break.
end
end
if (panelFound)
break; %We can only have one panel for any given number, so continue break.
end
end
%Check if entire grid is covered
if (all(all(grid==-1)))
success = true; %Grid is all covered and valid
return;
end
end
end
end
%Grid Size
x=3; %3x3
y=3;
%Enumerate all grids
grids=makeGrids({},zeros(x,y),1,x,y);
gridCount = numel(grids);
disp(['Grids Found: ' num2str(gridCount)]);
%Colour mapping for image - hot(x*y), hsv(x*y) and jet(x*y) all look good
map=[jet(x*y);0,0,0]; %;0,0,0 is for black borderd
%Create images for all grids
for i=1:gridCount
img=grids{i};
img(x+1,:)=10;
img(:,y+1)=10;
[img, newmap]=imresize(img,map,32,'nearest');
imwrite(ind2rgb(img,newmap),['Grid' num2str(i,'%03d') '.png']);
end
%Create tiled montage of images
dirOutput = dir('Grid*.png');
fileNames = {dirOutput.name}';
vertical = max(factor(gridCount));
horizontal = gridCount/vertical;
montage(fileNames, 'Size', [vertical horizontal]);
%And save montage as PNG.
allGrids=getframe(gca);
imwrite(allGrids.cdata,'AllGrids.png');
Loading