• codevs1050


    题目地址:http://codevs.cn/problem/1050/

    分析:

    最開始想直接用状压做,发现怎么都想不出来。就和当年的多行多米诺骨牌(这道题至少最后还是把普通状压做法看懂了)。

    直到听到 @tsyao 神牛说这个是轮廓线状压。

    大白书(LRJ算法竞赛)第六章第一节好像就是吧。

     

    主体思路:

    一。做法思路比較简单,写起来蛋疼死我了= =,用四进制来保存状态(连通性问题,第六章第三个例题算是这个题的提高)。直接位运算比較方便,可是easy出错。注意运算符的优先级。

    二。把整个棋盘分成5*N个阶段,对于每一个阶段枚举上一个阶段的全部情况。做轮廓线DP。

    注意当前点在行首的特判,由于这个时候它的左边是没有点的。

    还有第一行第一个点的情况。我的做法是開始虚构一行,将其除00000外全部状态的值都赋值为inf,00000 赋值为0。

    三。做完DP后,答案就是最后一个阶段里值最小的一个。

     

    一些小细节:

    一。

    用四进制来推断连通性的正确性证明:

    首先用四进制能够用位运算,这个是选择它的优点之中的一个。

    而这道题比較简单。一个阶段仅仅有5个格子,对于该阶段每个没有联通的黑色(如果白色为0的话,黑色为正整数)。其值不同。已经连接好的值是一样的,我们令最初出现的黑色为1,与它联通的都为1,其它的为2。3,4,5,依此类推。

    (1)可是因为一个阶段仅仅有5个格子,并且假设五个格子里面没有1却有其它数字的黑色。说明不合法。所以黑色最大为5。

    (2)一个阶段中5个格子最多仅仅有3种黑色:假设5个格子均在一行,最多仅仅有  “黑 白 黑 白 黑” 这样的情况,假设不在一排,也仅仅有   “line1: 黑 白 黑 白 line2:黑”  这样的情况。所以 0 1 2 3就能够把全部情况表示出来,因此使用四进制能够解决。

    二。在每次到达一个新阶段而且枚举上一个阶段后:

    (1)将旧阶段左移2位。推断:1-> 其上方是否为黑色,是的话直接赋值为上方黑色的值。 2-> 其上方假设不是黑色。左边假设有格子且为黑色,则赋值为该黑色的值。 3-> 否则赋值为3。

    (2)处理该阶段,将相邻的黑色的值赋值为他们中值较小的黑色的值。

    (3)上面的处理完之后检查一下该阶段,假设当中仅仅有1和3的黑色,将全部3赋值为2。

    (4)处理完一个阶段后,要将四进制中第六位移出(即二进制中第11 12位),而且进行推断,假设移出的不为1,且在现有状态中没有其它数值和它同样的格子,那么新状态不合法,要赋值inf。

    (5)第一个1之前的全部格子不处理,最后一个1之后的格子都不处理(正确性非常好证明,想想就明确了)。

     

    注:

    (1)主要就是这些问题了。假设还有小问题,能够自己模拟检查错误。手写一两组数据即可了。

    这里感谢wr_1737的手写数据帮助我AC= =。

    (2)我DEBUG的方法非常原始,PRINTF大法= =。打出来的16W行数据我看了两三遍。DEBUG接近两个小时才过= =,细节错误一个一个找出来的。

    (3)以下发一个代码,里面的DEBUG过程我凝视掉了。能够删了再看,另一些DEBUG过程中加的思路凝视,能够看看。

    代码结合上面的分析看起来会easy点。

    代码:http://paste.ubuntu.com/7299284/

    /*我是一个从来不发BLOG骗訪问量的真诚的人*/

    /*

    为了那16W行的DEBUG数据,认为有帮助的同学点一个赞吧。

    (

    求验证的同学能够把DEBUG凝视全打开,然后输入下面输入:

    5
    10101
    01010
    10101
    01010
    10101

    )

    代码:

    const
        maxn=102;
    type
        node=record
          x,y:longint;
        end;
        aa=array[0..6]of longint;
    var
        f:array[0..maxn,0..1204]of longint;
        a:array[0..maxn]of longint;
        fa:array[1..4]of longint;
        flag:array[1..4]of boolean;
        n:longint;
    
    procedure init;
    var
        i,j:longint;
        s:char;
    begin
        readln(n);
        for i:=1 to n do
          begin
            for j:=1 to 5 do
              begin
                read(s);
                a[i]:=a[i]<<1+ord(s)-ord('0');
              end;
            readln;
          end;
        while n>0 do
          begin
            if a[n]>0 then break;
            dec(n);
          end;
        if n=0 then
        begin
          write(0);
          halt;
        end;
    end;
    
    procedure change(var a:aa;b,c:longint);
    var
        i:longint;
    begin
        for i:=1 to 5 do
          if a[i]=b then
          begin
            a[i]:=c;
            if (a[i-1]<>0) and (a[i-1]<10) then change(a,a[i-1],c);
            if (a[i+1]<>0) and (a[i+1]<10) then change(a,a[i+1],c);
          end;
    end;
    
    procedure get(var a:aa);
    var
        i,c:longint;
    begin
        c:=10;
        for i:=1 to 5 do
          if (a[i]<>0) and (a[i]<10) then
          begin
            inc(c);
            change(a,a[i],c);
          end;
        for i:=1 to 5 do
          if a[i]>0 then dec(a[i],10);
    end;
    
    function bit(x:longint):longint;
    begin
        if x=0 then exit(0);
        exit(bit(x-(x and -x))+1);
    end;
    
    var
        q:array[0..maxn*1024]of node;
    
    procedure work;
    var
        head,tail,i,j,k,ans,save:longint;
        s,t:aa;
        flag:boolean;
    begin
        fillchar(f,sizeof(f),1);
        t[0]:=0;
        t[6]:=0;
        ans:=500;
        f[0,0]:=0;
        q[1].x:=0;
        q[1].y:=0;
        head:=1;
        tail:=1;
        while head<=tail do
          begin
            save:=q[head].y;
            for i:=1 to 5 do
              begin
                s[i]:=q[head].y and 3;
                q[head].y:=q[head].y>>2;
              end;
            q[head].y:=save;
            if q[head].x=n then
            begin
              flag:=true;
              for i:=1 to 5 do
                if s[i]>1 then flag:=false;
              if flag then
              if ans>f[q[head].x,q[head].y] then ans:=f[q[head].x,q[head].y];
              inc(head);
              continue;
            end;
            for i:=0 to 31 do
              if i and a[q[head].x+1]=0 then
              begin
                for j:=1 to 5 do
                  t[j]:=(((a[q[head].x+1]+i)>>(j-1))and 1)*(j+3);
                k:=0;
                for j:=1 to 5 do
                  if (s[j]>0) and (t[j]>0) then
                  begin
                    t[j]:=s[j];
                    k:=k or (1<<s[j]);
                  end;
                flag:=true;
                for j:=1 to 5 do
                  if (s[j]>0) and (k and (1<<s[j])=0) then flag:=false;
                if flag=false then continue;
                get(t);
                k:=0;
                for j:=5 downto 1 do
                  k:=k<<2+t[j];
                if f[q[head].x+1,k]>500 then
                begin
                  inc(tail);
                  q[tail].x:=q[head].x+1;
                  q[tail].y:=k;
                end;
                if f[q[head].x+1,k]>f[q[head].x,q[head].y]+bit(i) then f[q[head].x+1,k]:=f[q[head].x,q[head].y]+bit(i);
              end;
            inc(head);
          end;
        write(ans);
    end;
    
    begin
        init;
        work;
    end.

  • 相关阅读:
    罗技 M280 无线鼠标 All In One
    How to use macOS to connect to Raspberry Pi without the monitor All In One
    lodashes All In One
    Raspberry Pi 5 All In One
    Vite All In One
    GitHub & All Contributors All In One
    Vite 运行 TypeScript 文件原理剖析 All In One
    VNC Viewer All In One
    python中 把每条FASTA序列分割成特定个数个字母一行的序列
    c 语言中实现数组元素的逆向排列
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6698661.html
Copyright © 2020-2023  润新知