题意:有一个n*m矩阵,其中有些格子必走,有些格子不可走,其他格子是可走也可不走,问有多少条哈密顿回路?

思路:

  本来是一道很简单的题,代码写多了连白痴bug都查不出了,竟然用i>=ex&&j>=ey来判定最后一个必走点后面的点!明显是错的。

  其实主要在选走的格子,那么有两种选择,“走”or“不走”,不走的话上一个格子的状态照搬过来。这样就没有了固定的终点了,因为终点可以是很多种情况(比如是选走点/必走点),那么只要是在ex和ey后面的格子(指的是(ex,ey)之后遍历的所有非障碍格子),都是可以作为终点的,只有在这些格子才可以将左右括号连起来(只统计但不插入)。其他的基本和常规的一样了,本次仍然用括号表示法,再过滤掉很多无效的状态,所以代码略长,但容易看。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=;
const int mod=;
const int NN=;
char g[N][N];
LL ans;
int cur, n, m, ex, ey, tx, ty;
struct Hash_Map
{
int head[mod]; //桶指针
int next[NN]; //记录链的信息
LL status[NN]; //状态
LL value[NN]; //状态对应的DP值。
int size; void clear() //清除哈希表中的状态
{
memset(head, -, sizeof(head));
size = ;
} void insert(LL st, LL val) //插入状态st的值为val
{
int h = st%mod;
for(int i=head[h]; ~i; i=next[i])
if(status[i] == st) //这个状态已经存在,累加进去。
{
value[i] += val;
return ;
}
status[size]= st; //找不到状态st,则插入st。
value[size] = val;
next[size] = head[h] ; //新插入的元素在队头
head[h] = size++;
}
}hashmap[]; inline int getbit(LL s,int pos) //取出状态s的第pos个插头
{
return (s>>*pos)&;
}
inline int setbit(LL s,int pos,int bit) //将状态s的第pos个插头设置为bit
{
if(s&(<<*pos )) s^=<<(*pos);
if(s&(<<(*pos+))) s^=<<(*pos+);
return (s|(bit<<*pos));
} int Fr(LL s,int pos,int bit) //寻找状态s的第pos个插头对应的右括号。
{
int cnt=;
for(pos+=; pos<m; pos++)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if(cnt==-) return setbit(s, pos, -bit);
}
}
int Fl(LL s,int pos,int bit) //寻找状态s的第pos个插头对应的左括号。
{
int cnt=;
for(pos--; pos>=; pos--)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if( cnt==-) return setbit(s, pos, -bit);
}
} void DP(int i,int j) //非障碍空格
{
LL t;
for(int k=; k<hashmap[cur^].size; k++)
{
LL s=hashmap[cur^].status[k];
LL v=hashmap[cur^].value[k];
int R=getbit(s,j), D=getbit(s,j+);
if(g[i][j]=='X') //障碍格子
{
if( R== && D== ) hashmap[cur].insert(s, v);
continue ;
}
if(R && D) //两个括号
{
t=(setbit(s,j,)&setbit(s,j+,));
if(R==D) //同个方向的括号
{
if(R==) t=Fr(t, j, ); //要改
else t=Fl(t, j, );
hashmap[cur].insert(t, v);
}
else if( R== && D== ) //不同的连通分量
hashmap[cur].insert(t, v);
else if(t== && ( i>ex || (i==ex && j>=ey))) //注意点!
ans+=v;
}
else if(R || D) //仅1个括号
{
if(R)
{
if(i+<n && g[i+][j]!='X') hashmap[cur].insert(s, v);
if(j+<m && g[i][j+]!='x') hashmap[cur].insert(setbit(setbit(s,j,), j+, R), v);
}
else
{
if(j+<m && g[i][j+]!='X') hashmap[cur].insert(s, v);
if(i+<n && g[i+][j]!='X') hashmap[cur].insert(setbit(setbit(s,j+,), j, D), v);
}
}
else //无括号
{
if( g[i][j+]!='X' && g[i+][j]!='X' && j+<m && i+<n ) //新括号
hashmap[cur].insert( setbit(s,j,)|setbit(s,j+,), v);
if( g[i][j]=='*' ) //此点可不走
hashmap[cur].insert( s, v);
}
}
} void cal()
{
for(int i=; i<n; i++)
{
cur^=;
hashmap[cur].clear();
for(int j=; j<hashmap[cur^].size; j++) //新行,需要左移一下状态。
hashmap[cur].insert( hashmap[cur^].status[j]<<, hashmap[cur^].value[j] );
for(int j=; j<m; j++)
{
cur^=;
hashmap[cur].clear();
DP(i,j);
if(i==tx && j==ty) return ; //最后的有效点
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int t, Case=;
cin>>t;
while(t--)
{
tx=ty=ex=ey=-;
ans=cur=;
memset(g, 'X', sizeof(g));
scanf("%d%d",&n,&m);
for(int i=; i<n; i++) //输入矩阵
{
for(int j=; j<m; j++)
{
char c=getchar();
if(c=='X'||c=='*'||c=='O')
{
g[i][j]=c;
if( c=='O' ) ex=i,ey=j; //必走格
if( c!='X' ) tx=i,ty=j; //终点格
}
else j--;
}
} hashmap[cur].clear();
hashmap[cur].insert(, ); //初始状态
cal();
cout<<"Case "<<++Case<<": "<<ans<<endl;
}
return ;
}

AC代码

最新文章

  1. Linux 挂载管理(mount)
  2. Graph Cuts初步理解
  3. MySQL + Atlas --- 部署读写分离
  4. repo 版本回退
  5. 找模式串[XDU1032]
  6. iOS开发之Xcode6 之手动添加Pch预编译文件
  7. 打印print
  8. Visual Studio 2013 各版本注册码
  9. c++ 概念及学习/c++ concept&amp;learning(一)
  10. Java知识总结--JDBC&amp;XML
  11. 基于jQuery UI的tabs选项卡美化
  12. C++ 表达式语句 海伦的故事
  13. Gitolite v3安装配置指南
  14. 在block函数中规避错误信息 &quot;capturing self strongly in this block is likely to lead to a retain cycle”
  15. K:java中的序列化与反序列化
  16. [Swift]LeetCode474. 一和零 | Ones and Zeroes
  17. 与webview打交道踩过的坑
  18. docker下载镜像received unexpected Http status:500 Internal Server Error
  19. UE4 C++ 笔记
  20. mysql中CONCAT值为空的问题解决办法

热门文章

  1. libvirt监控
  2. java:calendar类及一些比较实用的utils(二)
  3. Android开发—— 传递数据
  4. ECMAScript 6 &amp;ECMAScript 5(在线手册)
  5. 3.1 HiveServer2.Beeline JDBC使用
  6. 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解
  7. HDU - 2181 哈密顿绕行世界问题 dfs图的遍历
  8. 阿里云的opensearch
  9. 浅谈C++中内存泄漏的检测
  10. 51nod1270(dp)