USACO 1.4.2 Clocks

http://hi.baidu.com/raulliubo/blog/item/45a1bb447116f680b2b7dcc6.html

yo2那边上不去。。。就来这先写吧。此题为标准的bfs。但是dfs,枚举也能过。我简单的go了一下就过了。。。。就是这个效率。。。。不知我这个搜索叫什么,深度没有下限,搜不到答案就一直往下搜,直到搜到答案为止。哈哈,今天改程序风格了。。。

TASK: clocks
LANG: PASCAL

Compiling...
Compile: OK

Executing...
         Test 1: TEST OK [0.036 secs]
         Test 2: TEST OK [0.008 secs]
         Test 3: TEST OK [0.432 secs]
         Test 4: TEST OK [0.644 secs]
         Test 5: TEST OK [0.528 secs]
         Test 6: TEST OK [0.756 secs]
         Test 7: TEST OK [0.848 secs]
         Test 8: TEST OK [0.856 secs]
         Test 9: TEST OK [0.86 secs]

All tests OK.

请看程序。。。

--------------------------------------------------------华丽的分割线------------------------------------------------------

{
ID:parachutes
PROG:clocks
LANG:PASCAL
}

const method:array[1..9] of string=
('ABDE','ABC','BCEF','ADG','BDEFH','CFI','DEGH','GHI','EFHI');//变换规则。

type tclock=array[1..9] of longint;//记录9个clock
     tans=array[1..100] of longint;//记录答案。

var q,a:tans;
used,clock:tclock;
maxdep:longint;//最大深度

procedure init;//初始化,读入。
var i:longint;
begin
assign(input,'clocks.in'); reset(input);
for i:=1 to 9 do begin
read(clock[i]);
clock[i]:=(clock[i] div 3) and 3;
end;
maxdep:=maxlongint;
close(input);
end;

function check(c:Tclock):boolean;//判断是否全部12点
var i:longint;
begin
for i:=1 to 9 do
if c[i]<>0 then exit(false);
exit(true);
end;

procedure go(dep,last:longint;st:Tclock);//深搜过程。。。
var i,j,k:longint;
ss:Tclock;
begin
if dep>maxdep then exit;
if check(st) and (dep<maxdep) then//找到答案则输出
begin
maxdep:=dep;
a:=q;
exit;
end;
for i:=last to 9 do
if used[i]<4 then begin//可以叫剪枝吧。。。变换4次及以上和没变一样。。
     inc(used[i]);
     q[dep]:=i;
     ss:=st;
     for j:=1 to length(method[i]) do begin
      k:=ord(method[i][j])-64;
      ss[k]:=(ss[k]+1) and 3;
     end;
go(dep+1,i,ss);
dec(used[i]);
end;
end;

procedure print;//答案输出
var i:longint;
begin
assign(output,'clocks.out'); rewrite(output);
dec(maxdep);
for i:=1 to maxdep do
if i<>maxdep then write(a[i],' ') else
writeln(a[i]);
close(output);
end;

begin
init;
go(1,1,clock);
print;
end.

posted @ 2009-01-04 12:37  jesonpeng  阅读(230)  评论(0编辑  收藏  举报