[插头DP] BZOJ 2331: [SCOI2011]地板

Solution

好菜啊。。第一次写插头DP。。。
这个 L 型砖块具有特征的就是在之前DP的状态有没有拐过弯。
0表示没有插头, 1 表示未拐弯的插头,2表示已拐弯的插头。
大力分类讨论。
这里写图片描述
然后这种题打的时候专注一点吧。。
刚开始暴力map,T掉了。。还是得用Hash表。

#include <bits/stdc++.h>
using namespace std;

const int P = 233333;
const int N = 12;
const int M = 111;
const int MOD = 20110520;
typedef pair<int, int> Pairs;
typedef map<int, int>::iterator mit;

int r, c;
char mp[N][M];
struct Map {
  int head[P], vis[P];
    int pp[505050];
    int pnt;
  struct edge {
    int ori, key, next;
    edge(int o = 0, int k = 0, int n = 0): ori(o), key(k), next(n) {}
  };
  edge G[505050];
  int Gcnt, clc;
  Map(void) {
    clc = 1;
    for (int i = 0; i < P; i++) vis[i] = 0;
  }
  inline void Clear(void) {
    Gcnt = pnt = 0; clc++;
  }
  inline int Head(int x) {
    if (vis[x] == clc) return head[x];
    else {
      vis[x] = clc; return head[x] = 0;
    }
  }
  inline void AddEdge(int from, int ori, int key) {
    G[++Gcnt] = edge(ori, key, head[from]); head[from] = Gcnt;
  }
  inline int &operator [](int x) {
    int h = x % P;
    for (int i = Head(h); i; i = G[i].next)
      if (G[i].ori == x) return G[i].key;
        pp[++pnt] = x;
    AddEdge(h, x, 0); return G[Gcnt].key;
  }
};
Map f[N];
int p[505050];

inline Map &dp(int x, int y) {
    return f[y];
}
inline void Add(int &x, int a) {
    x = (x + a) % MOD;
}
inline void Update(int x, int y, int nS, int t) {
    if (y > c) {
        nS ^= (nS >> c >> c) << c << c;
        ++x; y = 1;
    }
    Add(dp(x, y)[nS], t);
}
inline void Clear(int x, int y) {
    if (y > c) {
        ++x; y = 1;
    }
    dp(x, y).Clear();
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    scanf("%d %d", &r, &c);
    for (int i = 1; i <= r; i++)
        scanf("%s", mp[i] + 1);
    if (r < c) {
        for (int i = 1; i <= c; i++)
            for (int j = i + 1; j <= c; j++)
                swap(mp[i][j], mp[j][i]);
        swap(r, c);
    }
    dp(1, 1)[0] = 1;
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++) {
            Clear(i, j + 1);
            for (int k = dp(i, j).pnt; k; k--) p[k] = dp(i, j).pp[k];
            for (int k = dp(i, j).pnt; k; k--) {
                int S = p[k], t = dp(i, j)[S];
                int x = S >> c >> c & 3, y = S & 3;
                int nS;
                if (mp[i][j] == '*') {
                    if (x || y) continue;
                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);
                    continue;
                }
                if (x == 0 && y == 0) {
                    if (j != c) {
                        nS = (S ^ (x << c << c)) >> 2;
                        nS = nS ^ (2 << (c - 1) << (c - 1)) ^ (2 << c << c);
                        Update(i, j + 1, nS, t);
                    }

                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (1 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);

                    if (j != c) {
                        nS = (S ^ (x << c << c)) >> 2;
                        nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (1 << c << c);
                        Update(i, j + 1, nS, t);
                    }
                }
                if (x == 1 && y == 0) {
                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (2 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);

                    if (j != c) {
                        nS = (S ^ (x << c << c)) >> 2;
                        nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (1 << c << c);
                        Update(i, j + 1, nS, t);
                    }
                }
                if (x == 0 && y == 1) {
                    if (j != c) {
                        nS = (S ^ (x << c << c)) >> 2;
                        nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (2 << c << c);
                        Update(i, j + 1, nS, t);
                    }

                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (1 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);
                }
                if (x == 2 && y == 0) {
                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);

                    if (j != c) {
                        nS = (S ^ (x << c << c)) >> 2;
                        nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (2 << c << c);
                        Update(i, j + 1, nS, t);
                    }
                }
                if (x == 0 && y == 2) {
                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);

                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (2 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);
                }
                if (x == 1 && y == 1) {
                    nS = (S ^ (x << c << c)) >> 2;
                    nS = nS ^ (0 << (c - 1) << (c - 1)) ^ (0 << c << c);
                    Update(i, j + 1, nS, t);
                }
            }
        }
    printf("%d\n", dp(r + 1, 1)[0]);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值