CLOCK - Chỉnh đồng hồ
Tác giả: ll931110
Ngôn ngữ: Pascal
program clock;
const
input = '';
output = '';
len: array[1..9] of longint = (4,3,4,3,5,3,4,3,4);
adj: array[1..9,1..5] of longint =
((1,2,4,5,0),(1,2,3,0,0),(2,3,5,6,0),(1,4,7,0,0),
(2,4,5,6,8),(3,6,9,0,0),(4,5,7,8,0),(7,8,9,0,0),(5,6,8,9,0));
type
arr = array[1..9] of longint;
var
a,b,list: arr;
fi,fo: text;
res: longint;
procedure openfile;
begin
assign(fi, input); reset(fi);
assign(fo, output); rewrite(fo);
end;
procedure closefile;
begin
close(fo); close(fi);
end;
procedure init;
var
i,j: longint;
ch: char;
begin
for i := 1 to 3 do
begin
for j := 1 to 3 do
begin
read(fi, ch);
a[(i - 1) * 3 + j] := ord(ch) - ord('0');
end;
readln(fi);
end;
end;
operator <(x,y: arr) c: boolean;
var
i: longint;
begin
c := false;
for i := 1 to 9 do
if x[i] <> y[i] then
begin
c := x[i] > y[i];
exit;
end;
end;
procedure update;
var
i,j,ss: longint;
begin
b := a;
for i := 1 to 9 do
for j := 1 to len[i] do
b[adj[i,j]] := (b[adj[i,j]] + list[i]) mod 4;
for i := 1 to 9 do if b[i] <> 0 then exit;
ss := 0;
for i := 1 to 9 do ss := ss + list[i];
if ss < res then res := ss;
end;
procedure att(i: longint);
var
j: longint;
begin
for j := 0 to 3 do
begin
list[i] := j;
if i = 9 then update else att(i + 1);
end;
end;
procedure solve;
begin
res := 100;
att(1);
writeln(fo, res);
end;
begin
openfile;
init;
solve;
closefile;
end.