bzoj3905: Square
Description
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.
黑或者染白,对于一个棋盘,定义它的优美度为它上面最大的连续白色
子正方形的边长,对于每个0 <= i <= n,问有多少种染色方案使得棋盘的
优美度为i?
Input
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
类似插头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 ;
}
bzoj3905: Square的更多相关文章
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- OPEN CASCADE Gauss Least Square
OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...
- OpenCascade Eigenvalues and Eigenvectors of Square Matrix
OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...
- Leetcode: Matchsticks to Square && Grammar: reverse an primative array
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- Modified Least Square Method and Ransan Method to Fit Circle from Data
In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...
随机推荐
- JMS生产者+单线程发送-我们到底能走多远系列(29)
我们到底能走多远系列(29) 扯淡: “然后我俩各自一端/望着大河弯弯/终于敢放胆/嘻皮笑脸/面对/人生的难” --- <山丘> “迎着风/迎向远方的天空/路上也有艰难/也有那解 ...
- iOS学习笔记---c语言第八天
指针 首先将变量a的地址存放在另一个变量中,比如存放在变量b中,然后通过变量b来间接引用变量a,间接读写变量a的值.用来存放变量地址的变量,就称为"指针变量" int *p=nul ...
- 第一部分 python基础
1.1,参数传递:*代表元组,**代表字典 1.2 ,常用数据类型 列表 [1,2,3] 元组 (1,2,3) 不可变的list 集合 {1,2,3} 字典 {1:a, 2:c} (4)字典以关键字为 ...
- ZSDR101-跑成品MRP
*&---------------------------------------------------------------------**& Report ZSDR101*&a ...
- 15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) J dp 背包
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- Codeforces Round #260 (Div. 2) A B C 水 找规律(大数对小数取模) dp
A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- JS开发者常用的10个Sublime Text插件
Sublime Text 是每个开发者工具箱中都应该有的一个强大的应用.它是一个跨平台的.高定制化的.高级的文本编辑器,在功能强大的 集成开发环境(众所周知地消耗资源)和类似于 Vim 或 Emacs ...
- 第二篇T语言代码编写技巧
控件事件 控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件 的一些简单而可见的功能. 概述 1.控件应用 使用现成的控件来开发应用程序时,控件工作在 ...
- MongoDB副本集搭建及备份恢复
一.MongoDB副本集(repl set)介绍 早起版本使用master-slave,一主一从和MySQL类似,但slave在此架构中为只读,当主库宕机后,从库不能自动切换为主: 目前已经淘汰了ma ...