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 ...
随机推荐
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- Java——容器(Map)
[Map接口]
- python list 插入元素
https://www.jb51.net/article/57923.htm List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持着初始时的定义的顺序(除非你对它们进行排 ...
- python 存取字典dict
数据处理的时候主要通过两个函数(1):np.save(“test.npy”,数据结构) ----存数据(2):data =np.load('test.npy") ----取数据 1.存列表 ...
- sh_02_第2个Python程序
sh_02_第2个Python程序 print("hello")
- formdata方式上传文件,支持大文件分割上传
1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...
- HDU 1314 Numerically Speaking(大数加减乘除+另类二十六进制互相转换)
原题代号:HDU 1314 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1314 Numerically Speaking Time Limit: 2 ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- js基础补漏
1.for...in 和 for...of有何区别 for ... in循环由于历史遗留问题,它遍历的实际上是对象的属性名称.一个Array数组实际上也是一个对象,它的每个元素的索引被视为一个属性. ...
- CDQ求子矩阵的和
Description维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=20000 ...