zoukankan      html  css  js  c++  java
  • [SCOI2011][bzoj2331] 地板 [插头dp]

    题面:

    传送门

    思路:

    插头dp基础教程

    这个L形......第一眼看上去真的是丧病啊

    但是仔细想想,实际上也就是拿一堆路径铺满一个棋盘,这个路径还是有限制的

    那还有什么好说的,插头dp上啊【雾】

    首先。我们定义这道题中的一个插头代表着从这个格子往插头指向的方向可以伸展一个L形

    但是整张图上的所有插头不可能只有一种,因此我们要分析,可以继续延伸的L形都有哪些限制呢?

    我们知道,L形的一个特点就是拐了一个弯,而且只拐了一个弯,因此我们可以定义这样的两种插头:

    插头1代表一个还没有拐过弯的L形的延伸,插头2则代表一个已经拐过弯的L形的延伸

    这样我们解决了定义问题,接下来看如何分类讨论

    第一种情况:当前状态下,当前格子上方和左方都没有插头

    这时我们需要找一个L形来把这个格子填上,那么我们可能有三种决策:

      决策一:给这个格子加一个二号下插头和一个二号右插头,此时这个格子是一个新的L形的拐角

      决策二:给这个格子加一个一号下插头,此时相当于构建了一个先向下再向右的L形,从当前格子开始

      决策三:给这个格子加一个一号右插头,此时相当于构建了一个先向右再向下的L形,从当前格子开始

    第二种情况:当前状态下,当前格子上方有一个一号下插头,左方没有插头

    这时相当于上面有一个L形的未拐弯的一边伸过来了,有两种决策:

      决策一:L形不拐弯,继续向下延伸,当前格子只有一个一号下插头

      决策二:L形在当前格子拐弯,L形向右延伸,当前格子只有一个二号右插头

    第三种情况:当前状态下,当前格子左方有一个一号右插头,上方没有插头

    与第二种情况类似,故不赘述

    第四种情况:当前状态下,当前格子上方有一个二号下插头,左方没有插头

    这时相当于上面有一个L形的拐过弯的一边伸过来了,有两种决策:

      决策一:L形继续延伸,当前格子有一个二号下插头

      决策二:L形在当前格子终止,当前格子没有插头

    决策二时注意,如果当前处在最后一个没有障碍的格子,那么需要统计入最终答案

    第五种情况:当前状态下,当前格子左方有一个二号右插头,上方没有插头

    与第四种情况类似,故不赘述

    第六种情况:当前状态下,当前格子左方和上方都有一号插头

    此时相当于两条“臂”伸了过来,在当前格子相交,应该合并成一个L形

    当前格子相当于一个L形的拐弯处,没有插头

    我们发现,情况一和情况六已经覆盖了四种L形的摆放方式,同时也不会有一个二号插头一个一号插头或者两个二号插头的情况出现——它们被上文的六种情况限制了

    故这种分类讨论方式可以覆盖所有情况

    状态可以用四进制表示(比较快),因为状态数较多,放到哈希表里面,滚动数组处理即可

    Code:

      1 // luogu-judger-enable-o2
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<algorithm>
      6 #define hash deep_dark_fantasy
      7 #define ll long long
      8 #define MOD 20110520
      9 using namespace std;
     10 inline int read(){
     11     int re=0,flag=1;char ch=getchar();
     12     while(ch>'9'||ch<'0'){
     13         if(ch=='-') flag=-1;
     14         ch=getchar();
     15     }
     16     while(ch>='0'&&ch<='9') re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
     17     return re*flag;
     18 }
     19 int n,m,x[150][150],cur,pre,ex,ey;
     20 int st[2][300010];ll ans[2][300010],re;
     21 int tot[2],bit[20],state[300010],st_tot,hash=300000;
     22 struct edge{
     23     int to,next;
     24 }a[300010];
     25 void insert(int now,ll val){
     26     int p=now%hash;
     27     for(int i=state[p];i;i=a[i].next){
     28         if(st[cur][a[i].to]==now){
     29             ans[cur][a[i].to]+=val;
     30             ans[cur][a[i].to]%=MOD;return;
     31         }
     32     }
     33     tot[cur]++;
     34     a[++st_tot].to=tot[cur];
     35     a[st_tot].next=state[p];
     36     state[p]=st_tot;st[cur][tot[cur]]=now;ans[cur][tot[cur]]=val%MOD;
     37 }
     38 void dp(){
     39     int i,j,k,down,right,now;ll val;
     40     cur=0;tot[cur]=1;ans[cur][1]=1;st[cur][1]=0;
     41     for(i=1;i<=n;i++){
     42         for(j=1;j<=tot[cur];j++) st[cur][j]<<=2;
     43         for(j=1;j<=m;j++){
     44             memset(state,0,sizeof(state));st_tot=0;
     45             pre=cur;cur^=1;tot[cur]=0;
     46             for(k=1;k<=tot[pre];k++){
     47                 now=st[pre][k];val=ans[pre][k];
     48                 right=(now>>bit[j-1])%4;down=(now>>bit[j])%4;
     49                 if(!x[i][j]){//障碍格子
     50                     if(!down&&!right){
     51                         insert(now,val);continue;
     52                     }
     53                 }
     54                 if(!right&&!down){//第一种情况
     55                     if(x[i+1][j]&&x[i][j+1])
     56                         insert(now+((1<<bit[j-1])<<1)+((1<<bit[j])<<1),val);
     57                     if(x[i+1][j]) insert(now+(1<<bit[j-1]),val);
     58                     if(x[i][j+1]) insert(now+(1<<bit[j]),val);
     59                 }
     60                 if(right==1&&!down){//第三种情况
     61                     if(x[i][j+1]) insert(now-(1<<bit[j-1])+(1<<bit[j]),val);
     62                     if(x[i+1][j]) insert(now+(1<<bit[j-1]),val);
     63                 }
     64                 if(down==1&&!right){//第二种情况
     65                     if(x[i+1][j]) insert(now-(1<<bit[j])+(1<<bit[j-1]),val);
     66                     if(x[i][j+1]) insert(now+(1<<bit[j]),val);
     67                 }
     68                 if(right==2&&!down){//第五种情况
     69                     if(i==ex&&j==ey) re+=val,re%=MOD;
     70                     if(x[i][j+1]) insert(now-((1<<bit[j-1])<<1)+((1<<bit[j])<<1),val);
     71                     insert(now-((1<<bit[j-1])<<1),val);
     72                 }
     73                 if(down==2&&!right){//第四种情况
     74                     if(i==ex&&j==ey) re+=val,re%=MOD;
     75                     if(x[i+1][j]) insert(now-((1<<bit[j])<<1)+((1<<bit[j-1])<<1),val);
     76                     insert(now-((1<<bit[j])<<1),val);
     77                 }
     78                 if(down==1&&right==1){//第六种情况
     79                     if(i==ex&&j==ey) re+=val,re%=MOD;
     80                     insert(now-(1<<bit[j-1])-(1<<bit[j]),val);
     81                 }
     82             }
     83         }
     84     }
     85 }
     86 int main(){
     87     int i,j;char ch;
     88     n=read();m=read();
     89     for(i=1;i<=20;i++) bit[i]=i<<1;
     90     if(n>m){
     91         for(i=1;i<=n;i++){
     92             for(j=1;j<=m;j++){
     93                 ch=getchar();
     94                 while(ch!='*'&&ch!='_') ch=getchar();
     95                 x[i][j]=(ch=='_');
     96                 if(x[i][j]) ex=i,ey=j;
     97             }
     98         }
     99     }
    100     else{
    101         swap(n,m);
    102         for(i=m;i>0;i--){
    103             for(j=1;j<=n;j++){
    104                 ch=getchar();
    105                 while(ch!='*'&&ch!='_') ch=getchar();
    106                 x[j][i]=(ch=='_');
    107                 if(x[j][i]&&((j>ex)||(j==ex&&i>ey))) ex=j,ey=i;
    108             }
    109         }
    110     }
    111     dp();
    112     printf("%lld",re);
    113 }
  • 相关阅读:
    7月的尾巴,你是XXX
    戏说Android view 工作流程《下》
    “燕子”
    Android开机动画bootanimation.zip
    戏说Android view 工作流程《上》
    ViewController里已连接的IBOutlet为什么会是nil
    My first App "Encrypt Wheel" is Ready to Download!
    iOS开发中角色Role所产生的悲剧(未完)
    UIScrollView实现不全屏分页的小技巧
    Apple misunderstood my app,now my app status changed to “In Review”
  • 原文地址:https://www.cnblogs.com/dedicatus545/p/8630522.html
Copyright © 2011-2022 走看看