HDU 4285 circuits( 插头dp , k回路 )
circuits
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 793 Accepted Submission(s): 253
Given a map of N * M (2 <= N, M <= 12) , '.' means empty, '*'
means walls. You need to build K circuits and no circuits could be
nested in another. A circuit is a route connecting adjacent cells in a
cell sequence, and also connect the first cell and the last cell. Each
cell should be exactly in one circuit. How many ways do we have?
For each case:
The first line has three integers N M K, as described above.
Then the following N lines each has M characters, ‘.’ or ‘*’.
Each line is the answer % 1000000007 to the case.
#include <bits/stdc++.h>
using namespace std ;
const int N = ;
const int M = ;
const int MAXN = ;
const int mod = 1e9+;
int n , m , K ;
int maze[N][N] ;
int code[N] ;
int ch[N] , num ;
int ex , ey ; struct HASHMAP {
int head[M] , next[MAXN] , tot ;
long long st[MAXN] , f[MAXN] ;
void init() {
memset( head , - , sizeof head ) ;
tot = ;
}
void push( long long state , long long ans ) {
int u = state % M ;
for( int i = head[u] ; ~i ; i = next[i] ) {
if( st[i] == state ) {
f[i] += ans ;
f[i] %= mod ;
return ;
}
}
st[tot] = state ;
f[tot] = ans % mod ;
next[tot] = head[u] ;
head[u] = tot++ ;
}
} mp[] ; void decode ( int* code , int m , long long st ) {
num = st & ;
st >>= ;
for( int i = m ; i >= ; --i ) {
code[i] = st& ;
st >>= ;
}
} long long encode( int *code , int m ) {
int cnt = ;
long long st = ;
memset( ch , - , sizeof ch) ;
ch[] = ;
for( int i = ; i <= m ; ++i ) {
if( ch[code[i]] == - ) ch[ code[i] ] = cnt++ ;
code[i] = ch[ code[i] ] ;
st <<= ;
st |= code[i] ;
}
st <<= ;
st |= num ;
return st ;
} void shift( int *code , int m ) {
for( int i = m ; i > ; --i ) {
code[i] = code[i-] ;
} code[] = ;
} void dpblank( int i , int j , int cur ) {
int left , up ;
for( int k = ; k < mp[cur].tot ; ++k ) {
decode( code , m , mp[cur].st[k] );
left = code[j-] ;
up = code[j] ;
if( left && up ) {
if( left == up ) {
if( num >= K ) continue ;
int c = ;
for( int y = ; y < j - ; ++y )
if( code[y] ) c++ ;
if( c& ) continue ;
num++ ;
code[j-] = code[j] = ;
if( j == m ) shift( code , m ) ;
mp[cur^].push( encode(code,m),mp[cur].f[k] );
}else {
code[j-] = code[j] = ;
for( int t = ; t <= m ; ++t ) {
if( code[t] == up )
code[t] = left ;
}
if( j == m ) shift( code,m );
mp[cur^].push(encode(code,m),mp[cur].f[k]) ;
}
}
else if( ( left && ( !up ) ) || ( up && (!left ) ) ) {
int t ;
if( left ) t = left ;
else t = up ;
if( maze[i][j+] ) {
code[j-] = ;
code[j] = t ;
mp[cur^].push( encode(code,m) , mp[cur].f[k] ) ;
}
if( maze[i+][j] ) {
code[j-] = t ;
code[j] = ;
if( j == m ) shift( code , m );
mp[cur^].push(encode(code,m),mp[cur].f[k]); }
}
else {
if( maze[i][j+] && maze[i+][j] ) {
code[j-] = code[j] = ;
mp[cur^].push( encode(code,m),mp[cur].f[k]);
}
}
}
}
void dpblock( int i , int j , int cur ) {
for( int k = ; k < mp[cur].tot ; ++k ) {
decode( code , m , mp[cur].st[k] );
code[j-] = code[j] = ;
if( j == m ) shift( code , m );
mp[cur^].push( encode(code,m) , mp[cur].f[k] );
}
} void Solve() {
int v = ;
mp[v].init();
mp[v].push(,);
for( int i = ; i <= n ; ++i ) {
for( int j = ; j <= m ; ++j ) {
mp[v^].init() ;
if( maze[i][j] ) dpblank( i , j , v ) ;
else dpblock( i , j , v );
v ^= ;
}
}
long long ans = ;
for( int i = ; i < mp[v].tot ; ++i ) {
if( mp[v].st[i] == K ) ans = ( ans + mp[v].f[i] ) % mod ;
}
cout << ans << endl ;
}
string s ; int main () {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio();
int _ ; cin >> _ ;
while( _-- ) {
cin >> n >> m >> K ;
ex = ;
memset( maze , , sizeof maze ) ;
for( int i = ; i <= n ; ++i ) {
cin >> s ;
for( int j = ; j < m ; ++j ) {
if( s[j] == '.' ) {
ex = i , ey = j + ;
maze[i][j+] = ;
}
}
}
if( !ex ) { cout << '' << endl ; continue ; }
else Solve();
}
return ;
}
HDU 4285 circuits( 插头dp , k回路 )的更多相关文章
- hdu1693插头dp(多回路)
题意:在n*m的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃全然部的树,求有多少中方法. 这题是插头dp,刚刚学习,不是非常熟悉,研究了好几天才明确插头dp的方法,他们老是讲一些什 ...
- 【插头dp】 hdu4285 找bug
打模板的经验: 1.变量名取一样,换行也一样,不要宏定义 2.大小写,少写,大括号 #include<algorithm> #include<iostream> #includ ...
- Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...
- HDU 4113 Construct the Great Wall(插头dp)
好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...
- HDU 1693 Eat the Trees(插头DP,入门题)
Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- 【HDU】1693:Eat the Trees【插头DP】
Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- CF1242B. 0-1 MST
题目大意 有一个n个点的完全图,上面有m条边的权值为1,其余为0 求MST n,m<=10^5 题解 方法一: 维护一个点集,表示当前MST中的点 一开始任意加一个点 对于一个未加入的点,如果和 ...
- 【bzoj3564】 [SHOI2014]信号增幅仪
题目描述: 无线网络基站在理想状况下有效信号覆盖范围是个圆形.而无线基站的功耗与圆的半径的平方成正比. 现给出平面上若干网络用户的位置,请你选择一个合适的位置建设无线基站.... 就在你拿起键盘准备开 ...
- 【CF1249D】Too Many Segments(贪心,set,vector)
题意:给定n条线段和覆盖上限k,每条线段都覆盖了区间内的整点 问最少删掉几条线段能使所有的整点都被覆盖不超过k次 k<=n<=2e5,l[i],r[i]<=2e5 思路:比赛时候不会 ...
- 170907-关于JavaWeb的题
1. 答案是B.D Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终 ...
- TestingWhiz社区版2013版下载地址
TestingWhiz社区版 https://sourceforge.net/projects/testingwhiz-community-edition/ https://sourceforge.n ...
- 最新版本的MySQL的下载和安装(Release: 8.0.12)
1.打开百度搜索[Myql],或直达官网https://dev.mysql.com/ 2.点选[Download按钮],跳转到下载页面,拉到底部再点选[Community Download]社区版[免 ...
- P1439 【模板】最长公共子序列(LCS)
先来看一看普通的最长公共子序列 给定字符串A和B,求他们的最长公共子序列 DP做法: 设f[i][j]表示A[1~i]和B[1~j]的最长公共子序列的长度 那么f[i][j]=max(f[i-1][j ...
- apache配置补充
apache的安装: 分成三种方式: tar包 rpm安装 yum安装. ============ tar包安装 ======================== 下载.tar.gz的安装包 解压和安 ...
- Collector 源码分析
Collectors Collectors 配合 stream 可以实现 MapReduce 操作,也可以单独完成流中元素的收集. 收集器接口和实现 /** * 收集器接口 */ public int ...
- 架构-数据库访问-SQL语言进行连接数据库服务器-DAO:DAO
ylbtech-架构-数据库访问-SQL语言进行连接数据库服务器-DAO:DAO DAO(Data Access Object) 数据访问对象是一个面向对象的数据库接口,它显露了 Microsoft ...