Codevs P1004 four sons Lian Chess

Source: Internet
Author: User
Tags memory usage
Codevs p1004 four sons Lian Chess

First of all, because of my low strength, so the code is extremely lengthy .... Title Description Description

On a 4*4 board placed 14 pieces, of which there are 7 white pieces, 7 black pieces, two blank areas, any one black and white pieces can be moved up and down four direction to the adjacent space, this is called a step, black and white sides alternately moves, either party can go first, If at some point a piece of any color is formed into four lines (including a slash), the position is the target.

|0| | space |
0| |0| |
|0| | 0|
0| |0| Empty | Enter a description input Description

Read from the file into a 4*4 of the initial game, black pieces with B, white pieces with W, the space zone with O is indicated.
Outputs description Output Description
Moves the number of steps to the target game with a minimum number of steps. sample input to sample

Bwbo
Wbwb
Bwbw
WBWO Sample output sample outputs

5 Analysis

The BFS is directly carried out and exits the program when it is first found to meet the test instructions's solution. Must be the minimum number of steps. The code is as follows (limited strength)

Program p1004;
      Const DX:ARRAY[1..4] of integer= (1,0,-1,0);
DY:ARRAY[1..4] of integer= (0,1,0,-1);
Type ar=array[1..4,1..4] of longint;
    var can:array[1..100000] of ar;
                               QUEUE:ARRAY[1..100000] of record x1,y1,x2,y2,step,pre:longint;
                              Now:ar;
    End
    Ch:char;
    Form:ar;
    Flag:boolean;
I,j,k,head,rear,sum:longint;
function Complete (X,y:longint): boolean;
    var i,j:longint;
Flag:boolean;
 Begin Flag:=true;
      For I:=1 to 4 does begin if form[x,i]<>form[x,y] then begin flag:=false;
     Break
  End
 End
 If Flag then exit (true);
 Flag:=true;
      For I:=1 to 4 does begin if form[i,y]<>form[x,y] then begin flag:=false;
     Break
  End
 End
 If Flag then exit (true);
 Flag:=true;
         If X+y=5 then begin for i:=1 to 4 does begin if form[i,5-i]<>form[x,y] Then Begin Flag:=false;
          Break
      End
     End
    If Flag then exit (true);
 End
 Flag:=true;
         If X=y then begin for i:=1 to 4 does begin if form[i,i]<>form[x,y] then BEGIN
         Flag:=false;
        Break
     End
    End
   If Flag then exit (true);
 End
Exit (FALSE);

End
function Same:boolean;
    var i,j,k:longint;
Flag:boolean;
   Begin for K:=1 to sum do begin flag:=false; For I:=1 to 4 does begin for j:=1 to 4 does if FORM[I,J]&LT;&GT;CAN[K,I,J] then begin F
         Lag:=true;
        Break
     End
    If flag then break;
   End
  If not flag then exit (true);
 End
Exit (FALSE);

End
Procedure BFS;
var x,y,k:longint;
     Begin while head<=rear does begin for k:=1 to 4 does begin x:=queue[head].x1+dx[k];
     Y:=QUEUE[HEAD].Y1+DY[K];
     Form:=queue[head].now;
  if (x>0) and (x<=4) and (y>0) and (y<=4) and (Form[x,y]<>queue[head].pre) then     Begin Form[queue[head].x1,queue[head].y1]:=form[x,y];
        form[x,y]:=0;
           If complete (queue[head].x1,queue[head].y1) THEN begin Writeln (QUEUE[HEAD].STEP+1);
          Exit
        End
           If not same then BEGIN Inc (rear);
           Queue[rear].x1:=x;
           Queue[rear].y1:=y;
           queue[rear].x2:=queue[head].x2;
           Queue[rear].y2:=queue[head].y2;
           QUEUE[REAR].PRE:=FORM[QUEUE[HEAD].X1,QUEUE[HEAD].Y1];
           Queue[rear].now:=form;
           queue[rear].step:=queue[head].step+1;
           Inc (SUM);
          Can[sum]:=form;
       End
    End
   End
     For k:=1 to 4 do begin x:=queue[head].x2+dx[k];
     Y:=QUEUE[HEAD].Y2+DY[K];
     Form:=queue[head].now;
        if (x>0) and (x<=4) and (y>0) and (y<=4) and (Form[x,y]<>queue[head].pre) THEN BEGIN
        Form[queue[head].x2,queue[head].y2]:=form[x,y]; Form[X,y]:=0;
           If complete (queue[head].x2,queue[head].y2) THEN begin Writeln (QUEUE[HEAD].STEP+1);
          Exit
        End
           If not same then BEGIN Inc (rear);
           queue[rear].x1:=queue[head].x1;
           Queue[rear].y1:=queue[head].y1;
           Queue[rear].x2:=x;
           Queue[rear].y2:=y;
           QUEUE[REAR].PRE:=FORM[QUEUE[HEAD].X2,QUEUE[HEAD].Y2];
           Queue[rear].now:=form;
           queue[rear].step:=queue[head].step+1;
           Inc (SUM);
          Can[sum]:=form;
       End
    End
   End
  Inc (head);
End
End
 Begin Head:=1;
 Rear:=1;
 Flag:=true;
     For I:=1 to 4 does begin for j:=1 to 4 does begin read (CH);
         Case Ch of ' B ': Begin queue[1].now[i,j]:=1;
        Can[1,i,j]:=1;
    End
         ' W ': Begin queue[1].now[i,j]:=2;
        can[1,i,j]:=2;
    End
         ' O ': Begin queue[1].now[i,j]:=0;
  can[1,i,j]:=0;       If flag then begin queue[1].x1:=i;
            Queue[1].y1:=j;
           Flag:=false;
            end ELSE begin queue[1].x2:=i;
           Queue[1].y2:=j;
         End
        queue[1].step:=0;
     End
    End
   End
  READLN;
 End
 Queue[1].pre:=-1;
 Sum:=1;
BFs End.
Test Results

Tested by Accepted

Total time: 4 ms
0/0 data passed the test.
Run results
Test point #chess.in Result: AC memory usage: 256kB time consumption: 1ms
Test point #chess1.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess10.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess2.in Result: AC memory usage: 256kB time consumption: 1ms
Test point #chess3.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess4.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess5.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess6.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess7.in Result: AC memory usage: 256kB time consumption: 0ms
Test point #chess8.in Result: AC memory usage: 256kB time consumption: 1ms
Test point #chess9.in Result: AC memory usage: 256kB time usage: 1ms want to

Missing U

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.