bzoj3905: Square

Posted ccz181078

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj3905: Square相关的知识,希望对你有一定的参考价值。

Description

Nothing is more beautiful than square! So, given a grid of cells, each cell being black or white, it is reasonable to evaluate this grid’s beautifulness by the side length of its maximum continuous subsquare which fully consists of white cells.
Now you’re given an N × N grid, and the cells are all black. You can paint some cells white. But other cells are broken in the sense that they cannot be paint white. For each integer i between 0 and N inclusive, you want to find the number of different painting schemes such that the beautifulness is exactly i. Two painting schemes are considered different if and only if some cells have different colors. Painting nothing is considered to be a scheme.
技术分享
For example, N = 3 and there are 4 broken cells as shouwn in Fig. J(a). There are 2 painting schemes for i=2 as shown in Fig. J(b) and J(c).
You just need to output the answer modulo 10^9 + 7.
给你一个n * n(n <= 8)的棋盘,上面有一些格子必须是黑色,其它可以染
黑或者染白,对于一个棋盘,定义它的优美度为它上面最大的连续白色
子正方形的边长,对于每个0 <= i <= n,问有多少种染色方案使得棋盘的
优美度为i?

Input

The first line contains an integer T (T ≤ 10) denoting the number of the test cases.
For each test case, the first line contains an integer N (1 ≤ N ≤ 8), denoting the size of the grid is N × N . Then N lines follow, each line containing an N-character string of “o” and “*”, where “o” stands for a paintable cell and “*” for a broken cell.

Output

For each test case, for each integer i between 0 and N inclusive, output the answer in a single line.

类似插头dp,f[x][y][k][S]表示当前决策到第x行第y列,到目前为止最大白色正方形大小为k,轮廓线上状态为S的方案数

具体S表示以轮廓线上每个格子为右下角的最大白色正方形大小,这样若一个格子x,y决策为白色,则S(x,y)=min(S(x-1,y),S(x-1,y-1),S(x,y-1))+1,若黑色则S(x,y)=0

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
int T,n;
char s[11][11];
const int M=2939999,P=1000000007;
struct map{
    int x[M],y[M],s[M],sp;
    map(){
        memset(y,-1,sizeof y);
        sp=0;
    }
    void clear(){
        for(;sp;y[s[--sp]]=-1);
    }
    int&get(int a){
        int w=a%M;
        while(~y[w]){
            if(x[w]==a)return y[w];
            if((w+=1237)>=M)w-=M;
        }
        x[w]=a;s[sp++]=w;
        return y[w];
    }
}_m1,_m2,*m1=&_m1,*m2=&_m2;
std::queue<int>q1,q2;
void upd(int x,int y){
    int&w=m2->get(x);
    if(w==-1)w=0,q2.push(x);
    if((w+=y)>=P)w-=P;
}
void mins(int&a,int b){if(a>b)a=b;}
int ans[11];
void cal(){
    for(int i=0;i<=n;++i)ans[i]=0;
    upd(0,1);
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            std::swap(q1,q2);
            std::swap(m1,m2);
            m2->clear();
            while(!q1.empty()){
                int w=q1.front();q1.pop();
                int v=m1->get(w);
                upd(w&~(7<<j*3),v);
                if(s[i][j]==o){
                    int a=w>>j*3&7,b=w>>n*3+3&7;
                    if(j)mins(a,w>>j*3-3&7);
                    mins(a,w>>j*3+3&7);
                    ++a;
                    if(a<8)upd((w&~(7<<j*3)|(a<<j*3))+(a>b?1<<n*3+3:0),v);else ++ans[8];
                }
            }
        }
        std::swap(q1,q2);
        std::swap(m1,m2);
        m2->clear();
        while(!q1.empty()){
            int w=q1.front();q1.pop();
            int v=m1->get(w);
            if(i!=n-1)upd(w&(7<<n*3+3)|(w&(1<<n*3)-1)<<3,v);
            else (ans[w>>n*3+3]+=v)%=P;
        }
    }
    for(int i=0;i<=n;++i)printf("%d\n",ans[i]);
}
int main(){
    for(scanf("%d",&T);T;--T){
        scanf("%d",&n);
        for(int i=0;i<n;++i)scanf("%s",s[i]);
        cal();
    }
    return 0;
}

 

以上是关于bzoj3905: Square的主要内容,如果未能解决你的问题,请参考以下文章

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

bzoj1661[Usaco2006 Nov]Big Square 巨大正方形*

POJ 3905 Perfect Election(2-sat)

LA3905

uvaliva3905(扫描线)

bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序置换群