题意:从左下角到右下角有多少种走法。

析:特殊处理左下角和右下角即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int maxm = 1e5 + 10;
const int mod = 30007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Hash{
int head[mod], state[maxm], next[maxm];
int f[maxm];
int sz; void clear(){ sz = 0; ms(head, -1); }
void push(int st, int ans){
int h = st % mod;
for(int i = head[h]; ~i; i = next[i])
if(st == state[i]){
f[i] += ans;
return ;
}
f[sz] = ans;
state[sz] = st;
next[sz] = head[h];
head[h] = sz++;
}
};
Hash dp[2];
int a[maxn][maxn];
char s[maxn];
int code[maxn], ch[maxn]; void decode(int m, int st){
for(int i = m; i >= 0; --i){
code[i] = st & 7;
st >>= 3;
}
} int encode(int m){
int st = 0, cnt = 1;
ms(ch, -1); ch[0] = 0;
for(int i = 0; i <= m; ++i){
if(ch[code[i]] == -1) ch[code[i]] = cnt++;
code[i] = ch[code[i]];
st <<= 3;
st |= code[i];
}
return st;
} void dpblank(int i, int j, int cur){
for(int k = 0; k < dp[cur].sz; ++k){
decode(m, dp[cur].state[k]);
int left = code[j-1];
int up = code[j];
if(i == n && (j == 1 || j == m)){ //special deal
if(left && up) continue;
if(left || up){
code[j] = code[j-1] = 0;
dp[cur^1].push(encode(j == m ? m-1 : m), dp[cur].f[k]);
}
else if(a[i][j+1]){
code[j-1] = 0;
code[j] = 13;
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
continue;
}
if(left && up){
if(left == up) continue;
code[j] = code[j-1] = 0;
for(int i = 0; i <= m; ++i)
if(code[i] == up) code[i] = left;
dp[cur^1].push(encode(j == m ? m-1 : m), dp[cur].f[k]);
}
else if(left || up){
int t = max(left, up);
if(a[i][j+1]){
code[j-1] = 0;
code[j] = t;
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
if(a[i+1][j]){
code[j-1] = t;
code[j] = 0;
dp[cur^1].push(encode(j == m ? m-1 : m), dp[cur].f[k]);
}
}
else{
if(a[i+1][j] && a[i][j+1]){
code[j-1] = code[j] = 13;
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
}
}
} void dpblock(int i, int j, int cur){
for(int k = 0; k < dp[cur].sz; ++k){
decode(m, dp[cur].state[k]);
dp[cur^1].push(encode(j == m ? m-1 : m), dp[cur].f[k]);
}
} int main(){
while(scanf("%d %d", &n, &m) == 2 && n + m){
ms(a, 0);
for(int i = 1; i <= n; ++i){
scanf("%s", s+1);
for(int j = 1; j <= m; ++j)
if(s[j] == '.') a[i][j] = 1;
}
int cur = 0;
dp[cur].cl; dp[cur].push(0, 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
dp[cur^1].cl;
if(a[i][j]) dpblank(i, j, cur);
else dpblock(i, j, cur);
cur ^= 1;
}
int ans = 0;
for(int i = 0; i < dp[cur].sz; ++i)
ans += dp[cur].f[i];
printf("%d\n", ans);
}
return 0;
}

  

POJ 1739 Tony's Tour (DP)的更多相关文章

  1. [POJ 1739] Tony's Tour

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

  2. 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< ...

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

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

  4. 【POJ】1739 Tony's Tour

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

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

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

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  8. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  9. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

随机推荐

  1. win10 下ie11安装flash debuger (install flashplayer debuger on win10 64bit)

    1不能安装的现象 由于win10  ie11  内置flash  微软不让用户自己手动更新ie11的flash以及安装flash  debugger  ,这怕是让还在用 flex 开发的大胸弟们很头疼 ...

  2. Centos7配置 SNMP服务

    本文转载至:http://blog.51cto.com/5001660/2097212   一.安装yum源安装SNMP软件包 1.更新yum源: yum clean all yum makecach ...

  3. bzoj 3924 [Zjoi2015]幻想乡战略游戏——动态点分治(暴力移动找重心)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3924 度数只有20,所以从一个点暴力枚举其出边,就能知道往哪个方向走. 知道方向之后直接走到 ...

  4. IE9 下面, XMLHttpRequest 是不支持跨域请求的解决方法

    在 IE9 下面, XMLHttpRequest 是不支持跨域请求的. IE10 的 XMLHttpRequest 支持跨域, 而 IE8, IE9 需要使用 XDomainRequest 来实现跨域 ...

  5. redhat 防火墙禁止允许的IP链接指定的端口

    编辑:iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 192.168.4.3 --dport 80 -j A ...

  6. 峰Spring4学习(8)spring对事务的支持

    一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...

  7. python笔记--2018-2019

    一:读取json文件的方法 import json json.loads(open('./users.dev.json', 'r').read())     #获取文件的类容,并且序列化把看似列表的字 ...

  8. yaml语言教程

    大家直接去看阮一峰的教程. http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt 简介 基本语法规则: 大小写敏感 使用缩进表示层级关系 缩进时不 ...

  9. PhpStorm 配置在浏览器中打开文件

    这东东中文资料太少- -鼓捣了好久才有结果 点击File-Settings-Deployment .点+按钮增加服务器 Mapping 设置工程路径等 点击 Edit Confi 这东东中文资料太少- ...

  10. adb端口占用及模拟器调试

    首先在使用ADB前所有手机辅助类软件 1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr &quo ...