奶牛们按不太传统的方式玩起了小孩子们玩的"跳房子"游戏。奶牛们创造了一个5x5的、由与x,y轴平行的数字组成的直线型网格,而不是用来在里面跳的、线性排列的、带数字的方格。然后他们熟练地在网格中的数字中跳:向前跳、向后跳、向左跳、向右跳(从不斜过来跳),跳到网格中的另一个数字上。他们再这样跳啊跳(按相同规则),跳到另外一个数字上(可能是已经跳过的数字)。一共在网格内跳过五次后,他们的跳跃构建了一个六位整数(可能以0开头,例如000201)。
求出所有能被这样创造出来的不同整数的总数。
样例输入
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1
样例输出
15
var a,b,c,i,u,q1,q2,q:longint;
v:array[0..16000,1..4]of longint;
y:array[1..5,1..5]of integer;
t:array[0..10889]of longint;
const r:array[1..4,1..2]of integer=((1,0),(0,1),(-1,0),(0,-1));
procedure ss(l,r:longint);
var
i,j,mid:longint;
begin
i:=l;
j:=r;
mid:=t[(i+j) div 2];
while i<=j do begin
while t[i]<mid do inc(i);
while t[j]>mid do dec(j);
if i<=j then begin
t[0]:=t[i];
t[i]:=t[j];
t[j]:=t[0];
inc(i);
dec(j);
end;
end;
if j>l then ss(l,j);
if i<r then ss(i,r);
end;
procedure bb;
begin
ss(1,u);
b:=0;
t[0]:=maxint;
for a:=1 to u do
if t[a]<>t[a-1] then inc(b);
writeln(b);
halt;
end;
begin
assign(input,'numgrid.in');reset(input);
assign(output,'numgrid.out');rewrite(output);
for a:=1 to 5 do
begin
for b:=1 to 5 do
begin
inc(c);
read(v[c,1]);
y[a,b]:=v[c,1];
v[c,2]:=a;
v[c,3]:=b;
v[c,4]:=1;
end;
readln;
end;
q1:=0;
q2:=25;
while q1<q2 do
begin
inc(q1);
for i:=1 to 4 do
begin
if ((v[q1,2]+r[i,1])in [1..5])and
((v[q1,3]+r[i,2])in[1..5]) then
begin
inc(q2);
v[q2,2]:=v[q1,2]+r[i,1];
v[q2,3]:=v[q1,3]+r[i,2];
v[q2,1]:=y[v[q2,2],v[q2,3]]+v[q1,1]*10;
v[q2,4]:=v[q1,4]+1;
if v[q2,4]=6 then
begin
inc(u);
t[u]:=v[q2,1];
end;
if v[q2,4]=7 then
begin
bb;
end;
end;
end;
end;
close(input);
close(output);
end.