URAL 1519 基础插头DP
题目大意:
给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条
基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_51cea4040100gmky.html
这个博客写的更容易理解,不过好像这篇博客里的代码有问题,反正是A不了题目的
不过上面的图和思路写的很清楚,可以作为参考
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- using namespace std;
- #define ll unsigned long long
- const int HASH_SIZE = ;
- bool mp[][];
- char str[][];
- int n, m , k , nn , mm;
- int tot[] , bit[] , hash[HASH_SIZE] , state[][HASH_SIZE];
- ll dp[][HASH_SIZE] , ans;
- void init()
- {
- //每一位用4进制保存,那么对于每一个状态来说都是右移2次,所以这里i*2
- for(int i= ; i<= ; i++) bit[i] = i<<;
- memset(dp , , sizeof(dp));
- tot[] = dp[][] = , ans = k = ;
- state[][] = ;
- }
- void hash_in(int s , ll sum)//s表示当前状态,sum是指s状态下共有sum种方式形成
- {
- // print(s);
- // cout<<" "<<sum<<endl;
- int p = s%HASH_SIZE;
- while(hash[p]){
- if(state[k][hash[p]] == s){
- dp[k][hash[p]] += sum;
- return ;
- }
- p++;
- if(p == HASH_SIZE) p=;
- }
- hash[p] = ++tot[k];
- state[k][hash[p]] = s;
- dp[k][hash[p]] = sum;
- }
- void work()
- {
- for(int i= ; i<=n ; i++){
- for(int j= ; j<=m ; j++){
- k^= ; //滚动数组
- tot[k] = ;
- memset(hash , , sizeof(hash));
- for(int u= ; u<=tot[-k] ; u++){
- int s = state[-k][u] , curs;
- ll sum = dp[-k][u];
- int p = (s>>bit[j-])& , q = (s>>bit[j])&; //3进制 , 用( , # , )来理解
- if(!mp[i][j]){
- if(p== && q==) hash_in(s , sum);
- }
- else{
- if(p == && q == ){
- if(!mp[i+][j] || !mp[i][j+]) continue;
- curs = s + (<<bit[j-]) + (<<bit[j]);
- hash_in(curs , sum);
- }
- else if(!p && q){
- if(mp[i][j+]) hash_in(s , sum);
- if(mp[i+][j]){
- curs = s+q*(<<bit[j-])-q*(<<bit[j]);
- hash_in(curs , sum);
- }
- }
- else if(p && !q){
- if(mp[i+][j]) hash_in(s , sum);
- if(mp[i][j+]){
- curs = s - p*(<<bit[j-])+p*(<<bit[j]);
- hash_in(curs , sum);
- }
- }
- else if(p+q==){ //可理解为p==1 && q==1
- int cnt = ;
- for(int v=j+ ; v<=m ; v++){
- int w = (s>>bit[v])&;
- if(w == ) cnt++;
- if(w == ) cnt--;
- if(!cnt){
- curs = s-(<<bit[v]); //将)转化为( ->状态由2->1 , 减少了一个
- break;
- }
- }
- curs = curs-(<<bit[j])-(<<bit[j-]);
- hash_in(curs , sum);
- }
- else if(p+q==){//可理解为p==2 && q==2
- int cnt = ;
- for(int v=j- ; v>= ; v--){
- int w = (s>>bit[v])&;
- if(w == ) cnt--;
- if(w == ) cnt++;
- if(!cnt){
- curs= s+(<<bit[v]);
- break;
- }
- }
- curs = curs-(<<bit[j])-(<<bit[j-]);
- hash_in(curs , sum);
- }
- else if(p== && q==){
- if(i==nn && j==mm)
- ans+=sum;
- }
- else if(p== && q==){
- curs = s-(<<bit[j-])-(<<bit[j]);
- hash_in(curs , sum);
- }
- }
- }
- }
- for(int j= ; j<=tot[k] ; j++) state[k][j]<<=;
- }
- printf("%lld\n" , ans);
- }
- int main()
- {
- // freopen("in.txt" , "r" , stdin);
- while(~scanf("%d%d" , &n , &m))
- {
- memset(mp , , sizeof(mp));
- for(int i= ; i<=n ; i++){
- scanf("%s" , str[i]+);
- for(int j= ; j<=m ; j++){
- mp[i][j] = str[i][j]=='.';
- // if(mp[i][j]) cout<<i<<" "<<j<<endl;
- if(mp[i][j]) nn=i , mm=j;//记录最后一个可执行块
- }
- }
- init();
- work();
- }
- return ;
- }
后来写hdu上的题目的时候觉得kuangbin的表达方式还是很清晰的,所以又将源代码修改成了:
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- #define ll unsigned long long
- const int HASH = ;
- const int STATE = ;
- const int MAXD = ;
- int n , m , enx , eny;
- int code[MAXD] , mp[MAXD][MAXD];
- ll ans = ;
- struct HASHMAP{
- int head[HASH] , next[STATE] , state[STATE] , size;
- ll f[STATE];
- void init(){
- size = ;
- memset(head , - , sizeof(head));
- }
- void push_in(int st , ll sum){
- int h = st%HASH;
- for(int i = head[h] ; ~i ; i=next[i]){
- if(st == state[i]){
- f[i]+=sum;
- return ;
- }
- }
- f[size]=sum;
- state[size] = st;
- next[size] = head[h];
- head[h] = size++;
- }
- }hashmap[];
- void decode(int *code , int m , int st)
- {
- for(int i=m ; i>= ; i--){
- code[i] = st&;
- st>>=;
- }
- }
- int encode(int *code , int m)
- {
- int st=;
- for(int i= ; i<=m ; i++){
- st<<=;
- st |= code[i];
- }
- return st;
- }
- void init()
- {
- char str[MAXD][MAXD];
- for(int i= ; i<=n ; i++){
- scanf("%s" , str[i]+);
- for(int j= ; j<=m ; j++){
- if(str[i][j] == '.'){
- mp[i][j] = ;
- enx = i , eny = j;
- }else mp[i][j] = ;
- }
- mp[i][m+]=;
- mp[n+][i] = ;
- }
- }
- 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) //处理可执行格子
- {
- // cout<<"ok: "<<i<<" "<<j<<endl;
- int k , left , up;
- for(k= ; k<hashmap[cur].size ; k++){
- decode(code , m , hashmap[cur].state[k]);
- left = code[j-];
- up = code[j];
- if(!left && !up){
- if(!mp[i][j+] || !mp[i+][j]) continue;
- code[j-] = , code[j] = ;
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- else if(!left && up){
- if(mp[i][j+]) hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- if(mp[i+][j]){
- code[j-] = up , code[j] = ;
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- }
- else if(left && !up){
- if(mp[i+][j]){
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- if(mp[i][j+]){
- code[j-] = , code[j] = left;
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- }
- else if(left== && up == ){
- int cnt = ;
- for(int v=j+ ; v<=m ; v++){
- if(code[v]==)cnt++;
- if(code[v]==)cnt--;
- if(!cnt){
- code[v]=;
- break;
- }
- }
- code[j-] = code[j] = ;
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- else if(left == && up == ){
- int cnt=;
- for(int v=j- ; v>= ; v--){
- if(code[v]==)cnt++;
- if(code[v]==)cnt--;
- if(!cnt){
- code[v]=;
- break;
- }
- }
- code[j-] = code[j] = ;
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- else if(left== && up==){
- if(i==enx && j==eny) ans+=hashmap[cur].f[k];
- }
- else{
- code[j-]=code[j]=;
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- }
- }
- void dpblock(int i , int j , int cur)
- {
- // cout<<"flase: "<<i<<" "<<j<<endl;
- int k , left , up;
- for(k= ; k<hashmap[cur].size ; k++){
- decode(code , m , hashmap[cur].state[k]);
- left = code[j-] , up = code[j];
- if(!left && !up){
- if(j == m) shift(code , m);
- hashmap[cur^].push_in(encode(code , m) , hashmap[cur].f[k]);
- }
- }
- }
- ll solve()
- {
- ans = ;
- int cur = ;
- hashmap[cur].init();
- hashmap[cur].push_in( , );
- for(int i= ; i<=n ; i++){
- for(int j= ; j<=m ; j++){
- hashmap[cur^].init();
- if(mp[i][j]) dpblank(i , j , cur);
- else dpblock(i , j , cur);
- cur ^= ;
- }
- }
- return ans;
- }
- int main()
- {
- // freopen("in.txt" , "r" , stdin);
- while(~scanf("%d%d" , &n , &m))
- {
- init();
- printf("%I64d\n" , solve());
- }
- return ;
- }
URAL 1519 基础插头DP的更多相关文章
- URAL Formula 1 ——插头DP
[题目分析] 一直听说这是插头DP入门题目. 难到爆炸. 写了2h,各种大常数,ural垫底. [代码] #include <cstdio> #include <cstring> ...
- Ural 1519 Formula 1 (DP)
题意:给定一个 n * m 的矩阵,问你能花出多少条回路. #pragma comment(linker, "/STACK:1024000000,1024000000") #inc ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- Ural 1519. Formula 1 优美的插头DP
今天早上学了插头DP的思想和最基础的应用,中午就开始敲了,岐哥说第一次写不要看别人代码,利用自己的理解一点点得写出来,这样才锻炼代码能力!于是下午慢慢地构思轮廓,一点点地敲出主体代码,其实是很磨蹭的, ...
- bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 942 Solved: 356[Submit][Sta ...
- 【Ural】1519. Formula 1 插头DP
[题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...
随机推荐
- mustache.js渲染带事件的模板
http://zccst.iteye.com/blog/2183111 最近遇到这样一个问题,预览内容可点击,问题是通过$.Mustache.render("templateId" ...
- D3.js 完整的柱形图
一个完整的柱形图包含三部分:矩形.文字.坐标轴.制作一个实用的柱形图,内容包括:选择集.数据绑定.比例尺.坐标轴等内容. 1. 添加 SVG 画布 //画布大小 var width = 400; va ...
- android中textview设置为多行文本时,如何让文字从最顶开始显示
<span style="white-space:pre"> </span><EditText android:layout_width=" ...
- iOS开发 判断扫码是否为有效网址
- (BOOL)achiveStringWithWeb:(NSString *)infor { NSString *emailRegex = @"[a-zA-z]+://.*"; ...
- POJ 1218
题目描述看着就乐了,死板得只按着题意来写了ps: tequi是escape的方言版.. #include <iostream> using namespace std; int main( ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- Python顺序集合之 tuple
慕课网<Python 入门>学习笔记 1.tuple特性 tuple是另一种有序的列表,中文翻译为“ 元组 ”.tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改 ...
- ubuntu安装SCrapy
依次安装 sudo apt-get install build-essential; sudo apt-get install python-dev; sudo apt-get install lib ...
- 经典排序算法---冒泡排序(Bubble Sort)
原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位,然后再从头开始进行两两比较交换,直到倒数第二位时结束 void Bubble ...
- Sudoku Solver [LeetCode]
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...