在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
● | ○ | ● | |
○ | ● | ○ | ● |
● | ○ | ● | ○ |
○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
hi
分析:
其实这也是一个裸的bfs,判重不用都可以过了。。
代码:
var
d:array[1..100000,1..4,1..4]of char;
map:array[1..4,1..4]of longint;
s:string;
t:char;
i,j,head,tail,ii,jj:longint;
cost:array[1..100000]of longint;
p:array[1..100000]of char;
begin
for i:=1 to 4 do begin
readln(s);
for j:=1 to 4 do begin
d[1,i,j]:=s[j];
end;
end;
head:=1;
tail:=1;
while head<=tail do begin
if (d[head,1,1]=d[head,1,2])and(d[head,1,2]=d[head,1,3])and(d[head,1,3]=d[head,1,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,2,1]=d[head,2,2])and(d[head,2,2]=d[head,2,3])and(d[head,2,3]=d[head,2,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,3,1]=d[head,3,2])and(d[head,3,2]=d[head,3,3])and(d[head,3,3]=d[head,3,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,4,1]=d[head,4,2])and(d[head,4,2]=d[head,4,3])and(d[head,4,3]=d[head,4,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,1]=d[head,2,1])and(d[head,2,1]=d[head,3,1])and(d[head,3,1]=d[head,4,1]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,2]=d[head,2,2])and(d[head,2,2]=d[head,3,2])and(d[head,3,2]=d[head,4,2]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,3]=d[head,2,3])and(d[head,2,3]=d[head,3,3])and(d[head,3,3]=d[head,4,3]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,4]=d[head,2,4])and(d[head,2,4]=d[head,3,4])and(d[head,3,4]=d[head,4,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,1]=d[head,2,2])and(d[head,2,2]=d[head,3,3])and(d[head,3,3]=d[head,4,4]) then begin
writeln(cost[head]);
halt;
end;
if (d[head,1,4]=d[head,2,3])and(d[head,2,3]=d[head,3,2])and(d[head,3,2]=d[head,4,1]) then begin
writeln(cost[head]);
halt;
end;
for i:=1 to 4 do for j:=1 to 4 do begin
if d[head,i,j]='O' then begin
if p[head]<>'B' then begin
if (i>1)and(d[head,i-1,j]='B') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i-1,j];
d[tail,i-1,j]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='B';
cost[tail]:=cost[head]+1;
end;
if (j>1)and(d[head,i,j-1]='B') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i,j-1];
d[tail,i,j-1]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='B';
cost[tail]:=cost[head]+1;
end;
if (i<4)and(d[head,i+1,j]='B') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i+1,j];
d[tail,i+1,j]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='B';
cost[tail]:=cost[head]+1;
end;
if (j<4)and(d[head,i,j+1]='B') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i,j+1];
d[tail,i,j+1]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='B';
cost[tail]:=cost[head]+1;
end;
end;
if p[head]<>'W' then begin
if (i>1)and(d[head,i-1,j]='W') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i-1,j];
d[tail,i-1,j]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='W';
cost[tail]:=cost[head]+1;
end;
if (j>1)and(d[head,i,j-1]='W') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i,j-1];
d[tail,i,j-1]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='W';
cost[tail]:=cost[head]+1;
end;
if (i<4)and(d[head,i+1,j]='W') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i+1,j];
d[tail,i+1,j]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='W';
cost[tail]:=cost[head]+1;
end;
if (j<4)and(d[head,i,j+1]='W') then begin
inc(tail);
for ii:=1 to 4 do for jj:=1 to 4 do d[tail,ii,jj]:=d[head,ii,jj];
t:=d[tail,i,j+1];
d[tail,i,j+1]:=d[tail,i,j];
d[tail,i,j]:=t;
p[tail]:='W';
cost[tail]:=cost[head]+1;
end;
end;
end;
end;
inc(head);
end;
end.