您的位置:首页 > 其它

POJ 1739 Tony's Tour (DP)

2017-10-07 14:44 405 查看
题意:从左下角到右下角有多少种走法。

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

代码如下:

#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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: