题意:给定一个 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. java并发回答

    java 线程安全比较重要的点:   在 java 编程中,记住这些关键点可以帮你避免一些严重的并发问题,比如条件竞争或死锁.   1).不可变对象默认是线程安全的,因为他们一旦被创建就不会被修改.比 ...

  2. 什么是HBase(六)性能调优

    硬件层面 内存要大,最好是ECC(Error Checking and Correcting),实现内存的动态纠错:CPU要多路(每个路彼此隔离)每个路一个CPU,每个cpu上面一般都是2~12核. ...

  3. Spring Cloud 入门 之 Eureka 篇(一)

    原文地址:Spring Cloud 入门 之 Eureka 篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Cloud 是一系列框架的有序集合.它利用 Sp ...

  4. python 书籍推荐 一

    最近"瑞丽模特学Python"的热点牵动了大江南北程序员的心,有人说这是炒作,也有人说这是推广Python的爆点...我嘿嘿一笑,美女就是美女,眼光那是杠杠的,不仅人美,学的语言也 ...

  5. ANSI和UNICODE编程的注意事项

    建立UNICODE编码工程 在VC60下,默认方式下建立的是ANSI编码的工程(注:编译的exe内部,其资源字符是以UNICODE保存),建立UNICODE编码工程的方法: 1.为工程添加UNICOD ...

  6. URL 收录

    http://www.cnblogs.com/Olive116/p/3426957.html 地图控件 http://www.cnblogs.com/tugenhua0707/ 前端

  7. Air test 基于屏幕比例实现滑动的方法

    # -*- encoding=utf8 -*- __author__ = "chenshanju" __docs__ = "基于iOS类实现滑动" from a ...

  8. php error_log记录日志的使用方法和配置 (日志目录一定要手动创建)

    对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...

  9. centos7.3下apache搭建django[未成功]

    1 apache肯定已经按照完毕了, 如果没有  yum install httpd yum install mod_wsgi 安装完成之后,mod_wsgi.so会在Apache的modules目录 ...

  10. Java——poi读取Excel文件

    1.创建文件流,打开EXCEL文件 FileInputStream excelFile = new FileInputStream(excelPath); XSSFWorkbook workbook ...