弱弱的搜索题,
我的做法是将矩阵看做二进制然后用位运算来做的,感觉比较舒服
const dx:array[..] of integer=(-,,,);
dy:array[..] of integer=(,,-,); type node=record
po,next:longint;
end; var a:array[..] of node;
d,q:array[..] of longint;
p:array[..] of longint;
num:array[..,..] of longint;
i,j,n,k,m,st,en,len,x,y:longint;
s:string; procedure add(x,y:longint);
begin
inc(len);
a[len].po:=y;
a[len].next:=p[x];
p[x]:=len;
end; procedure bfs;
var u,j,i,k,x,y,f,r:longint;
begin
f:=;
r:=;
q[]:=st;
fillchar(d,sizeof(d),);
d[st]:=;
while f<=r do
begin
x:=q[f];
k:=;
for i:= to do
begin
if k and x<> then
begin
j:=p[i];
while j<>- do
begin
u:= shl a[j].po;
y:=(x xor k) or u;
if (x and u=) and (d[y]=-) then
begin
d[y]:=d[x]+;
if y=en then exit;
inc(r);
q[r]:=y;
end;
j:=a[j].next;
end;
end;
k:=k shl ;
end;
inc(f);
end;
end; begin
fillchar(num,sizeof(num),);
fillchar(p,sizeof(p),);
for i:= to do
begin
readln(s);
for j:= to do
begin
num[i,j]:=k;
st:=st+(ord(s[j])-)*( shl k);
inc(k);
end;
end;
readln;
for i:= to do
begin
readln(s);
for j:= to do
en:=en+(ord(s[j])-)*( shl num[i,j]);
end;
for i:= to do
for j:= to do
for k:= to do
begin
x:=i+dx[k];
y:=j+dy[k];
if num[x,y]<>- then add(num[i,j],num[x,y]);
end;
bfs;
writeln(d[en]);
end.