circuits

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 793    Accepted Submission(s): 253

Problem Description
 
 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?

 
Input
  The first line of input has an integer T, number of cases.
  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 ‘*’.
 
Output
  For each case output one lines.
  Each line is the answer % 1000000007 to the case.
 
Sample Input
2
4 4 1
**..
....
....
....
 
4 4 1
....
....
....
....
 
Sample Output
2
6
#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回路 )的更多相关文章

  1. hdu1693插头dp(多回路)

    题意:在n*m的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃全然部的树,求有多少中方法. 这题是插头dp,刚刚学习,不是非常熟悉,研究了好几天才明确插头dp的方法,他们老是讲一些什 ...

  2. 【插头dp】 hdu4285 找bug

    打模板的经验: 1.变量名取一样,换行也一样,不要宏定义 2.大小写,少写,大括号 #include<algorithm> #include<iostream> #includ ...

  3. Ural 1519 Formula 1 插头DP

    这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...

  4. HDU 4113 Construct the Great Wall(插头dp)

    好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...

  5. 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 ...

  6. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

  7. HDU 1693 Eat the Trees(插头DP)

    题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...

  8. 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 ...

  9. 【HDU】1693:Eat the Trees【插头DP】

    Eat the Trees Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. Thymeleaf静态资源引入方式及公共页面代码抽取

    静态资源引入 Thymeleaf模板引擎url问题,要用如下的方式写,确保在任何情况下都能访问到 <!-- Bootstrap core CSS --> <link href=&qu ...

  2. compile and link C/CPP programs on Mac

    ref: https://stackoverflow.com/questions/29987716/cannot-use-gsl-library-on-macos-ld-symbols-not-fou ...

  3. POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)

    题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...

  4. Deque 容器

    1.Deque (1)定义 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除, ...

  5. HDU 6287 Just h-index

    Time limit 3000 ms Memory limit 132768 kB OS Windows Source CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学) 中文题意 一个序列,每 ...

  6. Linux下" >/dev/null 2>&1 "详解

    在学习Linux的过程中,常会看到一些终端命令或者程序中有">/dev/null 2>&1 "出现,由于已经遇到了好几次了,为了理解清楚,不妨花点时间百度或者g ...

  7. Web引用中文个性字体

    最近在前端开发时,因为设计的原因,要引用一些特殊字体(otf格式),但是后来发现这些字体文件非常大,平均每个要8mb左右,严重影响了网页效率.经过一番搜索,发现了前端字体压缩工具(只支持utf-8格式 ...

  8. python开发环境准备

    python 以版本众多,包之间依赖复杂而著称,所以一个趁手的开发环境还是很有必要的. 我的建议是用Anaconda做环境隔离.包管理,PyCharm做项目开发,jupyter做笔记,ipython和 ...

  9. mongoexport导出记录到csv文件

    root@service:~# mongoexport -d prod -c employees -f _id,platform,phone --csv -o /opt/employees.csv 2 ...

  10. 二十二、正则表达式中的“r”含义

    '''r:Python中字符串前面加上 r 表示原生字符串(rawstring)不使用r,那么匹配时候需要4个反斜杠,正则需要转化一次,python解释器需要转化一次'''mm="c:\\a ...