题解:P2337 [SCOI2012] 喵星人的入侵

· · 题解

#include <cstdio>
#include <vector>
#include <cstring>
#define ll long long
#define ull unsigned ll
#pragma GCC optimize(2)/*以我的常数可能有点悬...*/
using std::vector;

const int M =1e6+3;

int n, m, K;
char map[21][7];
struct qwq{
    int state/*插头信息*/, state2/*地图格子信息*/;
    int mpl/*新格子左上角格子信息*/, battery/*炮台数*/;
    int pre;
    ll sum;
    qwq(int s, int s2, int mp, int b, int p, ll su)
        :state(s), state2(s2), mpl(mp), battery(b), pre(p), sum(su){}
};
int pos[M];
bool used[M];
vector<qwq> hsh, dp;

inline int get(int state, int x){
    int p =(1<<(x<<1));
    int a =(bool)(state&p), b =(bool)(state&(p<<1));
    return a+(b<<1);
}

inline void upd(int &state, int x, int val){ state +=-(get(state, x)<<(x<<1))+(val<<(x<<1)); }

/*hash 链式前向星*/
inline void push(qwq nw){
    int key=(((((ull)nw.state<<18)|(ull)nw.state2)<<4)|nw.battery)%M;
//  unsigned ll tmp =(nw.state-nw.state2+nw.battery);
//  int key =tmp*tmp%M;
    if(!used[key]){
        used[key] =1;
        nw.pre =-1;
        hsh.push_back(nw);
        pos[key] =hsh.size()-1;
    }
    else{
        for(int p =pos[key]; p != -1; p =hsh[p].pre)
            if(hsh[p].state == nw.state && hsh[p].state2 == nw.state2 && hsh[p].battery == nw.battery && hsh[p].mpl == nw.mpl){
                if(nw.sum > hsh[p].sum)
                    hsh[p].sum =nw.sum;
                return;
            }
        nw.pre =pos[key];
        hsh.push_back(nw);
        pos[key] =hsh.size()-1;
    }
}

inline void clearhsh(){
    hsh.clear();
    memset(used, 0, sizeof(used));/*比起清空 pos 会稍微节省些常数*/
}

inline int count(int mp1, int lmp, int mp2, int rmp, int typ){
    int cnt =0;
    if(lmp == typ) ++cnt;
    if(mp1 == typ) ++cnt;
    if(mp2 == typ) ++cnt;
    if(rmp == typ) ++cnt;
    return cnt;
}

/*给定一个括号位置,把这个括号对替换成指定插头*/
inline void updbracket(int &state, int posi, int val){
    int x, d, cnt =0, pg =get(state, posi), pg_;
    if(pg == 1)
        d =1, pg_ =2;
    else
        d =-1, pg_ =1;
    for(x =posi+d; cnt >= 0; x +=d){
        if(get(state, x) == pg) ++cnt;
        else if(get(state, x) == pg_) --cnt;
    }
    x -=d;
    upd(state, posi, val), upd(state, x, val);
}

