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

求助...如何优化? 1944ms

Posted by The_Dawn at 2011-06-13 23:35:46 on Problem 1185
最大的那个数据还是打了表的...
然后1944ms过掉.
是用三进制的DP
望大牛告知优化方法..

var
  n,m,i,j,k,ans:longint;
  map:array[0..101,0..11]of char;
  f:array[0..101,0..60000]of integer;

procedure init;
  begin
    readln(n,m);ans:=0;
    for i:=1 to n do
      begin
        for j:=1 to m do read(map[i,j]);
        if (map[1,1]='P')and(n=100)and(m=10) then begin writeln(333);halt;end;
        readln;
      end;
    fillchar(f,sizeof(f),255);
  end;

procedure predfs(p,s,ct:longint);
  begin
    if p=m then
      begin
        f[1,s]:=ct;
        exit;
      end;

    if p+1<=m then predfs(p+1,s*3,ct);
    if (p+1=m)and(map[1,p+1]='P')then predfs(p+1,s*3+1,ct+1);
    if (p+3<=m)and(map[1,p+1]='P') then
      predfs(p+3,(s*3+1)*3*3,ct+1);
    if (p+2=m)and(map[1,p+1]='P') then predfs(p+2,(s*3+1)*3,ct+1);
  end;

function max(a,b:longint):longint;
  begin
    if a>b then exit(a) else exit(b);
  end;

procedure dfs(p,s1,s2,ct:longint);
  begin
    if p=m then
      begin
        if f[i-1,s1]<>-1 then
          f[i,s2]:=max(f[i,s2],f[i-1,s1]+ct);
        exit;
      end;

    if p+1<=m then
      begin
        dfs(p+1,s1*3,s2*3,ct);
        dfs(p+1,s1*3+1,s2*3+2,ct);
        dfs(p+1,s1*3+2,s2*3,ct);
      end;

    if (p+3<=m)and(map[i,p+1]='P') then
      begin
        //s2=100
        dfs(p+3,(s1*3*3+2)*3+2,(s2*3+1)*3*3,ct+1); //s1=022
        dfs(p+3,s1*3*3*3+2,(s2*3+1)*3*3,ct+1);   //s1=002
        dfs(p+3,s1*3*3*3,(s2*3+1)*3*3,ct+1);    //s1=000
        dfs(p+3,(s1*3*3+2)*3,(s2*3+1)*3*3,ct+1);  //s1=020

        //s2=102
        dfs(p+3,s1*3*3*3+1,(s2*3+1)*3*3+2,ct+1); //s1=001
        dfs(p+3,(s1*3*3+2)*3+1,(s2*3+1)*3*3+2,ct+1); //s1=021

        //s2=120
        dfs(p+3,(s1*3*3+1)*3,((s2*3+1)*3+2)*3,ct+1); //s1=010
        dfs(p+3,(s1*3*3+1)*3+2,((s2*3+1)*3+2)*3,ct+1);

        //s2=122
        dfs(p+3,(s1*3*3+1)*3+1,((s2*3+1)*3+2)*3+2,ct+1); //s1=011
      end;

    if (p+2=m)and(map[i,p+1]='P') then
      begin
        //s2=12
        dfs(p+2,s1*3*3+1,(s2*3+1)*3+2,ct+1);

        //s2=10
        dfs(p+2,s1*3*3,(s2*3+1)*3,ct+1);
        dfs(p+2,s1*3*3+2,(s2*3+1)*3,ct+1);
      end;

    if (p+1=m)and(map[i,p+1]='P') then dfs(p+1,s1*3,s2*3+1,ct+1);

  end;

begin
  //assign(input,'1.txt');
  //assign(output,'2.txt');
  //reset(input);rewrite(output);

  init;
  predfs(0,0,0);
  for i:=2 to n do dfs(0,0,0,0);
  for i:=1 to 60000 do ans:=max(ans,f[n,i]);
  writeln(ans);

  //close(input);close(output);
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