1. Самое первое правильное решениеVOLVOИсходный код
const
size = 4;
type
all_type = array[1 .. size] of integer;
pair = array[1 .. 2] of word;
const
s1_len = 6;
step_one: array[1 .. s1_len] of pair =
((1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4));
s2_len = 2;
step_two: array[1 .. s2_len] of word =
(1, 2);
s3_len = 3;
step_three: array[1 .. s3_len] of pair =
((1, 2), (2, 3), (1, 3));
s4_len = 3;
step_four: array[1 .. s4_len] of word =
(1, 2, 3);
s5_len = 1;
step_five: array[1 .. s5_len] of pair =
((1, 2));
const
right: all_type = (1, 2, 5, 10);
left: all_type = (-1, -1, -1, -1);
procedure compact(var x: all_type);
var T: all_type;
i, next: byte;
begin
move(x, T, sizeof(x));
next := 0;
for i := 1 to size do
if T[i] <> -1 then
begin
inc(next);
x[next] := T[i]
end;
for i := succ(next) to size do
x[i] := -1;
end;
procedure append(var x: all_type; a: word);
var i: integer;
begin
i := 1;
while x[i] <> -1 do inc(i);
x[i] := a;
end;
function max(a, b: word): word;
begin
max := a;
if b > a then max := b
end;
function get_two(by: pair; var x: pair): word;
begin { from right }
x[1] := right[by[1]]; right[by[1]] := -1;
x[2] := right[by[2]]; right[by[2]] := -1;
compact(right);
get_two := max(x[1], x[2])
end;
function get_one(by: word; var x: word): word;
begin { from left }
x := left[by]; left[by] := -1;
compact(left);
get_one := x
end;
procedure print_array(x: all_type);
var i: byte;
begin
for i := 1 to size do
if x[i] <> -1 then write(x[i]:4);
writeln
end;
var
min1, min2, min3, min4, min5: byte;
mp: pair; path, T: word;
procedure create_path;
var
right0, right1, right2, right3, right4,
left0, left1, left2, left3, left4: all_type;
s1, s2, s3, s4: word;
p1, p2, p3, p4, p5: byte;
min_path: word;
begin
path := 0; min_path := maxInt;
move(left, left0, sizeof(left));
move(right, right0, sizeof(right));
for p1 := 1 to s1_len do
begin
move(left0, left, sizeof(left));
move(right0, right, sizeof(right));
path := get_two(step_one[p1], mp);
append(left, mp[1]); append(left, mp[2]);
s1 := path;
move(left, left1, sizeof(left));
move(right, right1, sizeof(right));
for p2 := 1 to s2_len do
begin
path := s1;
move(left1, left, sizeof(left));
move(right1, right, sizeof(right));
inc(path, get_one(step_two[p2], T));
append(right, T);
s2 := path;
move(left, left2, sizeof(left));
move(right, right2, sizeof(right));
for p3 := 1 to s3_len do
begin
path := s2;
move(left2, left, sizeof(left));
move(right2, right, sizeof(right));
inc(path, get_two(step_three[p3], mp));
append(left, mp[1]); append(left, mp[2]);
s3 := path;
move(left, left3, sizeof(left));
move(right, right3, sizeof(right));
for p4 := 1 to s4_len do
begin
path := s3;
move(left3, left, sizeof(left));
move(right3, right, sizeof(right));
inc(path, get_one(step_four[p4], T));
append(right, T);
s4 := path;
move(left, left4, sizeof(left));
move(right, right4, sizeof(right));
for p5 := 1 to s5_len do
begin
path := s4;
move(left4, left, sizeof(left));
move(right4, right, sizeof(right));
inc(path, get_two(step_five[p5], mp));
append(left, mp[1]); append(left, mp[2]);
if min_path > path then
begin
min1 := p1; min2 := p2; min3 := p3;
min4 := p4; min5 := p5;
min_path := path;
end;
end;
end;
end;
end;
end;
writeln('min path len = ', min_path);
move(left0, left, sizeof(left));
move(right0, right, sizeof(right));
end;
begin
write('right'); print_array(right);
write('left'); print_array(left);
writeln('starting...');
create_path;
{#1}
path := get_two(step_one[min1], mp);
append(left, mp[1]); append(left, mp[2]);
writeln('right > left: ', mp[1]:4, mp[2]:4, ' time = ', path);
{#2}
inc(path, get_one(step_two[min2], T));
append(right, T);
writeln('left > right: ', T:4, ' time = ', path);
{#3}
inc(path, get_two(step_three[min3], mp));
append(left, mp[1]); append(left, mp[2]);
writeln('right > left: ', mp[1]:4, mp[2]:4, ' time = ', path);
{#4}
inc(path, get_one(step_four[min4], T));
append(right, T);
writeln('left > right: ', T:4, ' time = ', path);
{#5}
inc(path, get_two(step_five[min5], mp));
append(left, mp[1]); append(left, mp[2]);
writeln('right > left: ', mp[1]:4, mp[2]:4, ' time = ', path);
end.