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 ...
随机推荐
- man da'te
DATE(1) 用户命令 DATE(1) 名称 日期-打印或设置系统日期和时间 简介 date [OPTION]... ...
- 【GDOI2016模拟3.10】习用之语
前言 这道题看上去很水,结果我在比赛上浪费了N多时间在上面,但还是没AC.比赛结束后发现:实际上这道题还是是大水. 题目 分析 设字符串c1c2c3c4,其中c1.c2.c3.c4={'0'~'9', ...
- __new__与__init__的区别
__new__ : 控制对象的实例化过程 , 在__init__方法之前调用 __init__ : 对象实例化对象进行属性设置 class User: def __new__(cls, *args, ...
- php 客户端调用elasticsearch接口
1.php调用elasticsearch接口[参考资料:https://www.cnblogs.com/php0916/articles/6587340.html] /data/www/syhuo.n ...
- height设置百分比的条件
很多时候我们在给height设置百分比的时候不起作用, 这时候就要来谈谈什么情况下才起作用了 1)所有父级元素必须有高度: 2)必须是块级元素,行内元素不起作用: 3)ie9 以下 使用 positi ...
- 基于python实现自动化办公学习笔记一
1.CSV(1)写csv文件 import csv def writecsv(path,data): with open(path, "w") as f: writer = csv ...
- Spring_Boot 简单例子
第一步创建项目: 创建项目地址:https://start.spring.io/ 接下来就下载到本地了 跟着加压 接着用idea打开:等待资源下载完成 我写了个简单的:增删改查 项目结构: dao层: ...
- ListView 九宫格布局实现
1.效果图 2.数据 SettingData.json { "data": [{ "icon":"setting", "title ...
- tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
在执行自动化打包的时候报错,检查发现是Xcode的路径被改了 标记3的地方原来默认是没有内容的,点击它,然后会自动弹出一个选项,就是xcode的版本. 修改后,在命令行输入xcodebuild命令测试 ...
- python实现格式化输出9*9乘法表
# python 9*9 乘法表 for i in range(1,10): for j in range(1,i+1): print("%s*%s=%s"%(i,j,i*j),e ...