Description

A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked. The Farm is located in the lower left plot and the Market is located in the lower right plot. Tony takes her tour of the township going from Farm to Market by walking through every unblocked plot exactly once. 
Write a program that will count how many unique tours Betsy can take in going from Farm to Market. 

Input

The input contains several test cases. The first line of each test case contain two integer numbers n,m, denoting the number of rows and columns of the farm. The following n lines each contains m characters, describe the farm. A '#' means a blocked square, a '.' means a unblocked square. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

Sample Input

2 2
..
..
2 3
#..
...
3 4
....
....
....
0 0

Sample Output

1
1
4 楼教主八题之一,同样可以加行,也可以特殊处理,我写的是加行。
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mn=;
int i;
struct na{
int x,z;
na(int xx,int zz):x(xx),z(zz){}
};
int n,m,x,y,z,a[],k,en,p1,p2;
char u[];
bool map[][];
long long f[][mn+],ans;
int v[][mn+];
int re[][];
queue <na> q;
inline int gx(int x,int q1,int q2){k=;for (register int i=m+;i;i--) k=k*+(i==x?q1:(i==x+?q2:a[i]));return k;}
inline void up(int x,int z,long long lj){
x++;
k=x%;
if (v[k][z]!=x) v[k][z]=x,f[k][z]=,q.push(na(x,z));
f[k][z]+=lj;
}
int main(){
register int i,j;
while(scanf("%d%d",&n,&m)!=EOF){
if (n==&&m==) break;
ans=;
memset(map,,sizeof(map));memset(v,,sizeof(v));memset(re,,sizeof(re));memset(f,,sizeof(f));
for (i=;i<=n;i++){
scanf("%s",&u);
for (j=;j<=m;j++)
map[j][n-i+]=u[j-]=='.';
}
n+=;
en=n*m-;
map[][]=;map[m][]=;
for (i=;i<=m;i++) map[i][]=;
while(!map[en%m+][en/m+]) en--;
f[][]=v[][]=;
q.push(na(,));
while(!q.empty()){
na no=q.front();q.pop();
long long an=f[no.x%][no.z];
if(no.x%m==) no.z*=;
x=no.x%m+;y=no.x/m+;
for (i=;i<=m+;i++) a[i]=;
for (i=,j=no.z;j;i++,j/=) a[i]=j%;
if (!map[x][y])up(no.x,gx(x,,),an);else
if (a[x]==&&a[x+]==){
if (no.x==en) ans+=an;
}else if (a[x]==&&a[x+]==) up(no.x,gx(x,,),an);else
if (a[x]==&&a[x+]==){
if (map[x][y+]&&map[x+][y]) up(no.x,gx(x,,),an);
}else if (a[x]==){
if (map[x+][y]) up(no.x,gx(x,,a[x+]),an);
if (map[x][y+]) up(no.x,gx(x,a[x+],),an);
}else if (a[x+]==){
if (map[x+][y]) up(no.x,gx(x,,a[x]),an);
if (map[x][y+]) up(no.x,gx(x,a[x],),an);
}else if (a[x]==a[x+]){
p1=p2=;
if (a[x]==)
for (j=,i=x+;i<=m;i++){
if (a[i]==) j--;
if (a[i]==) j++;
if (j>&&!p1) p1=i,j--;
if (j>&&p1){p2=i;break;}
}else
for (j=,i=x-;i;i--){
if (a[i]==) j++;
if (a[i]==) j--;
if (j>&&!p2) p2=i,j--;
if (j>&&p2){p1=i;break;}
}
a[p1]=;a[p2]=;up(no.x,gx(x,,),an);
}
}
printf("%I64d\n",ans);
}
}
												

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

  1. 【POJ】【1739】Tony's Tour

    插头DP 楼教主男人八题之一! 要求从左下角走到右下角的哈密顿路径数量. 啊嘞,我只会求哈密顿回路啊……这可怎么搞…… 容易想到:要是把起点和重点直接连上就变成一条回路了……那么我们就连一下~ 我们可 ...

  2. [POJ 1739] Tony's Tour

    Link: POJ 1739 传送门 Solution: 这题除了一开始的预处理,基本上就是插头$dp$的模板题了 由于插头$dp$求的是$Hamilton$回路,而此题有起点和终点的限制 于是可以构 ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  5. 【POJ】1739 Tony's Tour

    http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include & ...

  6. POJ 1739 Tony's Tour(插头DP)

    Description A square township has been divided up into n*m(n rows and m columns) square plots (1< ...

  7. POJ 1739 Tony's Tour (DP)

    题意:从左下角到右下角有多少种走法. 析:特殊处理左下角和右下角即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000 ...

  8. POJ 1739 Tony's Tour (插头DP,轮廓线DP)

    题意:给一个n*m的矩阵,其中#是障碍格子,其他则是必走的格子,问从左下角的格子走到右下角的格子有多少种方式. 思路: 注意有可能答案是0,就是障碍格子阻挡住了去路. 插头DP有两种比较常见的表示连通 ...

  9. 【poj1739】 Tony's Tour

    http://poj.org/problem?id=1739 (题目链接) 题意 给出一个n*m的地图,有些是障碍.问从左下角走遍所有非障碍格子一次且仅一次最终到达右下角的路径方案数. Solutio ...

随机推荐

  1. 文件系统常用命令df、du、fsck、dumpe2fs

    df 查看文件系统 [root@localhost ~]# df 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda5 16558080 1337676 15220404 9% / de ...

  2. Parcel:常见技术栈的集成方式

    前言 Parcel 是什么 Parcel 是一个前端构建工具,Parcel 官网 将它定义为极速零配置的Web应用打包工具.没错,又是一个构建工具,你一定会想,为什么前端的构建工具层出不穷,搞那么多工 ...

  3. iOS sourceTree忽略掉必要的xcuserdata文件

    1.找到git对应的文件 git status 结果 会得到已经修改的文件. modified: Zing.xcodeproj/xcuserdata/tiny.xcuserdatad/xcscheme ...

  4. OC学习9——反射机制

    1.OC提供了3种编程方式与运行环境进行交互: 直接通过OC的源代码:这是最常见的方式,开发人员只是编写OC源代码,而运行环境负责在后台工作. 通过NSObject类中定义的方法进行动态编程:因为绝大 ...

  5. 【Zookeeper】源码分析之服务器(三)之LeaderZooKeeperServer

    一.前言 前面分析了ZooKeeperServer源码,由于QuorumZooKeeperServer的源码相对简单,于是直接分析LeaderZooKeeperServer. 二.LeaderZooK ...

  6. visual studio 2012打开提示 未能将网站×××配置为使用 ASP.NET 4.5 和 尚未在Web服务器上注册,您需要手动将Web服务器配置为使用ASP.NET 4.5

    未能将网站×××配置为使用 ASP.NET 4.5.为了使此网站正确运行,您必须将它手动配置为使用ASP.NET 4.5. ASP.NET 4.5尚未在Web服务器上注册,您需要手动将Web服务器配置 ...

  7. JavaWeb之数据源连接池(1)---DBCP

    何为数据源呢?也就是数据的来源.我在前面的一篇文章<JavaWeb之原生数据库连接>中,采用了mysql数据库,数据来源于mysql,那么mysql就是一种数据源.在实际工作中,除了mys ...

  8. Linux服务器上的oracle数据导入和导出

    背景: 在同一台Linux服务器上,有两个数据库用户,分别为:database1,database2,如何把database1用户下面的所有的表和数据,导入到database2数据库(database ...

  9. CJOJ 血帆海盗

    Description 随着资本的扩大,藏宝海湾贸易亲王在卡利姆多和东部王 国大陆各建立了N/2 个港口.大灾变发生以后,这些港口之间失去了联系,相继脱离了藏宝海湾贸易亲王的管辖,各自为政.利益的驱动 ...

  10. Confluence5.4.4迁移至6.3.1

    1.数据备份 服务器查看: 2.安装破解文件及安装包至服务器 3.停止旧版本并启动安装 4.访问8090端口开始安装 5.获取授权码,需要能访问国外网站,并且有atlassian账号 6.将数据库连接 ...