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.
bzoj3905: Square
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[][];
const int M=,P=;
struct map{
int x[M],y[M],s[M],sp;
map(){
memset(y,-,sizeof y);
sp=;
}
void clear(){
for(;sp;y[s[--sp]]=-);
}
int&get(int a){
int w=a%M;
while(~y[w]){
if(x[w]==a)return y[w];
if((w+=)>=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==-)w=,q2.push(x);
if((w+=y)>=P)w-=P;
}
void mins(int&a,int b){if(a>b)a=b;}
int ans[];
void cal(){
for(int i=;i<=n;++i)ans[i]=;
upd(,);
for(int i=;i<n;++i){
for(int j=;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&~(<<j*),v);
if(s[i][j]=='o'){
int a=w>>j*&,b=w>>n*+&;
if(j)mins(a,w>>j*-&);
mins(a,w>>j*+&);
++a;
if(a<)upd((w&~(<<j*)|(a<<j*))+(a>b?<<n*+:),v);else ++ans[];
}
}
}
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-)upd(w&(<<n*+)|(w&(<<n*)-)<<,v);
else (ans[w>>n*+]+=v)%=P;
}
}
for(int i=;i<=n;++i)printf("%d\n",ans[i]);
}
int main(){
for(scanf("%d",&T);T;--T){
scanf("%d",&n);
for(int i=;i<n;++i)scanf("%s",s[i]);
cal();
}
return ;
}
上一篇:JSP常用Form表单控件


下一篇:Bootstrap页面布局10 - BS代码