Geometry (cubes)

Hi there. I have a trouble with an algorithm. I need to divide 3D rectangle(I don't know how it is called in English) to cubes. How many cubes there will be trying to cut the biggest cube. There are a, b, c (dimensions) entered by the keyboard.For example, I enter 2 3 5program answers:"2x2x2 2 cubes1x1x1 14 cubesAt all 16 cubes."There may not be functions or procedures, structural data types(arrays, records or sets).Any help will be appreciated.

Sry for my bad English skills and being inaccurate. The program you have written is a bit different. I need to cut hexahedron into cubes trying to cut the biggest. I mean when program cuts two 2x2x2 cubes hexahedron changes (gets smaller). There are 14 cubes 1x1x1 after that.For example, I have a=3; b=4; c=5.First of all, program cuts 3x3x3 cube. Then the shape's which was hexahedron volume becomes 3x4x5-3x3x3 = 33. I mean it's changing. Then program cuts two 2x2x2 cubes. Volume becomes 33-2*8 = 17. In the end, program cuts 17 1x1x1 cubes.With this a, b, c program should reply:"3x3x3 1 cubes2x2x2 2 cubes1x1x1 17 cubesAt all 20 cubes"As you see: 1*27 + 2*8 + 17*1 = a*b*c.I hope it is clear now.

This complicates the situation, is no longer possible to solve with a few simple operations... see after you cut out the largest possible cube the remaining shape is not convex, so has to be divided into (the composing ) different convex shapes (hexahedrons )in order to be able to perform the next cut.(Unless is a simpler way to get around this) To do this without structures and subroutines (possible recursion) is quite hard... I could possibly put together a pseudo code for you, but I don't promote this kind of coding...

There you go, I was able to convert your 2D example into 3D, but this implementation has a structure and two subroutines and uses recursion. :-) It is possible to go without all these of course, but like i said I don't like that kind of coding.[code][color=Blue]program cubes_v2;

const max_side=40;

var i,x,y,z:byte; { counter + sides of the hexahedron } v:array[1..max_side] of word; { Structure to hold number of cut out cubes for each size } n:word; {no. of cubes cut}

I am wondering if there is a simple version of this algorithm. Program should not be much harder than the program of example.However, Atex your program gets into trouble with x = 3; y = 5; z = 2.Phat Nat I was not able to compile your program with FP IDE.Anyway, thank you guys for helping me

for i:=max_side downto 1 do if v[i]>0 then begin writeln(#32,i,'x',i,'x',i,' cubes: ',v[i]:5); { Display cut out cubes } inc(n,v[i]); end; writeln(#13#10,'Total cubes : ',n:5); writeln(#13#10,'Press Esc to exit, any key to continue',#13#10); ch:=readkey; until ch=#27;end.[/color][/code]Also, for FP, try to include [b]{$mode tp}[/b] and [b]{$ asmmode intel}[/b] as the first two lines, to force FP to compile in TP compatible mode and to use intel's dialect for ASM.

What's the correct result for 9x11x12? I get 93 cubes.I don't bother counting how many of each. It lists them. It could be done, but I was going for the whole no procedure/sets/arrays thing.Either way, it's terrible coding. Done way faster and shorter code using a recursive function

Try to test this:[code][color=Blue]procedure cutout_cube_(a,b,c:byte); begin if a>b then swap(a,b); if b>c then swap(b,c); if a>b then swap(a,b); case a of 0:exit; 1:v[a]:=v[a]+b*c; else begin v[a]:=v[a]+((b div a)*(c div a)); if (c mod a=0) then cutout_cube_(a,c,b mod a) else if (b mod a=0) then cutout_cube_(a,b,c mod a) else begin if ((c mod a)>=(b mod a)) then begin if ((a * ( b div a )) mod (c mod a))=0 then swap(b,c); cutout_cube_(a,a * (c div a),b mod a); cutout_cube_(a,b,c mod a); end else begin if ((a * ( c div a )) mod (b mod a))=0 then swap(b,c); cutout_cube_(a,a * (b div a),c mod a); cutout_cube_(a,c,b mod a); end; end; end; end; end;[/color][/code]It is correct with: [b](2,3,5)[/b], [b](3,4,5)[/b], [b](3,5,5)[/b], [b](9,11,12)[/b], [b](x,x,x)[/b], but could be some combinations when the splitting won't be optimized ( like 9,11,12 with the previous algorithm ).In comparison with the 2D situation the 3D problem is much more complicated. The algorithm first will cut out a cube equal to the smallest size of the hexahedron, the remaining 3D "[b]L[/b]" shape must be split in 2 hexahedrons so more (smaller) cubes could be cut out. The split can happen is two ways, where the algorithm must choose the case with the more bigger cube yield. For example, with the 9,11,12 solid, in the first step a 9x9x9 cube is removed leaving the following shape:[code] 12 OOOOOOOOOOOO OOOOOOOOOOOO OOO OOO OOO 11 OOO ( 9 deep ) OOO OOO O = 1x1x1 cube OOO OOO OOO[/code]This can be split in 2 ways:[code]1: 2:

OOOOOOOOOOOO 12x2x9 OOO OOOOOOOOO 9x2x9 OOOOOOOOOOOO OOO OOOOOOOOO OOO OOO 3x9x9 OOO 3x11x9 OOO OOO OOO OOO OOO OOO OOO ( 9 deep ) OOO ( 9 deep ) OOO OOO OOO OOO OOO OOO OOO[/code]One could expect that [b]2[/b] choice would be better since could yield more bigger cubes, it is usually true for most situations. But in this case the 3x3x3 yield is the same for both ways, so choosing [b]1[/b] results in more 2x2x2 cubes than [b]2[/b], therefore more efficient. See, by adding an extra dimension the problem deepens logarithmically , imagine what nightmare would it be working with a hypercube...