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

时间竟然高达313MS才AC,如何优化?

Posted by frankvista at 2010-11-27 14:14:36 on Problem 1185
const
  MAXN = 100;

type
  statetype = record
    com,cnt : longint;
  end;

var
  M,N,size,upperlim,i,j,k,l,fl0,fl1,ans,tmp,newer : longint;
  state : array[1..1 shl 10] of statetype;
  dp : array[0..1,1..1 shl 10,1..1 shl 10] of longint;
  map : array[-1..MAXN] of longint;
  ch : char;

procedure DFS(now,curr,cnt : longint);
var lowbit : longint;
begin
  repeat
    if now = 0 then begin
      inc(size);
      state[size].com := curr; state[size].cnt := cnt;
      exit;
    end;
    lowbit := now and -now;
    DFS((now xor lowbit) and not (lowbit shl 1) and not (lowbit shl 2),curr or lowbit,cnt+1);
    now := now xor lowbit;
  until false;
end;  

begin
//  assign(input,'cannon.in'); reset(input);
//  assign(output,'cannon.out'); rewrite(output);
  readln(N,M);
  for i := 1 to N do begin
    for j := 0 to M-1 do begin
      read(ch);
      if ch = 'H' then map[i] := map[i] or (1 shl j);
    end;
    readln;
  end;
  upperlim := (1 shl M)-1; map[-1] := 0; map[0] := 0;
  size := 0; DFS(upperlim,0,0);
  filldword(dp[0],size,-1);
  dp[0][size][size] := 0;
  for i := 1 to N do begin
    fl1 := i and 1; fl0 := fl1 xor 1;
    filldword(dp[fl1],size,-1);
    for j := 1 to size do
      if state[j].com and map[i-2] = 0 then
        for k := 1 to size do
          if (state[k].com and map[i-1] = 0) and (state[j].com and state[k].com = 0) and (dp[fl0,j,k] <> -1) then begin
            tmp := state[j].com or state[k].com;
            for l := 1 to size do
              if (state[l].com and map[i] = 0) and (state[l].com and tmp = 0) then begin
                newer := dp[fl0,j,k]+state[l].cnt;
                if newer > dp[fl1,k,l] then dp[fl1,k,l] := newer;
              end;
          end;
  end;
  fl1 := N and 1; ans := 0;
  for i := 1 to size do
    if state[i].com and map[N-1] = 0 then
      for j := 1 to size do
        if state[j].com and map[N] = 0 then
          if (state[i].com and state[j].com = 0) and (dp[fl1,i,j] > ans) then ans := dp[fl1,i,j];
  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