http://poj.org/problem?id=1739

题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数。(n,m<=8)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
#define BIT(a,b) ((a)<<((b)<<1))
#define CLR(a,b) (a^=((a)&BIT(3,b)))
#define GET(a,b) (((a)>>((b)<<1))&3)
int n, m, all, lastx, lasty;
bool mp[12][12]; int find(int col, int flag, int s) {
int sum=0;
if(flag==0) {
for(int i=col; i>=0; --i) {
int k=GET(s, i);
if(k==1) --sum;
if(k==2) ++sum;
if(!sum) return i;
}
}
else {
for(int i=col; i<=m; ++i) {
int k=GET(s, i);
if(k==1) ++sum;
if(k==2) --sum;
if(!sum) return i;
}
}
return -1;
}
bool next(int s, int row, int col, bool U, bool D, bool L, bool R, int &T) {
if((col==0 && L) || (col==m-1 && R)) return 0;
if((row==0 && U) || (row==n-1 && D)) return 0;
if((D && mp[row+1][col]) || (R && mp[row][col+1])) return 0;
int l=GET(s, col), u=GET(s, col+1), d=0, r=0;
if((!l && L) || (!u && U) || (l && !L) || (u && !U)) return 0;
T=s;
CLR(T, col);
CLR(T, col+1);
if(!l && !u) { if(D && R) d=1, r=2; }
else if(l && u) {
if(l==1 && u==1) {
int pos=find(col+1, 1, s);
CLR(T, pos);
T|=BIT(1, pos);
}
else if(l==2 && u==2) {
int pos=find(col, 0, s);
CLR(T, pos);
T|=BIT(2, pos);
}
else if(l==1 && u==2) { if(row!=lastx || col!=lasty) return 0; }
}
else if(l && !u) { if(D) d=l; if(R) r=l; }
else if(!l && u) { if(D) d=u; if(R) r=u; }
T|=BIT(d, col);
T|=BIT(r, col+1); if(col==m-1) T<<=2, T&=all; //printf("t:"); print(T); puts("");
return 1;
} struct H {
static const int M=1000007;
struct E { int next, to; }e[M];
int head, cnt, hash[M];
ll sum[M];
H() { memset(hash, -1, sizeof hash); memset(sum, 0, sizeof sum); cnt=head=0; }
bool find(int x, int &pos) {
pos=x%M;
while(1) { if(hash[pos]==x) return 0; else if(hash[pos]==-1) break; ++pos; if(pos==M) pos=0; }
hash[pos]=x;
return 1;
}
void ins(int t, ll d) { int pos; if(!find(t, pos)) { sum[pos]+=d; return; } e[++cnt].next=head; head=cnt; e[cnt].to=pos; sum[pos]=d; }
void clr() { for(int i=head; i; i=e[i].next) hash[e[i].to]=-1, sum[e[i].to]=0; head=0; cnt=0; }
}T1, T2; ll bfs() {
H *q1=&T1, *q2=&T2;
q1->clr(); q1->ins(0, 1);
for(int row=0; row<n; ++row) for(int col=0; col<m; ++col) {
q2->clr();
for(int i=q1->head; i; i=q1->e[i].next) {
ll sum=q1->sum[q1->e[i].to];
int s=q1->hash[q1->e[i].to], t;
if(mp[row][col]) {
if(next(s, row, col, 0, 0, 0, 0, t)) q2->ins(t, sum);
}
else {
if(next(s, row, col, 1, 1, 0, 0, t)) q2->ins(t, sum);
if(next(s, row, col, 1, 0, 1, 0, t)) q2->ins(t, sum);
if(next(s, row, col, 1, 0, 0, 1, t)) q2->ins(t, sum);
if(next(s, row, col, 0, 1, 1, 0, t)) q2->ins(t, sum);
if(next(s, row, col, 0, 1, 0, 1, t)) q2->ins(t, sum);
if(next(s, row, col, 0, 0, 1, 1, t)) q2->ins(t, sum);
}
}
swap(q1, q2);
}
ll ans=0;
for(int i=q1->head; i; i=q1->e[i].next) ans+=q1->sum[q1->e[i].to];
return ans;
} int main() {
char s[20];
while(scanf("%d%d", &n, &m), n|m) {
for(int i=2; i<n+2; ++i) {
scanf("%s", s+2);
for(int j=2; j<m+2; ++j) if(s[j]=='#') mp[i][j]=1; else mp[i][j]=0;
}
n+=2; m+=4;
for(int i=0; i<n; ++i) mp[i][1]=mp[i][m-2]=1;
for(int i=0; i<m; ++i) mp[1][i]=1;
for(int i=0; i<m; ++i) mp[0][i]=0;
for(int i=0; i<n; ++i) mp[i][0]=mp[i][m-1]=0;
mp[n-1][1]=mp[n-1][m-2]=0;
lastx=n-1; lasty=m-1;
all=BIT(1, m+1)-1;
printf("%lld\n", bfs());
}
return 0;
}

  


模板题...

将图用墙先封起来,然后开一条小路从左下角旋转到右下角,然后就是裸的求简单回路的问题辣...正确性很显然吧...

【POJ】1739 Tony's Tour的更多相关文章

  1. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  4. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  5. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  6. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  7. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. Oracle数据库锁表及解锁进程

    下午代码迁移,更新数据库记录时for update语句无法执行,数据库被锁,KILL掉几个进程搞定. Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实 ...

  2. 【转载】 Python动态生成变量

    用Python循环创建多个变量, 如创建 a1=   .a2=   .a3=   .a4=   .a5=    或  self.a1=    .self.a2=   . self.a3= 一. 可以通 ...

  3. 【JAVA集合框架之工具类】

    一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...

  4. ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)

    1061: 从三个数中找出最大的数 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 124[Submit][Status][We ...

  5. 扩展LV

    LVM最大的特性就是可以弹性调整磁盘容量下面扩展一个已经存在的LV [root@ol6--rac1 mnt]# lvdisplay --- Logical volume --- LV Path /de ...

  6. JVM字节码之整型入栈指令(iconst、bipush、sipush、ldc)

    官网:http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html 原文地址:http://www.linmuxi.com/2016/02 ...

  7. Poisson Image Editing

    说起泊松,可以顺便提及一下泊松同学的老师,拉普拉斯.学图像或是信号的,一定对拉普拉斯算子和拉普拉斯卷积很熟悉.在泊松图像融合出现之前,也有一种叫Laplacian pyramid blending的融 ...

  8. the last lecture

    2008.07.25,CMU教授Randy Pausch教授因癌症去世,仅47岁. 几年之前,当我看到Pausch先生最后一课的视频时,让我震撼. 转眼之间,7年过去了,这7年,让我成长了许多. 7年 ...

  9. C# 获取wave文件信息【转】

    public class WaveHelper { /// <summary> /// 数据流 /// </summary> private Stream m_WaveData ...

  10. 使用python做科学计算

    这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...