codevs天梯 四子连棋

1004 四子连棋

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
题目描述 Description

在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。

 
 

 

输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description

用最少的步数移动到目标棋局的步数。

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值