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. websocket的通信原理

    首先什么是websocket? 1.websocket和http一样是一种通信协议,是HTML5的一种新的协议. 2.既然有了http协议了,为什么还会有websocket呢?是因为是为了弥补http ...

  2. js原生高逼格插件

    如何定义一个高逼格的原生JS插件 作为一个前端er,如果不会写一个小插件,都不好意思说自己是混前端界的.写还不能依赖jquery之类的工具库,否则装得不够高端.那么,如何才能装起来让自己看起来逼格更高 ...

  3. css雪碧图-css精灵图

    先将图片拼接在一张图上.类似实现的效果图 图片地址为合并后的图片地址,通过background-position调整背景图的位置.效果如: HTML: <div class="logo ...

  4. favicon.ico是什么?

    一.什么是favicon? 所谓favicon,便是其可以让浏览器的收藏夹中除显示相应的标题外,还以图标的方式区别不同的网站.favicon 中文名称:网页图标  英文名称:favorites ico ...

  5. oracle 11g 卸载 安装10g 成功(卸载oracle+清除注册表信息+清除oracle app文件)

    停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 2 在开始菜单中,找到Universal Installer,运行Oracle Universal Ins ...

  6. cookie、session和会话保持

    1.会话 在程序中,会话跟踪是很重要的事情.理论上,一个已登录用户,在这次登录后进行的所有请求操作都应该属于同一个会话,而另一个用户的所有请求操作则应该属于另一个会话,二者不能混淆.例如,用户 A 在 ...

  7. Nginx动静分离-tomcat

    一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...

  8. E. Natasha, Sasha and the Prefix Sums

    http://codeforces.com/contest/1204/problem/E 给定n个 1 m个 -1的全排 求所有排列的$f(a) = max(0,max_{1≤i≤l} \sum_{j ...

  9. Linux6.6及以上版本配置oracle-ASM共享储存-UDEV

    在linux6.6版本之前,我们又两种方式可以配置asm共享磁盘,一种是安装asm驱动包进行asm磁盘组配置,另一种是UDEV通过识别共享存储UUID号进行asm磁盘组配置. 但在linux6.6之后 ...

  10. 【转】SQL Pretty Printer for SSMS 很不错的SQL格式化插件

    源地址:https://www.cnblogs.com/leospace/archive/2012/09/04/SQL_Pretty_Printer_for_SSMS.html 写SQL语句或者脚本时 ...