POJ 1739:Tony's Tour

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=177147;
int i;
struct na{
    int x,z;
    na(int xx,int zz):x(xx),z(zz){}
};
int n,m,x,y,z,a[21],k,en,p1,p2;
char u[100];
bool map[21][21];
long long f[2][mn+1],ans;
int v[2][mn+1];
int re[21][21];
queue <na> q;
inline int gx(int x,int q1,int q2){k=0;for (register int i=m+1;i;i--) k=k*3+(i==x?q1:(i==x+1?q2:a[i]));return k;}
inline void up(int x,int z,long long lj){
    x++;
    k=x%2;
    if (v[k][z]!=x) v[k][z]=x,f[k][z]=0,q.push(na(x,z));
    f[k][z]+=lj;
}
int main(){
    register int i,j;
    while(scanf("%d%d",&n,&m)!=EOF){
        if (n==0&&m==0) break;
        ans=0;
        memset(map,0,sizeof(map));memset(v,0,sizeof(v));memset(re,0,sizeof(re));memset(f,0,sizeof(f));
        for (i=1;i<=n;i++){
            scanf("%s",&u);
            for (j=1;j<=m;j++)
            map[j][n-i+3]=u[j-1]==‘.‘;
        }
        n+=2;
        en=n*m-1;
        map[1][2]=1;map[m][2]=1;
        for (i=1;i<=m;i++) map[i][1]=1;
        while(!map[en%m+1][en/m+1]) en--;
        f[0][0]=v[0][0]=1;
        q.push(na(0,0));
        while(!q.empty()){
            na no=q.front();q.pop();
            long long an=f[no.x%2][no.z];
            if(no.x%m==0) no.z*=3;
            x=no.x%m+1;y=no.x/m+1;
            for (i=1;i<=m+1;i++) a[i]=0;
            for (i=1,j=no.z;j;i++,j/=3) a[i]=j%3;
            if (!map[x][y])up(no.x,gx(x,0,0),an);else
            if (a[x]==1&&a[x+1]==2){
                if (no.x==en) ans+=an;
            }else if (a[x]==2&&a[x+1]==1) up(no.x,gx(x,0,0),an);else
                if (a[x]==0&&a[x+1]==0){
                if (map[x][y+1]&&map[x+1][y]) up(no.x,gx(x,1,2),an);
            }else if (a[x]==0){
                if (map[x+1][y]) up(no.x,gx(x,0,a[x+1]),an);
                if (map[x][y+1]) up(no.x,gx(x,a[x+1],0),an);
            }else if (a[x+1]==0){
                if (map[x+1][y]) up(no.x,gx(x,0,a[x]),an);
                if (map[x][y+1]) up(no.x,gx(x,a[x],0),an);
            }else if (a[x]==a[x+1]){
                p1=p2=0;
                if (a[x]==1)
                for (j=0,i=x+2;i<=m;i++){
                    if (a[i]==1) j--;
                    if (a[i]==2) j++;
                    if (j>0&&!p1) p1=i,j--;
                    if (j>0&&p1){p2=i;break;}
                }else
                for (j=0,i=x-1;i;i--){
                    if (a[i]==1) j++;
                    if (a[i]==2) j--;
                    if (j>0&&!p2) p2=i,j--;
                    if (j>0&&p2){p1=i;break;}
                }
                a[p1]=1;a[p2]=2;up(no.x,gx(x,0,0),an);
            }
        }
        printf("%I64d\n",ans);
    }
}

POJ 1739:Tony's Tour

时间: 2025-01-18 10:51:20

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

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 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.

【POJ】【1739】Tony&#39;s Tour

插头DP 楼教主男人八题之一! 要求从左下角走到右下角的哈密顿路径数量. 啊嘞,我只会求哈密顿回路啊……这可怎么搞…… 容易想到:要是把起点和重点直接连上就变成一条回路了……那么我们就连一下~ 我们可以在整张图下面加两行:(例:3*5) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 红色的是原来的起点和终点,下面紫色的路径将这两个点连了起来= =然后这题瞬间就变回了Formula 1那题……求哈密顿回路数量,so easy有没有~(其实不这样

【POJ】1739 Tony&#39;s Tour

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)) #

【插头DP】 POJ 1739 Tony&#39;s Tour

通道:http://poj.org/problem?id=1739 题意:左下角走到右下角路径数,单回路.做法就是我们新添2行后寻找回路就可以啦 . 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int MAX_N = 13; 8 const int MAX_M = 13; 9 const int HASH =

poj 1739 Tony&#39;s Tour 插头dp模板题

题意: 给一个迷宫,求左下角到右下角的路径数. 分析: 插头dp的模板题,建议先看cdq的论文再看代码,这份代码在模板基础上略微有改动.论文地址http://wenku.baidu.com/view/ed2b3e23482fb4daa58d4b74.html 代码: #include <iostream> using namespace std; const int maxD=16; const int HASH=10007; const int STATE=1000024; int N,M;

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i