Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

没有A掉的人要注意,不一定光标移到的位置才能做up或down操作,swap6也可以对第6位做

Posted by lifeich1 at 2010-02-04 23:45:22 on Problem 1184
思路:把up和down操作分离出来,无视left操作。。。
想不出来看程序。。
const dd:array[1..5] of longint=(120,24,6,2,1);
type node=array[0..8] of longint;
var
    lis:array[0..30000] of node;
    hash:array[0..1000,0..10] of boolean;
    a1,b1,tmp,d:node;
    ans,q1,q2,i,p:longint;
procedure init();
var i:longint;
    s,s1:string;
begin
    readln(s1);
    s:=copy(s1,1,6);
    for i:=1 to 6 do a1[i]:=ord(s[i])-48;
    s:=copy(s1,8,6);
    for i:=1 to 6 do b1[i]:=ord(s[i])-48;
    ans:=maxlongint;
end;
procedure work();
procedure swap(var a,b:longint);
var t:longint;begin t:=a;a:=b;b:=t;end;
function get_hash(a:node):longint;
var i,j,e:longint;
begin
    e:=0;
    for i:=1 to 6 do d[i]:=i;
    for i:=1 to 5 do begin
        inc(e,(d[a[i]]-1)*dd[i]);
        for j:=a[i] to 6 do dec(d[j]);
    end;
    exit(e);
end;
procedure new_node(a:node);
var e:longint;
begin
    e:=get_hash(a);
    if hash[e][a[7]] then exit
    else hash[e][a[7]]:=true;
    inc(q2);
    move(a,lis[q2],sizeof(a));
end;
procedure try_answer(a:node);
var i,e:longint;
begin
    e:=a[0];
    for i:=1 to 6 do   //a[8]是有没有swap6过的标示
        if (not((i=6)and(a[8]=1)))and(i>a[7])and(a1[a[i]]<>b1[i]) then exit
        else inc(e,abs(a1[a[i]]-b1[i]));
    if e<ans then ans:=e;
end;
begin
    q1:=1;q2:=0;
    for i:=1 to 6 do lis[1][i]:=i;lis[1][7]:=1;
    new_node(lis[1]);
    try_answer(lis[0]);
    while q1<=q2 do begin
        inc(lis[q1][0]);
        move(lis[q1],tmp,sizeof(tmp));
        p:=tmp[7];
        if p>1 then begin
            swap(tmp[1],tmp[p]);
            new_node(tmp);
            try_answer(tmp);
        end;
        move(lis[q1],tmp,sizeof(tmp));
        if p<6 then begin
            swap(tmp[6],tmp[p]);
            tmp[8]:=1;
            new_node(tmp);
            try_answer(tmp);
        end;
        move(lis[q1],tmp,sizeof(tmp));
        if p<6 then begin
            inc(tmp[7]);
            new_node(tmp);
            try_answer(tmp);
        end;
        dec(lis[q1][0]);
        inc(q1);
    end;
end;
procedure print();
begin
    writeln(ans);
end;
begin
    init();
    work();
    print();
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator