Bugs Integrated, Inc.

Bugs Integrated, Inc.

Gives a \ (n \ times m \) of FIG rectangular grid, which gives the position of the K-th obstacle, wherein up to seek pendulum \ (2 \ times 3 \) rectangular number, \ (n- \ Leq 150, m \ Leq 10 \) .

solution

Noting m the range of data is small, here the band compression, and n each line is processed, setting \ (F [i] [j] \) represents the i-th row before the i-th row j of the program state number , noted, \ (2 \. 3 Times \) of the rectangle may have a length of 3, then the performance is not enough alone binary state, then j is a ternary number, where 2 represents the lattice control grid 2 below , 1 denotes a frame, 0 = no, there are not hard \ (2 \ times 3 \) rectangle is shown at FIG.

2 2
1 1
0 0

1 1 1
0 0 0

Most difficult to learn only \ (^ {3} = 10 59049 \) , clearly timeout, then consider pruning, firstly pretreated with every line legal \ (3 \ times 1,2 \ times 1 \) rectangle filled state, so after only a maximum number of tests (1278 \) \ more, so this is probably only the enumeration \ (1278 ^ 2 \ times 150 = 244.9926 million \) , apparently still time out, and thus the transfer time illegal status can not be enumerated, then continue with dfs transfer pruning, so far from each state will not have \ (1278 \) months, and tested, a state can no update on the status of chess board only 326, Therefore, the time complexity estimation, it was only \ (150 \ times 1278 \ times 326 = 62.4942 million \) , should be able to pass.

Because the card is too dead space, so to scroll through an array of transfer, realization look at the code.

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
using namespace std;
bool M[151][25];
int base[20],x,m,f,a[200][2000],
    at[200],dp[2][60000],tot;
il void read(int&);il int max(int,int);
void dfs1(int,int),dfs2(int,int,int);
int main(){
    int lsy;read(lsy),base[0]=1;
    for(int i(1);i<=19;++i)base[i]=base[i-1]*3;
    while(lsy--){
        int n,K,l;read(n),read(m),read(K);
        memset(M,1,sizeof(M)),memset(dp,-2,sizeof(dp));
        memset(at,0,sizeof(at)),dp[0][0]=0;
        for(int i(1),j,k;i<=K;++i)
            read(j),read(k),M[j][k-1]&=false;
        for(x=1;x<=n;++x)dfs1(0,0);++at[0];
        for(x=0;x<n;++x){
            for(l=1;l<=at[x];++l)
                f=a[x][l],dfs2(0,0,0),
                    cout<<tot<<endl,tot&=0;
            memset(dp[x&1],-2,sizeof(dp[x&1]));
        }printf("%d\n",dp[n&1][0]);
    }
    return 0;
}
il int max(int a,int b){
    return a>b?a:b;
}
void dfs2(int y,int e,int t){
    if(y>=m)return (void)(dp[(x+1)&1][e]=
                          max(dp[(x+1)&1][e],dp[x&1][f]+t));
    if(f/base[y]%3==2){
        if(M[x+1][y]&&M[x+1][y+1])
            dfs2(y+2,e+base[y]+base[y+1],t);
        return;
    }
    else if(f/base[y]%3==1){
        while(f/base[y]%3==1&&y<m)
            if(M[x+1][y])++y;
            else return;
        dfs2(y,e,t);return;
    }
    else{
        if(M[x+1][y]&&M[x+1][y+1]&&y+1<m)
            if(!(f/base[y+1]%3))
                dfs2(y+2,e+base[y]*2+base[y+1]*2,t+1);
        if(M[x+1][y]&&M[x+1][y+1]&&M[x+1][y+2]&&y+2<m)
            if(!(f/base[y+1]%3)&&!(f/base[y+2]%3))
                dfs2(y+3,e+base[y]+base[y+1]+base[y+2],t+1);
    }dfs2(y+1,e,t),++tot;
}
void dfs1(int y,int e){
    if(y==m)return (void)(a[x][++at[x]]=e);
    if(M[x][y]&&M[x][y+1]&&y+1<m){
        dfs1(y+2,e+base[y]+base[y+1]),
            dfs1(y+2,e+base[y]*2+base[y+1]*2);
        if(M[x][y+2]&&y+2<m)
            dfs1(y+3,e+base[y]+base[y+1]+base[y+2]);
    }dfs1(y+1,e);
}
il void read(int &x){
    x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11013958.html