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. mongodb 复制集

    mongodb 复制集 复制集简介 Mongodb复制集由一组Mongod实例(进程)组成,包含一个Primary节点和多个Secondary节点,Mongodb Driver(客户端)的所有数据都写 ...

  2. Node.js前言

    最近在学Node.js,所以学到一点东西就更新在上面吧,如果有错误,欢迎大家指正.

  3. Mybatis篇总结

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 JDBC写法 //sql: String sql = "s ...

  4. ArcGIS 网络分析[1.1] 创建用于网络分析用的线类型shp文件[这个太基础了吧!]

    具体的准备,在上一篇就说过了,不再赘述. 阅读本篇前,需要的预备知识是:ArcGIS创建各种矢量数据的方法,了解地理坐标与投影坐标 本篇只创建单一的线数据,至于点数据,以后进行复杂的网络分析时再添加进 ...

  5. RabbitMQ 1-入门学习

    环境: 软件环境MacOS ,Homebrew包管理工具 IDE: Eclipse 项目:Maven项目 1.安装RabbitMQ Server: 方式一:通过homebrew :终端执行:brew ...

  6. tp3.2 事务处理

    事务的机制通常被概括为“ACID”原则即原子性(A).稳定性(C).隔离性(I)和持久性(D).  原子性:构成事务的的所有操作必须是一个逻辑单元,要么全部执行,要么全部不执行.  稳定性:数据库在事 ...

  7. gitlab 接入 openldap、AD

    =============================================== 20171009_第2次修改                       ccb_warlock === ...

  8. Android 7.1 WindowManagerService 屏幕旋转流程分析 (三)

    三.屏幕的绘制 performSurfacePlacement()函数来触发window的绘制,这里最大的循环次数是6,当然一般不会到最大次数就会被Scheduled. final void perf ...

  9. ntopng 推送solr

    1.修改代码在且不说 2.修改完之后先卸载原先的ntopng 使用 whereis ntopng 找到安装目录,然后删除 /usr/local/bin/ntopng /usr/local/share/ ...

  10. 完美解决浮动IE6 7中的兼容性BUG问题

    想要创建出漂亮的网页设计,除了要认真学习每一个html和CSS代码之外,不可能不去了解一下臭名昭著的IE6和更早的那些IE浏览器的坏脾气,因为你本来写出的规规矩矩的代码,漂亮的设计就此就要完成了,却发 ...