inline void work(){
    clearhsh();
    dp.push_back(qwq(0, 0, 0, 0, -1, 0));
    for(int i =0; i < n; ++i){
        for(int k =0; k < (int)dp.size(); ++k)
            dp[k].state <<=2, dp[k].state2 <<=2, dp[k].mpl =0;
        for(int j =0; j < m; ++j){
            for(int k =0; k < (int)dp.size(); ++k){
                qwq nw =dp[k];
                if(get(nw.state, m+1) != 0)
                    continue;
                upd(nw.state2, m+1, 0);/*去掉换行产生的多余地图信息*/
                /*0 无插头,1 左括号,2 右括号,3 独立插头*/
                /*0 障碍,1 路径,2 炮台*/
                /*pg1, mp1 左,pg2,mp2 上*/
                int pg1 =get(nw.state, j), pg2 =get(nw.state, j+1);/*plug*/
                int mp1 =get(nw.state2, j), mp2 =get(nw.state2, j+1);/*map*/
                int lmp =nw.mpl/*左上角*/, rmp =get(nw.state2, j+2)/*右上角*/;
                nw.mpl =mp2;/*记录新状态左上角*/
                if(map[i][j] == '#'){
                    if(pg1 == 0 && pg2 == 0){
                        upd(nw.state2, j, 0), upd(nw.state2, j+1, 0);/*注意更新轮廓线*/
                        push(nw);
                    }
                }
                else if(map[i][j] == 'S' || map[i][j] == 'T'){/*起点终点处理类似*/
                    if(pg1 == 0 && pg2 == 0){/*没有插头*/
                        if(mp1 != 1 && mp2 != 1){/*不允许四连通*/
                            upd(nw.state, j, 3);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                            upd(nw.state, j, 0), upd(nw.state, j+1, 3);
                            push(nw);
                        }
                    }
                    else if(pg1 == 0 || pg2 == 0){/*有一个插头*/
                        int pg, mp, posi;
                        if(pg1 == 0)
                            pg =pg2, mp =mp1, posi =j+1;
                        else
                            pg =pg1, mp =mp2, posi =j;
                        if(mp != 1){/*判断四连通*/
                            if(pg == 3){/*合并独立插头*/
                                upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                                upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                                nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                                push(nw);
                            }
                            else{/*连接的不是一个独立插头,要更新括号*/
                                updbracket(nw.state, posi, 3);
                                upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                                upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                                nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                                push(nw);
                            }
                        }
                    }
                    else/*有两个插头,这格也必须有两个插头。但要求这格必须放独立插头(单个插头)*/
                        continue;
                }
                else{/*为空地*/
                    if(pg1 == 0 && pg2 == 0){
                        /*新建分量*/
                        if(mp1 != 1 && mp2 != 1){/*判断四连通*/
                            qwq nw2 =nw;
                            upd(nw2.state, j, 1), upd(nw2.state, j+1, 2);
                            upd(nw2.state2, j, 1), upd(nw2.state2, j+1, 1);
                            nw2.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw2);
                        }
                        /*放置障碍*/
                        upd(nw.state2, j, 0), upd(nw.state2, j+1, 0);
                        push(nw);
                        /*放置炮台*/
                        if(nw.battery < K){
                            upd(nw.state2, j, 2), upd(nw.state2, j+1, 2);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 1);
                            nw.battery +=1;
                            push(nw);
                        }
                    }
                    else if(pg1 == 0 || pg2 == 0){/*延伸路径*/
                        int pg, mp;
                        if(pg1 == 0)
                            pg =pg2, mp =mp1;
                        else
                            pg =pg1, mp =mp2;
                        if(mp != 1){/*判断四连通*/
                            upd(nw.state, j, pg), upd(nw.state, j+1, 0);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                            upd(nw.state, j, 0), upd(nw.state, j+1, pg);
                            push(nw);
                        }
                    }
                    else{/*有两个插头*/
                        if(pg1 == 3 && pg2 == 3){/*两个独立插头*/
                            upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                        }
                        else if(pg1 == 3 || pg2 == 3){/*有一个独立插头*/
                            int posi;
                            if(pg1 == 3)
                                posi =j+1;
                            else
                                posi =j;
                            updbracket(nw.state, posi, 3);
                            upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                        }
                        else if(pg1 == pg2){/*两个相同插头,要改括号*/
                            if(pg1 == 1)
                                updbracket(nw.state, j+1, 1);
                            else
                                updbracket(nw.state, j, 2);
                            upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                        }
                        else if(pg1 == 2 && pg2 == 1){/*两个不同插头,且没形成回路*/
                            upd(nw.state, j, 0), upd(nw.state, j+1, 0);
                            upd(nw.state2, j, 1), upd(nw.state2, j+1, 1);
                            nw.sum +=count(mp1, lmp, mp2, rmp, 2);
                            push(nw);
                        }
                        else/*不允许闭合回路产生*/
                            continue;
                    }
                }
            }
            dp.swap(hsh);
            clearhsh();
        }
    }
}


int main(){
    scanf("%d%d%d", &n, &m, &K);
    if(n >= m){
        for(int i =0; i < n; ++i){
            char c =getchar();
            while(c != '#' && c != '.' && c != 'S' && c != 'T') c =getchar();
            for(int j =0; j < m; ++j){
                map[i][j] =c;
                c =getchar();
            }
        }
    }
    else{/*镜像做出来也是对的*/
        n ^=m ^=n ^=m;
        for(int j =0; j < m; ++j){
            char c =getchar();
            while(c != '#' && c != '.' && c != 'S' && c != 'T') c =getchar();
            for(int i =0; i < n; ++i){
                map[i][j] =c;
                c =getchar();
            }
        }
    }
    work();
    ll ans =0;
    for(int k =0; k < (int)dp.size(); ++k)
        if(dp[k].state == 0)
            if(dp[k].sum > ans)
                ans =dp[k].sum;
    printf("%lld", ans);
}