USACO 6.5.4 The Clocks bfs

来源:互联网 发布:在vmware下安装ubuntu 编辑:程序博客网 时间:2024/05/17 08:35

朴素bfs就好了。用四进制记录状态然后判重。

代码:

{ID: ymwbegi1PROG: clocksLANG: PASCAL} var  w:array[1..9] of longint;  state:array[1..300000,1..9] of longint;  v:array[0..300000] of boolean;  flag:boolean;  i,x:longint;  f,d:array[1..300000] of longint;  s:array[0..9] of longint;  a:array[1..9,0..5] of longint;procedure print(x:longint);begin  if f[x]=0 then exit;  print(f[x]);  if flag    then flag:=false    else write(' ');  write(d[x]);end;procedure bfs;var  head,tail,i,j,tot:longint;begin  head:=0;  tail:=1;  repeat    inc(head);    for i:=1 to 9 do    begin      w:=state[head];      for j:=1 to a[i,0] do        w[a[i,j]]:=(w[a[i,j]]+1) mod 4;      tot:=0;      for j:=1 to 9 do        tot:=tot+s[j-1]*w[j];      if v[tot] then      begin        inc(tail);        state[tail]:=w;        f[tail]:=head;        d[tail]:=i;        v[tot]:=false;        if tot=0 then        begin          print(tail);          writeln;          exit;        end;      end;    end;  until head>=tail;end;procedure work;begin  a[1,0]:=4; a[1,1]:=1; a[1,2]:=2; a[1,3]:=4; a[1,4]:=5;  a[2,0]:=3; a[2,1]:=1; a[2,2]:=2; a[2,3]:=3;  a[3,0]:=4; a[3,1]:=2; a[3,2]:=3; a[3,3]:=5; a[3,4]:=6;  a[4,0]:=3; a[4,1]:=1; a[4,2]:=4; a[4,3]:=7;  a[5,0]:=5; a[5,1]:=2; a[5,2]:=4; a[5,3]:=5; a[5,4]:=6; a[5,5]:=8;  a[6,0]:=3; a[6,1]:=3; a[6,2]:=6; a[6,3]:=9;  a[7,0]:=4; a[7,1]:=4; a[7,2]:=5; a[7,3]:=7; a[7,4]:=8;  a[8,0]:=3; a[8,1]:=7; a[8,2]:=8; a[8,3]:=9;  a[9,0]:=4; a[9,1]:=5; a[9,2]:=6; a[9,3]:=8; a[9,4]:=9;end;begin  assign(input,'clocks.in');  assign(output,'clocks.out');  reset(input);  rewrite(output);  work;  flag:=true;  for i:=1 to 9 do  begin    read(x);    case x of      3:state[1,i]:=1;      6:state[1,i]:=2;      9:state[1,i]:=3;      12:state[1,i]:=0;    end;  end;  s[0]:=1;  for i:=1 to 9 do    s[i]:=s[i-1]*4;  fillchar(v,sizeof(v),true);  bfs;  close(input);  close(output);end.


0 0