题意:给定一个 n * m 的矩阵,问你能花出多少条回路。

#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 = 1e6 + 10;
const int mod = 50007;
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], next[maxm], sz;
LL f[maxm], state[maxm];
void clear(){ sz = 0; ms(head, -1); }
void push(LL st, LL ans){
int h = st % mod;
for(int i = head[h]; ~i; i = next[i])
if(st == state[i]){
f[i] += ans;
return ;
}
state[sz] = st;
f[sz] = ans;
next[sz] = head[h];
head[h] = sz++;
}
};
Hash dp[2];
int a[maxn][maxn], ch[maxn], code[maxn];
char s[maxn];
int tx, ty; void decode(int m, LL st){
for(int i = m; i >= 0; --i){
code[i] = st & 7;
st >>= 3;
}
} LL encode(int m){
int cnt = 1; ms(ch, -1);
LL st = 0; 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 shift(int m){
for(int i = m; i; --i) code[i] = code[i-1];
code[0] = 0;
} 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(up && left){
if(up == left){ // last grid
if(i != tx || j != ty) continue;
code[j] = code[j-1] = 0;
if(j == m) shift(m);
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
else{ //union two
code[j] = code[j-1] = 0;
for(int i = 0; i <= m; ++i)
if(code[i] == up) code[i] = left;
if(j == m) shift(m);
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
}
else if(up || left){
int t = max(up, left); // 0 or not
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;
if(j == m) shift(m);
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
}
else{ // product two
if(a[i][j+1] && a[i+1][j]){
code[j] = code[j-1] = 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]);
code[j] = code[j-1] = 0;
if(j == m) shift(m);
dp[cur^1].push(encode(m), dp[cur].f[k]);
}
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%s", s + 1);
for(int j = 1; j <= m; ++j)
if(s[j] == '.'){
tx = i; ty = j;
a[i][j] = 1;
}
}
if(tx == 0){ puts("0"); return 0; }
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;
}
LL ans = 0;
for(int i = 0; i < dp[cur].sz; ++i)
ans += dp[cur].f[i];
printf("%I64d\n", ans);
return 0;
}

  

Ural 1519 Formula 1 (DP)的更多相关文章

  1. bzoj 1814 Ural 1519 Formula 1 插头DP

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 942  Solved: 356[Submit][Sta ...

  2. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  3. 【BZOJ1814】Ural 1519 Formula 1 插头DP

    [BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...

  4. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  5. ural 1519 Formula 1(插头dp)

    1519. Formula 1 @ Timus Online Judge 干了一天啊!!!插头DP入门. 代码如下: #include <cstdio> #include <cstr ...

  6. bzoj1814: Ural 1519 Formula 1 动态规划 插头dp

    http://acm.timus.ru/problem.aspx?space=1&num=1519 题目描述 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数. ...

  7. URAL 1519 Formula 1(插头DP,入门题)

    Description Background Regardless of the fact, that Vologda could not get rights to hold the Winter ...

  8. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

  9. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

随机推荐

  1. Python学习思维导图

     刚学习Python时,边学边总结的,采用思维导图的形式, 适合回顾使用.内容参考<Python:从入门到实践>一书.   再给出一张Datacamp网站上的一张关于Python基础的总结 ...

  2. Web验证方式(2)--Form Authentication

    Form验证方式并不是HTTP标准,而是在微软ASP.NET Web框架下提供的一种验证方式.其大致流程如下: 在上图的流程中,ASP.NET框架提供了如下支持类:( FormsAuthenticat ...

  3. 字符串循环右移N位

    给一个长度为n的字符串,把这个字符串循环右移N位(0<N<n),要求只用O(1)的额外空间和O(N)时间,有些什么方法 一开始想到的是先保存temp=s[0],在左起第N个移到s[0]的位 ...

  4. jquery 绘图工具 flot 学习笔记

    今天想做一个统计图表,像163博客的流量统计一样的,借助 flot 实现了,而且很简单. flot网址:http://code.google.com/p/flot/ 下载 JS 文件,使用方法和 jq ...

  5. 【linux】Linux系统信息查看命令大全

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  6. T4模板批量生成代码文件

    <#@ template debug="false" hostspecific="true" language="C#" #> ...

  7. 转转转-精通js正则表达式

    原文地址:http://www.cnblogs.com/aaronjs/archive/2012/06/30/2570970.html 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字 ...

  8. 深入解析thinkphp中的addAll方法

  9. icape3 的使用

    在FPGA中,有时需要使用用户代码重配置FPGA,配置的内容可以是flash或者是其他的来源这样FPGA的启动模式有关,在本实验中配置文件是存放在flash中.实际的操作步骤如下: 1:生成一个工程, ...

  10. Linux常用命令之-grep

    简介 grep全称Global Regular Expression Print是一种强大的文本搜索工具,它能使用给定的正则表达式按行搜索文本输出,文件,目录等,统计并输出匹配的信息,grep在文本查 ...