CLOCK - Chỉnh đồng hồ
Tác giả: RR
Ngôn ngữ: Pascal
uses math;
const
next:array['0'..'3',0..3] of char=(
('0','1','2','3'),
('1','2','3','0'),
('2','3','0','1'),
('3','0','1','2'));
var
a:array[1..3,1..3] of char;
kq:array[1..9] of longint;
i,j,res:longint;
procedure update(i,j:longint);
procedure c(u,v:longint);
begin
a[u,v]:=next[a[u,v],j];
end;
begin
case i of
1: begin c(1,1); c(1,2); c(2,1); c(2,2); end;
2: begin c(1,1); c(1,2); c(1,3); end;
3: begin c(1,2); c(1,3); c(2,2); c(2,3); end;
4: begin c(1,1); c(2,1); c(3,1); end;
5: begin c(1,2); c(2,1); c(2,2); c(2,3); c(3,2); end;
6: begin c(1,3); c(2,3); c(3,3); end;
7: begin c(2,1); c(2,2); c(3,1); c(3,2); end;
8: begin c(3,1); c(3,2); c(3,3); end;
9: begin c(2,2); c(2,3); c(3,2); c(3,3); end;
end;
end;
function check:boolean;
var
i,j:longint;
begin
for i:=1 to 3 do
for j:=1 to 3 do
if a[i,j]<>'0' then exit(false);
exit(true);
end;
procedure duyet(i,sum:longint);
var
j:longint;
begin
for j:=0 to 3 do
begin
kq[i]:=j;
update(i,j);
if i<9 then duyet(i+1,sum+j)
else
if check then res:=min(res,sum+j);
if j<>0 then update(i,4-j);
end;
end;
begin
for i:=1 to 3 do
begin
for j:=1 to 3 do read(a[i,j]);
readln;
end;
res:=1000;
duyet(1,0);
writeln(res);
end.