讨论 / Pascal满分
dzy123 2018-07-18 23:07:49
点我顶贴 收藏 删除
const

dx:array[1..4] of integer=(0,1,0,-1);

dy:array[1..4] of integer=(1,0,-1,0);

var

a:array[0..101,0..101] of char;

t:array[-10..30000,1..2] of longint;

state:array[0..30000] of longint;

xx:array[1..101,1..101] of longint;

oo:array['A'..'Z'] of longint;

i,j,k,n,m:longint;

ch:char;

f,fl:boolean;

st:string;

procedure bfs;

var

head,tail,i,j,k:longint;

begin

head:=0; tail:=1;

t[1][1]:=1; t[1][2]:=1;

if a[1][1]<>'0' then

begin

for i:=1 to n do

begin

for j:=1 to n do

if (a[i,j]=a[1,1]) and (i<>1) or (j<>1) then

begin

f:=true;

break;

end;

if f then

begin

t[1][1]:=i;

t[1][2]:=j;

break;

end;

end;

end;

while (head<=tail) do

begin

inc(head);

if (t[head,1]=5) and (t[head,2]=11) then

f:=false;

for i:=1 to 4 do

if (t[head][1]+dx[i]>=1) and (t[head][1]+dx[i]<=n) and (t[head][2]+dy[i]>=1) and (t[head][2]+dy[i]<=m) and (a[t[head][1]+dx[i],t[head][2]+dy[i]]<>'1') and (xx[t[head][1]+dx[i],t[head][2]+dy[i]]=0) then

begin

if (a[t[head][1]+dx[i],t[head][2]+dy[i]]>='A') and ((a[t[head][1]+dx[i],t[head][2]+dy[i]]<='Z')) then

begin

ch:=a[t[head][1]+dx[i],t[head][2]+dy[i]];

f:=false;

for j:=1 to n do

begin

for k:=1 to m do

if (a[j,k]=ch) and ((j<>t[head][1]+dx[i]) or (k<>t[head][2]+dy[i])) then

begin

f:=true;

break;

end;

if f then break;

end;

inc(tail);

t[tail][1]:=j;

t[tail][2]:=k;

// if (oo[a[t[tail,1],t[tail,2]]]>4) then

xx[t[head][1]+dx[i],t[head][2]+dy[i]]:=1; //else inc(oo[a[t[head,1],t[head,2]]]);

state[tail]:=state[head]+1;

if (t[tail][1]=n) and (t[tail][2]=m) then

begin

writeln(state[tail]);

fl:=true;

exit;

end;

end;

if (a[t[head][1]+dx[i],t[head][2]+dy[i]]='0') then

begin

inc(tail);

t[tail][1]:=t[head][1]+dx[i];

t[tail][2]:=t[head][2]+dy[i];

f:=false;

for j:=1 to 4 do

if a[t[tail][1]+dx[j],t[tail][2]+dy[j]]='0' then f:=true;

if f then

xx[t[tail][1],t[tail][2]]:=1;

state[tail]:=state[head]+1;

if (t[tail][1]=n) and (t[tail][2]=m) then

begin

writeln(state[tail]);

fl:=true;

exit;

end;

end;

end;

end;

end;

begin

//assign(input,'way5.in'); reset(input);

readln(n,m);

for i:=1 to n do

begin

readln(st);

for j:=1 to m do

a[i,j]:=st[j];

end;

xx[1,1]:=1;

bfs;

if not fl then

writeln('No Solution.');

end.

#1 k8@2018-08-25 06:35:21
回复 删除
这么长
查看更多回复
提交回复